Skip to main content

Broadcast<P>

Broadcast event class allows for emitting and listening for events

Type Parameters

P

P extends Payload = Payload

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 even 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()

emit<T>(type, payload): 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>["payload"]

The event payload.

Returns

void

Example

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

getEvents()

getEvents(): P["type"][]

Get a list of all available events.

Returns

P["type"][]


handleListeners()

protected handleListeners(data): void

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

Parameters

data

P

Returns

void


init()

protected init(): void

Initialize the BroadcastChannel and set up event listeners.

Returns

void


off()

off<T>(type, callback): void

Unregister all callbacks 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 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

number

Returns

void


getInstance()

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

Get the singleton instance of Broadcaster.

Type Parameters

T

T extends Payload = Payload

Parameters

config?

BroadcastConfig

Optional custom configuration.

Returns

Broadcast<T>

Properties

channel

protected channel: null | BroadcastChannel = null


channelName

protected channelName: string


listenerCounter

protected listenerCounter: number = 0


listeners

protected listeners: Record<string, Listener<P>[]> = {}