其实就是发布订阅模式

    1. export class BusEvent {
    2. constructor() {
    3. this.handlers = new Map();
    4. }
    5. on(type, handler) {
    6. const handlers = this.handlers.get(type);
    7. const added = handlers && handlers.push(handler);
    8. if (!added) {
    9. this.handlers.set(type, [handler]);
    10. }
    11. }
    12. off(type, handler) {
    13. const handlers = this.handlers.get(type);
    14. if (handlers) {
    15. handlers.splice(handlers.indexOf(handler) >>> 0, 1)
    16. }
    17. }
    18. emit(type, evt) {
    19. (this.handlers.get(type) || []).slice().map(handler => handler(evt));
    20. }
    21. }