blob: 4fe46d1474d228d4b0ab7681d50180006d0c35a2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
export class Emitter {
constructor() { this._handlers = new Map(); }
on(type, fn) {
if (!this._handlers.has(type)) this._handlers.set(type, new Set());
this._handlers.get(type).add(fn);
return () => this.off(type, fn);
}
off(type, fn) { this._handlers.get(type)?.delete(fn); }
emit(type, payload) {
for (const fn of this._handlers.get(type) || []) {
try { fn(payload); } catch (err) { console.error(err); }
}
}
}
|