Skip to main content

Broadcast<P>

Broadcast event class allows for emitting and listening for events

Type Parameters

P

P extends object = Payload<string, any>

Constructors

Constructor

new Broadcast<P>(config?): Broadcast<P>

Broadcast class constructor.

Parameters

config?

BroadcastConfig

Returns

Broadcast<P>

Methods

addListener()

protected addListener(type, listener): void

Check for the existence of a event type and create it if missing.

Parameters

type

P["type"]

The event type.

listener

Listener<P>

Returns

void


deleteEvent()

deleteEvent(type): void

Delete an event and unregister all callbacks associated with it.

Parameters

type

P["type"]

The event to delete.

Returns

void


destroy()

destroy(): void

Destroy the BroadcastChannel. After calling this, no further messages will be received.

Returns

void


emit()

Call Signature

emit<T>(type, payload?, options?): void

Emit an event to all listening contexts.

Type Parameters
T

T extends string

The Payload type, inferred from the event.

Parameters
type

T

The event type.

payload?

undefined

The event payload.

options?

EmitOptions

Returns

void

Example
bus.emit(
EVENTS.LAYER_CLICK,
{
worldSpace: pickInfo.coordinate,
screenSpace: pickInfo.pixel,
index: pickInfo.index,
object: pickInfo.object,
},
);

Call Signature

emit<T>(type, payload, options?): void

Emit an event to all listening contexts.

Type Parameters
T

T extends string

The Payload type, inferred from the event.

Parameters
type

T

The event type.

payload

ExtractEvent<P, T> extends object ? Data : undefined

The event payload.

options?

EmitOptions

Returns

void

Example
bus.emit(
EVENTS.LAYER_CLICK,
{
worldSpace: pickInfo.coordinate,
screenSpace: pickInfo.pixel,
index: pickInfo.index,
object: pickInfo.object,
},
);

handleListeners()

protected handleListeners(data): void

Iterate through listeners for the given topic and invoke callbacks if criteria match.

Parameters

data

P

The event payload containing type, optional payload, and optional targetId.

Returns

void

Remarks

If targetId is provided, delivery is scoped to a specific browser context. We assume exactly one bus instance per context, so events are delivered only when target === this.uuid. If omitted, the event is treated as a broadcast within this context (audience filtering may occur elsewhere).


init()

protected init(): void

Initialize the BroadcastChannel and set up event listeners.

Returns

void


off()

off<T>(type, callback): void

Unregister callback for the specified event type.

Type Parameters

T

T extends string

The Payload type, inferred from the event.

Parameters

type

T

The event type.

callback

(data) => void

Returns

void


on()

on<T>(type, callback): () => void

Register a callback to be executed when a message of the specified event type is received.

Type Parameters

T

T extends string

The Payload type, inferred from the event.

Parameters

type

T

The event type.

callback

(data) => void

The callback function.

Returns

(): void

Returns

void

Example

bus.on(EVENTS.MAP_CLICK, (e) => {
if (!e.payload.picked) {
setSelected(null);
}
});

once()

once<T>(type, callback): () => void

Register a callback to be executed only once for a specified event type.

Type Parameters

T

T extends string

The Payload type, inferred from the event.

Parameters

type

T

The event type.

callback

(data) => void

The callback function.

Returns

(): void

Returns

void


onError()

protected onError(error): void

Handle errors from the BroadcastChannel.

Parameters

error

MessageEvent<Error>

Error event.

Returns

void


onMessage()

protected onMessage(event): void

Process incoming messages.

Parameters

event

MessageEvent<P>

Incoming message event.

Returns

void


removeListener()

protected removeListener(type, id): void

Removes a listener by id.

Parameters

type

P["type"]

id

UniqueId

Returns

void


setEventEmitOptions()

setEventEmitOptions(type, options): void

Set emit options for event by type, options will apply to all emits of this event

Keep in mind that options are merged: global, event, local (lowest to highest precendence)

Parameters

type

P["type"]

event type

options

emit options

null | EmitOptions

Returns

void


setEventsEmitOptions()

setEventsEmitOptions(events): void

Set a series of events emit options

Parameters

events

Map<P["type"], null | EmitOptions>

map of event type & options

Returns

void


setGlobalEmitOptions()

setGlobalEmitOptions(options): void

Set global emit options, the lowest precedence options, to be merged with event emit options and local options

Parameters

options

emit options

null | EmitOptions

Returns

void


getInstance()

static getInstance<T>(config?): Broadcast<T>

Get the singleton instance of Broadcaster.

Type Parameters

T

T extends object = { target?: UniqueId; type: string; } | { payload: any; target?: UniqueId; type: string; }

Parameters

config?

BroadcastConfig

Optional custom configuration.

Returns

Broadcast<T>

Properties

channel

protected channel: null | BroadcastChannel = null


channelName

protected channelName: string = DEFAULT_CONFIG.channelName


emitOptions

protected emitOptions: Map<P["type"], EmitOptions>


id

readonly id: UniqueId


listeners

protected listeners: Partial<Record<P["type"], Listener<P>[]>> = {}