5
5
* @description : 一个简单的观察者模式事件系统实现
6
6
*/
7
7
8
- function EventHandle ( ) {
9
- var events = { } ;
10
- this . on = function ( event , callback ) {
11
- callback = callback || function ( ) { } ;
12
- if ( typeof events [ event ] === 'undefined' ) {
13
- events [ event ] = [ callback ] ;
8
+ const Noop = ( ) => { } ;
9
+
10
+ class EventEmitter {
11
+ constructor ( ) {
12
+ this . events = [ ] ;
13
+ }
14
+
15
+ on ( event , handler = Noop ) {
16
+ if ( typeof this . events [ event ] === 'undefined' ) {
17
+ this . events [ event ] = [ handler ] ;
14
18
} else {
15
- events [ event ] . push ( callback ) ;
19
+ this . events [ event ] . push ( handler ) ;
16
20
}
17
- } ;
21
+ }
18
22
19
- this . emit = function ( event , args ) {
20
- if ( typeof events [ event ] !== 'undefined' ) {
21
- events [ event ] . forEach ( function ( fn ) {
22
- fn ( args ) ;
23
- } ) ;
23
+ once ( event , handler = Noop ) {
24
+ const once = `once_${ event } ` ;
25
+ if ( typeof this . events [ once ] === 'undefined' ) {
26
+ this . events [ once ] = [ handler ] ;
24
27
} else {
25
- throw new Error ( 'event: ' + event + ', not found' ) ;
28
+ this . events [ once ] . push ( handler ) ;
26
29
}
27
- } ;
30
+ }
31
+
32
+ emit ( event , args ) {
33
+ const once = `once_${ event } ` ;
34
+ if ( typeof this . events [ once ] !== 'undefined' ) {
35
+ this . events [ once ] . forEach ( ( handler ) => {
36
+ handler ( args ) ;
37
+ } ) ;
38
+ delete this . events [ `once_${ event } ` ] ;
39
+ }
40
+ if ( typeof this . events [ event ] !== 'undefined' ) {
41
+ this . events [ event ] . forEach ( ( handler ) => {
42
+ handler ( args ) ;
43
+ } ) ;
44
+ }
45
+ }
28
46
29
- this . off = function ( event ) {
30
- if ( typeof events [ event ] !== 'undefined' ) {
31
- delete events [ event ] ;
47
+ off ( event , handler ) {
48
+ if ( typeof this . events [ event ] !== 'undefined' ) {
49
+ if ( ! ! handler ) {
50
+ const index = this . events [ event ] . indexOf ( handler ) ;
51
+ this . events [ event ] . splice ( index , 1 ) ;
52
+ } else {
53
+ delete this . events [ event ] ;
54
+ }
32
55
}
33
56
} ;
34
57
}
35
58
36
59
function test ( ) {
37
- var eh = new EventHandle ( ) ;
60
+ const eh = new EventEmitter ( ) ;
61
+
62
+ const firstHd = ( str ) => { console . log ( 'first greet: ' , str ) ; } ;
38
63
39
- eh . on ( 'greet' , function ( str ) {
40
- console . log ( str ) ;
41
- } ) ;
64
+ eh . on ( 'greet' , firstHd ) ;
42
65
43
- eh . on ( 'greet' , function ( name ) {
44
- console . log ( name + ', hello!' ) ;
45
- } ) ;
66
+ eh . on ( 'greet' , ( str ) => { console . log ( 'second greet: ' , str ) ; } ) ;
46
67
47
- eh . on ( 'bye' , function ( name ) {
48
- console . log ( name + ', goodbye!' ) ;
49
- } ) ;
68
+ eh . on ( 'bye' , ( name ) => { console . log ( name + ', goodbye!' ) ; } ) ;
69
+
70
+ eh . once ( 'break' , ( str ) => { console . log ( `once break: ${ str } ` ) ; } ) ;
71
+ eh . on ( 'break' , ( str ) => { console . log ( `on break: ${ str } ` ) ; } ) ;
50
72
51
73
console . log ( '====== start ======' ) ;
52
74
eh . emit ( 'greet' , 'Green' ) ;
53
75
eh . emit ( 'bye' , 'Mark' ) ;
76
+ eh . emit ( 'break' , 'Jack' ) ;
77
+ eh . emit ( 'break' , 'Tony' ) ;
54
78
55
79
console . log ( '====== removeListener ======' ) ;
56
80
eh . off ( 'bye' ) ;
57
81
eh . emit ( 'bye' , 'Mark' ) ;
82
+
83
+ eh . off ( 'greet' , firstHd ) ;
84
+ eh . emit ( 'greet' , 'Green' ) ;
58
85
}
59
86
60
87
test ( ) ;
0 commit comments