* 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>
41 lines
897 B
TypeScript
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);
|
|
});
|
|
}
|
|
}
|