Emiter.js 954 字节

export default class Emiter {
  constructor() {
    this.MAPS = {};
  }
  on(eid, elistener) {
    if (eid && elistener) {
      let stub = this.MAPS[eid];
      if (!stub) {
        return this.MAPS[eid] = [elistener];
      }
      stub.push(elistener);
    }
  }
  off(eid, elistener) {
    if (eid) {
      let stub =this.MAPS[eid];
      if (stub) {
        if (elistener) {
          return stub.splice(stub.indexOf(elistener), 1);
        }
        stub.length = 0;
      }
    }
  }
  _emit(eid, data,data2) {
    if (eid) {
      //eid=* broadcast
      let asteriskStub =this.MAPS['*'];
      if (asteriskStub && asteriskStub.length) {
        asteriskStub.forEach(function (elistener) {
          elistener(eid, data,data2);
        })
      }

      // eid= normal
      let stub = this.MAPS[eid];
      if (stub && stub.length) {
        stub.forEach(function (elistener) {
          elistener(data,data2);
        });
      }
    }
  }
}