Class: Swarm<Events>
The class that provides peer communication between browser tabs and service workers
Extends
TypedEventEmitter<Events>
Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
Events extends TypedEventMap | TypedEventMap | The map of events and their arguments that can be emitted and listened to. |
Accessors
id
Get Signature
get id(): string;The ID of the current instance.
Returns
string
leader
Get Signature
get leader(): boolean;Whether this instance is the leader or not.
Returns
boolean
Methods
collect()
collect<R, E>(event: E, ...args: Events[E]): Promise<R[]>;Collect responses from all peers for a specific event.
Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
R | - | The type of the response. |
E extends string | number | symbol | keyof Events | The name of the event to collect responses for. |
Parameters
| Parameter | Type | Description |
|---|---|---|
event | E | The name of the event to collect responses for. |
...args | Events[E] | The arguments which will be passed to the event handler. |
Returns
Promise<R[]>
A promise that resolves with an array of responses from all peers.
emit()
emit<E>(event: E, ...args: Events[E]): this;Emit an event to all connected peers.
Type Parameters
| Type Parameter | Description |
|---|---|
E extends string | number | symbol | The name of the event to emit. |
Parameters
| Parameter | Type | Description |
|---|---|---|
event | E | The name of the event to emit. |
...args | Events[E] | The arguments to pass to the event listeners. |
Returns
this
Overrides
TypedEventEmitter.emit;off()
off<E>(event: E, listener?: (...args: Events[E]) => void): this;Remove a listener.
Type Parameters
| Type Parameter |
|---|
E extends string | number | symbol |
Parameters
| Parameter | Type |
|---|---|
event | E |
listener? | (...args: Events[E]) => void |
Returns
this
Inherited from
TypedEventEmitter.off;offCollect()
offCollect<E>(event: E): Swarm<Events>;De-register a handler for a specific event which will respond to incoming messages which require a general response
Type Parameters
| Type Parameter | Description |
|---|---|
E extends string | number | symbol | The name of the event to listen for. |
Parameters
| Parameter | Type | Description |
|---|---|---|
event | E | The name of the event to listen for. |
Returns
Swarm<Events>
offLeadershipChange()
offLeadershipChange(cb: (is: boolean) => void): Swarm<Events>;Remove the leadership change event listener.
Parameters
| Parameter | Type | Description |
|---|---|---|
cb | (is: boolean) => void | The callback function to be removed from the leadership change event. |
Returns
Swarm<Events>
offRequest()
offRequest<E>(event: E): Swarm<Events>;De-register a handler for a specific event which will respond to incoming messages which require a response from the leader
Type Parameters
| Type Parameter | Description |
|---|---|
E extends string | number | symbol | The name of the event to listen for. |
Parameters
| Parameter | Type | Description |
|---|---|---|
event | E | The name of the event to listen for. |
Returns
Swarm<Events>
on()
on<E>(
event: E,
listener: (...args: Events[E]) => void,
ctx?: any): this;Subscribe to an event with a typed listener.
Type Parameters
| Type Parameter |
|---|
E extends string | number | symbol |
Parameters
| Parameter | Type |
|---|---|
event | E |
listener | (...args: Events[E]) => void |
ctx? | any |
Returns
this
Inherited from
TypedEventEmitter.on;once()
once<E>(
event: E,
listener: (...args: Events[E]) => void,
ctx?: any): this;Subscribe once to an event.
Type Parameters
| Type Parameter |
|---|
E extends string | number | symbol |
Parameters
| Parameter | Type |
|---|---|
event | E |
listener | (...args: Events[E]) => void |
ctx? | any |
Returns
this
Inherited from
TypedEventEmitter.once;onceLeadershipChange()
onceLeadershipChange(cb: (is: boolean) => void): Swarm<Events>;Subscribe to the next occurance of a leadership change event.
Parameters
| Parameter | Type | Description |
|---|---|---|
cb | (is: boolean) => void | The callback function to be called when the leadership status changes. |
Returns
Swarm<Events>
onCollect()
onCollect<E>(event: E, handler: (...args: Events[E]) => ReturnType<RequestHandlerMap<Events>[E]>): Swarm<Events>;Register a handler for a specific event which will respond to incoming messages which require a general response
Type Parameters
| Type Parameter | Description |
|---|---|
E extends string | number | symbol | The name of the event to listen for. |
Parameters
| Parameter | Type | Description |
|---|---|---|
event | E | The name of the event to listen for. |
handler | (...args: Events[E]) => ReturnType<RequestHandlerMap<Events>[E]> | The function to call when the event is emitted. |
Returns
Swarm<Events>
onLeadershipChange()
onLeadershipChange(cb: (is: boolean) => void): Swarm<Events>;Subscribe to the leadership change event.
Parameters
| Parameter | Type | Description |
|---|---|---|
cb | (is: boolean) => void | The callback function to be called when the leadership status changes. |
Returns
Swarm<Events>
onRequest()
onRequest<E>(event: E, handler: (...args: Events[E]) => ReturnType<RequestHandlerMap<Events>[E]>): Swarm<Events>;Register a handler for a specific event which will respond to incoming messages which require a response from the leader
Type Parameters
| Type Parameter | Description |
|---|---|
E extends string | number | symbol | The name of the event to listen for. |
Parameters
| Parameter | Type | Description |
|---|---|---|
event | E | The name of the event to listen for. |
handler | (...args: Events[E]) => ReturnType<RequestHandlerMap<Events>[E]> | The function to call when the event is emitted. |
Returns
Swarm<Events>
request()
request<R, E>(event: E, ...args: Events[E]): Promise<R>;Request feedback from the leader for a specific event.
Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
R | any | The type of the response. |
E extends string | number | symbol | keyof Events | The name of the event to request feedback for. |
Parameters
| Parameter | Type | Description |
|---|---|---|
event | E | The name of the event to request feedback for. |
...args | Events[E] | The arguments to pass to the event handler. |
Returns
Promise<R>
A promise that resolves with the response from the event handler.
setAwaitResponseTimeout()
setAwaitResponseTimeout(timeout: number): Swarm<Events>;Set the timeout for waiting for a response from request() or collect().
Parameters
| Parameter | Type | Description |
|---|---|---|
timeout | number | The timeout in milliseconds to wait for a response. |
Returns
Swarm<Events>
The current instance of the Swarm class for the current context.
instance()
static instance<Events>(secret?: string | Secret<string>): Swarm<Events>;Retreive the current instance of the Swarm class for the current context.
Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
Events extends TypedEventMap | TypedEventMap | The map of events and their arguments that can be emitted and listened to. |
Parameters
| Parameter | Type |
|---|---|
secret? | string | Secret<string> |
Returns
Swarm<Events>
The current instance of the Swarm class for the current context.