/
message.js
77 lines (73 loc) · 1.6 KB
/
message.js
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* Simple Pub/Sub module
* @tencent/message and 减掉fns依赖
**/
'use strict';
function Message() {
this._evtObjs = {};
}
Message.prototype.on = function (evtType, handler, _once) {
if (!this._evtObjs[evtType]) {
this._evtObjs[evtType] = [];
}
this._evtObjs[evtType].push({
handler: handler,
once: _once
})
var that = this
return function () {
that.off(evtType, handler)
}
}
Message.prototype.off = function (evtType, handler) {
var types;
if (evtType) {
types = [evtType];
} else {
types = Object.keys(this._evtObjs)
}
var that = this;
types.forEach(function (type) {
if (!handler) {
// remove all
that._evtObjs[type] = [];
} else {
var handlers = that._evtObjs[type] || [],
nextHandlers = [];
handlers.forEach(function (evtObj) {
if (evtObj.handler !== handler) {
nextHandlers.push(evtObj)
}
})
that._evtObjs[type] = nextHandlers;
}
})
return this;
}
Message.prototype.emit = function (evtType) {
var args = Array.prototype.slice.call(arguments, 1)
var handlers = this._evtObjs[evtType] || [];
handlers.forEach(function (evtObj) {
if (evtObj.once && evtObj.called) return
evtObj.called = true
try {
evtObj.handler && evtObj.handler.apply(null, args);
} catch(e) {
console.error(e.stack || e.message || e)
}
})
}
Message.prototype.assign = function (target) {
var msg = this;
['on', 'off', 'emit', 'assign'].forEach(function (name) {
var method = msg[name]
target[name] = function () {
return method.apply(msg, arguments)
}
})
}
/**
* Global Message Central
**/
;(new Message()).assign(Message)
module.exports = Message;