index/classes/GlobalAndRoomCallbackDispatcher.md
Global and Room Callback Dispatcher
Dispatches callbacks to both global and room-specific listeners in the correct order. Global callbacks execute first, then room-specific callbacks.
Used in StreamWebSocketService to ensure consistent callback ordering across different event types.
Features:
- Global-first execution order
- Isolated error handling per listener
- Type-safe callback dispatch
- Logging for debugging
Example
// Test case: Global and room-specific callbacks
interface MyEvent { tokenName: string; value: number }
type MyCallback = (event: MyEvent) => void;
const dispatcher = new GlobalAndRoomCallbackDispatcher<MyEvent, MyCallback>();
const calls: string[] = [];
// Register global callback
dispatcher.registerGlobalCallback((event) => {
calls.push(`global:${event.tokenName}`);
});
// Register room-specific callbacks
dispatcher.registerRoomCallback('token:anime', (event) => {
calls.push(`room:anime:${event.value}`);
});
dispatcher.registerRoomCallback('token:gala', (event) => {
calls.push(`room:gala:${event.value}`);
});
// Dispatch event
const event: MyEvent = { tokenName: 'anime', value: 42 };
dispatcher.dispatch('token:anime', event);
// Global callback should execute first
console.assert(calls[0] === 'global:anime');
console.assert(calls[1] === 'room:anime:42');
// Unrelated room callback not called
console.assert(!calls.some(c => c.startsWith('room:gala')));
Since
6.6.0
Type Parameters
| Type Parameter | Description |
|---|---|
TEvent | Event type being dispatched |
TCallback extends (event) => void | Callback function type |
Constructors
Constructor
new GlobalAndRoomCallbackDispatcher<
TEvent,TCallback>(logger?):GlobalAndRoomCallbackDispatcher<TEvent,TCallback>
Parameters
| Parameter | Type |
|---|---|
logger? | Logger |
Returns
GlobalAndRoomCallbackDispatcher<TEvent, TCallback>
global and room callback dispatcher
Methods
clear()
clear():
void
Clear all callbacks
Returns
void
clearRoom()
clearRoom(
roomId):void
Remove all callbacks for a specific room
Parameters
| Parameter | Type | Description |
|---|---|---|
roomId | string | Room identifier |
Returns
void
clear room id
dispatch()
dispatch(
roomId,event):void
Dispatch an event to global and room-specific callbacks
Calls global callbacks first, then room-specific callbacks. Errors in one callback don't prevent others from executing.
Parameters
| Parameter | Type | Description |
|---|---|---|
roomId | string | Room identifier for this event |
event | TEvent | Event data to dispatch |
Returns
void
room id
registerGlobalCallback()
registerGlobalCallback(
callback):void
Register a global callback
Parameters
| Parameter | Type | Description |
|---|---|---|
callback | TCallback | Function to call for all events |
Returns
void
register global callback
registerRoomCallback()
registerRoomCallback(
roomId,callback):void
Register a room-specific callback
Parameters
| Parameter | Type | Description |
|---|---|---|
roomId | string | Room identifier (e.g., 'token:anime') |
callback | TCallback | Function to call for events in this room |
Returns
void
register room callback id