eventbus.js
1.5 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
60
61
62
63
64
65
66
67
/*
from github: https://github.com/littlebaozi/EventEmitter
author: littlebaozi
license: unknown
*/
class EventBus {
// 订阅
on (event, fn, context) {
// 判断fn是否是函数
if(typeof fn != "function"){
console.error('fn must be a function');
return;
}
// 将event的fn存放在store中
this._stores = this._stores || {};
(this._stores[event] = this._stores[event] || []).push({cb: fn, ctx: context});
}
// 发布
emit (event) {
this._stores = this._stores || {};
var store = this._stores[event],args;
// 遍历执行事件
if(store){
store = store.slice(0);
args = [].slice.call(arguments, 1); //获取传入的参数,https://segmentfault.com/q/1010000005643934
for(var i =0,len = store.length;i < len; i++){
store[i].cb.apply(store[i].ctx, args);
}
}
}
// 注销
off (event, fn) {
this._stores = this._stores || {};
// 删除所有
if(!arguments.length){
this._stores = [];
return;
}
var store = this._stores[event];
if(!store){
return;
}
// 删除指定event
if(arguments.length === 1){
delete this._stores[event];
return;
}
// 删除指定fn
for(var i=0, len = store.length; i < len; i++){
if(fn === store[i].cb){
store.splice(i,1); //splice slice
}
}
}
}
module.exports = EventBus;