1
- //创建EventBus对象
2
- let EventBus = function ( ) { } ;
3
- //准备数组容器
4
- var objBus = [ ] , arrBus = [ ] ;
5
- //添加方法
6
- EventBus . prototype = {
7
- obj : {
8
- set : function ( key , action ) {
9
- if ( key && action ) {
10
- var map = { } ;
11
- map . k = key ;
12
- map . v = action ;
13
- //如果存在,则删除之前添加的事件
14
- for ( var i = 0 , busLength = objBus . length ; i < busLength ; i ++ ) {
15
- var tempMap = objBus [ i ] ;
16
- if ( tempMap . k === key ) {
17
- objBus . splice ( i , 1 ) ;
18
- }
19
- }
20
- objBus . push ( map ) ;
21
- }
22
- } ,
23
- get : function ( key ) {
24
- if ( key ) {
25
- for ( var i = 0 , busLength = objBus . length ; i < busLength ; i ++ ) {
26
- var map = objBus [ i ] ;
27
- if ( map . k === key ) {
28
- return map . v ( ) ;
29
- }
30
- }
1
+ function EventBusClass ( ) {
2
+ this . msgQueues = { } ;
3
+ }
4
+
5
+ EventBusClass . prototype = {
6
+ // 将消息保存到当前的消息队列中
7
+ on : function ( msgName , func ) {
8
+ if ( Object . prototype . hasOwnProperty . call ( this . msgQueues , msgName ) ) {
9
+ if ( typeof this . msgQueues [ msgName ] === 'function' ) {
10
+ this . msgQueues [ msgName ] = [ this . msgQueues [ msgName ] , func ] ;
11
+ } else {
12
+ this . msgQueues [ msgName ] = [ ...this . msgQueues [ msgName ] , func ] ;
31
13
}
14
+ } else {
15
+ this . msgQueues [ msgName ] = func ;
32
16
}
33
17
} ,
34
- emit : function ( key , data ) {
35
- if ( key ) {
36
- for ( var i = 0 , busLength = arrBus . length ; i < busLength ; i ++ ) {
37
- var map = arrBus [ i ] ;
38
- if ( map . k === key ) {
39
- return map . v ( data ) ;
40
- }
41
- }
42
- }
43
- return new Promise ( ( resolve ) => { resolve ( ) ; } ) ;
18
+ // 消息队列中仅保存一个消息
19
+ one : function ( msgName , func ) {
20
+ // 无需检查msgName是否存在
21
+ this . msgQueues [ msgName ] = func ;
44
22
} ,
45
- on : function ( key , action ) {
46
- if ( key && action ) {
47
- var map = { } ;
48
- map . k = key ;
49
- map . v = action ;
50
- arrBus . push ( map ) ;
23
+ // 发送消息
24
+ emit : function ( msgName , msg ) {
25
+ if ( ! Object . prototype . hasOwnProperty . call ( this . msgQueues , msgName ) ) {
26
+ return ;
27
+ }
28
+ if ( typeof this . msgQueues [ msgName ] === 'function' ) {
29
+ this . msgQueues [ msgName ] ( msg ) ;
30
+ } else {
31
+ this . msgQueues [ msgName ] . map ( ( fn ) => {
32
+ fn ( msg ) ;
33
+ } ) ;
51
34
}
52
35
} ,
53
- arr : {
54
- push : function ( key , action ) {
55
- if ( key && action ) {
56
- var map = { } ;
57
- map . k = key ;
58
- map . v = action ;
59
- arrBus . push ( map ) ;
60
- }
61
- } ,
62
- pop : function ( key ) {
63
- if ( key ) {
64
- for ( var i = 0 , busLength = arrBus . length ; i < busLength ; i ++ ) {
65
- var map = arrBus [ i ] ;
66
- if ( map . k === key ) {
67
- map . v ( ) ;
68
- }
69
- }
70
- }
36
+ // 移除消息
37
+ off : function ( msgName ) {
38
+ if ( ! Object . prototype . hasOwnProperty . call ( this . msgQueues , msgName ) ) {
39
+ return ;
71
40
}
41
+ delete this . msgQueues [ msgName ] ;
72
42
}
73
43
} ;
74
- var eventBus = new EventBus ( ) ;
75
- export default eventBus ;
76
- // module.exports = {
77
- // eventBus: eventBus
78
- // }
44
+
45
+ // 将EventBus放到window对象中
46
+ const EventBus = new EventBusClass ( ) ;
47
+
48
+ export default EventBus ;
0 commit comments