Emiter.js
954 字节
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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);
});
}
}
}
}