Emiter.js
1.4 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
// //////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2016-present All Rights Reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// http://www.apache.org/licenses/LICENSE-2.0
//
// Github Home: https://github.com/AlexWang1987
// Author: AlexWang
// Date: 2017-05-20 11:55:44
// QQ Email: 1669499355@qq.com
// Last Modified time: 2017-05-20 11:55:45
// Description: saas-Emiter
//
// //////////////////////////////////////////////////////////////////////////////
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) {
if (eid) {
//eid=* broadcast
let asteriskStub = this.MAPS['*'];
if (asteriskStub && asteriskStub.length) {
asteriskStub.forEach(function (elistener) {
elistener(eid, data);
})
}
// eid= normal
let stub = this.MAPS[eid];
if (stub && stub.length) {
stub.forEach(function (elistener) {
elistener(data);
});
}
}
}
}