jan/web/app/_services/eventsService.ts
Louis 27258433d1
#357 plugin & app can subscribe and emit events (#358)
* feature: event based plugin

* chore: update README.md

* Update yarn script for build plugins (#363)

* Update yarn script for build plugins

* Plugin-core install from npmjs instead of from local

---------

Co-authored-by: Hien To <>

* #360 plugin preferences (#361)

* feature: #360 plugin preferences

* chore: update core-plugin README.md

* chore: create collections on start

* chore: bumb core version

* chore: update README

* chore: notify preferences update

* fix: preference update

---------

Co-authored-by: hiento09 <136591877+hiento09@users.noreply.github.com>
2023-10-16 10:23:38 +00:00

41 lines
897 B
TypeScript

export class EventEmitter {
private handlers: Map<string, Function[]>;
constructor() {
this.handlers = new Map<string, Function[]>();
}
public on(eventName: string, handler: Function): void {
if (!this.handlers.has(eventName)) {
this.handlers.set(eventName, []);
}
this.handlers.get(eventName)?.push(handler);
}
public off(eventName: string, handler: Function): void {
if (!this.handlers.has(eventName)) {
return;
}
const handlers = this.handlers.get(eventName);
const index = handlers?.indexOf(handler);
if (index !== undefined && index !== -1) {
handlers?.splice(index, 1);
}
}
public emit(eventName: string, args: any): void {
if (!this.handlers.has(eventName)) {
return;
}
const handlers = this.handlers.get(eventName);
handlers?.forEach((handler) => {
handler(args);
});
}
}