base con front
This commit is contained in:
17
public/lib/bus.js
Normal file
17
public/lib/bus.js
Normal file
@@ -0,0 +1,17 @@
|
||||
const listeners = new Map();
|
||||
|
||||
export function on(event, fn) {
|
||||
const arr = listeners.get(event) || [];
|
||||
arr.push(fn);
|
||||
listeners.set(event, arr);
|
||||
return () => off(event, fn);
|
||||
}
|
||||
|
||||
export function off(event, fn) {
|
||||
const arr = listeners.get(event) || [];
|
||||
listeners.set(event, arr.filter(x => x !== fn));
|
||||
}
|
||||
|
||||
export function emit(event, payload) {
|
||||
(listeners.get(event) || []).forEach(fn => fn(payload));
|
||||
}
|
||||
Reference in New Issue
Block a user