SubscriberMap
The SubscriberMap can be used for implementation of a pub/sub event system.
import { EventCallback, SubscriberMap } from '@archibald/core';
const eventSystem = new SubscriberMap();
subscribers
This method returns the subscribers map.
const subscribers = eventSystem.subscribers();
publish
This method publishes an event.
eventSystem.publish(PARAMETERS);
Parameters
| Name | Type | Optional | Description |
|---|---|---|---|
| eventName | Generic, defaults to string | A list with event names or an event name to subscribe to. | |
| data | any | ✔️ | Data that should be received in the callback function. |
publishAll
This method publishes all events.
eventSystem.publishAll();
Parameters
| Name | Type | Optional | Description |
|---|---|---|---|
| data | any | ✔️ | Data that should be received in the callback function. |
subscribe
This method subscribes to a list of events or an event with a callback function.
const unsubscribe = eventSystem.subscribe(PARAMETERS);
Parameters
| Name | Type | Description |
|---|---|---|
| eventNames | Generic, defaults to string | string[] | A list with event names or an event name to subscribe to. |
| callback | EventCallback | A callback function to execute when the events are published. |
unsubscribe
This method unsubscribes from all events by passing the callback function.
eventSystem.unsubscribe(PARAMETERS);
Parameters
| Name | Type | Description |
|---|---|---|
| callback | EventCallback | A callback function. |
unsubscribeAll
This method removes all subscribers from all events.
eventSystem.unsubscribeAll();