...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
export class EventBus implements EventTarget { private _eventListeners: Map<string, EventListenerOrEventListenerObject[]>; constructor() { this._eventListeners = new Map<string, EventListenerOrEventListenerObject[]>(); } addEventListener(type: string, callback: EventListenerOrEventListenerObject): void { if (!this._eventListeners.has(type)) { this._eventListeners.set(type, []); } this._eventListeners.get(type)!.push(callback); } dispatchEvent(event: Event): boolean { if (this._eventListeners.has(event.type)) { this._eventListeners .get(event.type) !.forEach((cb) => typeof cb === 'function' ? cb(event) : cb.handleEvent(event) ); return true; } return false; } removeEventListener(type: string, callback: EventListenerOrEventListenerObject): void { if (this._eventListeners.has(type)) { this._eventListeners.set(type, this._eventListeners.get(type)!.filter((cb) => cb !== callback)); } } } |
Drawio | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Alternatives
Instead of the central EventBus, we can let plugins dispatch events on the plugin itself. If a plugin needs an event from another plugin, It can listen to an event being dispatched to Open-SCD Core
.
...
Implementing a central event-bus means that current OSCD plugins need some refactoring to dispatch events to the event-bus instead of dispatching events on itself.
We can distinguish different type of events by Producer driven or Consumer driven.
For example, the oscd-open
event is a Consumer driven event. The API of this event is established in OpenSCD Core and should be adhered by any plugin.
Producer driven events can be seen as notification events. The producer is notifying anyone who is interested in this event.