Skip to main content

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 ParameterDescription
TEventEvent type being dispatched
TCallback extends (event) => voidCallback function type

Constructors​

Constructor​

new GlobalAndRoomCallbackDispatcher<TEvent, TCallback>(logger?): GlobalAndRoomCallbackDispatcher<TEvent, TCallback>

Parameters​

ParameterType
logger?Logger

Returns​

GlobalAndRoomCallbackDispatcher<TEvent, TCallback>

Methods​

clear()​

clear(): void

Clear all callbacks

Returns​

void


clearRoom()​

clearRoom(roomId): void

Remove all callbacks for a specific room

Parameters​

ParameterTypeDescription
roomIdstringRoom identifier

Returns​

void


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​

ParameterTypeDescription
roomIdstringRoom identifier for this event
eventTEventEvent data to dispatch

Returns​

void


registerGlobalCallback()​

registerGlobalCallback(callback): void

Register a global callback

Parameters​

ParameterTypeDescription
callbackTCallbackFunction to call for all events

Returns​

void


registerRoomCallback()​

registerRoomCallback(roomId, callback): void

Register a room-specific callback

Parameters​

ParameterTypeDescription
roomIdstringRoom identifier (e.g., 'token:anime')
callbackTCallbackFunction to call for events in this room

Returns​

void