其实就是发布订阅模式
export class BusEvent {
constructor() {
this.handlers = new Map();
}
on(type, handler) {
const handlers = this.handlers.get(type);
const added = handlers && handlers.push(handler);
if (!added) {
this.handlers.set(type, [handler]);
}
}
off(type, handler) {
const handlers = this.handlers.get(type);
if (handlers) {
handlers.splice(handlers.indexOf(handler) >>> 0, 1)
}
}
emit(type, evt) {
(this.handlers.get(type) || []).slice().map(handler => handler(evt));
}
}