1
+ var slice = [ ] . slice
2
+
1
3
function Emitter ( ctx ) {
2
4
this . _ctx = ctx || this
3
5
}
4
6
5
7
var EmitterProto = Emitter . prototype
6
8
7
- EmitterProto . on = function ( event , fn ) {
9
+ EmitterProto . on = function ( event , fn ) {
8
10
this . _cbs = this . _cbs || { }
9
11
; ( this . _cbs [ event ] = this . _cbs [ event ] || [ ] )
10
12
. push ( fn )
11
13
return this
12
14
}
13
15
14
- EmitterProto . once = function ( event , fn ) {
16
+ EmitterProto . once = function ( event , fn ) {
15
17
var self = this
16
18
this . _cbs = this . _cbs || { }
17
19
@@ -25,7 +27,7 @@ EmitterProto.once = function(event, fn){
25
27
return this
26
28
}
27
29
28
- EmitterProto . off = function ( event , fn ) {
30
+ EmitterProto . off = function ( event , fn ) {
29
31
this . _cbs = this . _cbs || { }
30
32
31
33
// all
@@ -56,7 +58,11 @@ EmitterProto.off = function(event, fn){
56
58
return this
57
59
}
58
60
59
- EmitterProto . emit = function ( event , a , b , c ) {
61
+ /**
62
+ * The internal, faster emit with fixed amount of arguments
63
+ * using Function.call
64
+ */
65
+ EmitterProto . emit = function ( event , a , b , c ) {
60
66
this . _cbs = this . _cbs || { }
61
67
var callbacks = this . _cbs [ event ]
62
68
@@ -70,4 +76,22 @@ EmitterProto.emit = function(event, a, b, c){
70
76
return this
71
77
}
72
78
79
+ /**
80
+ * The external emit using Function.apply
81
+ */
82
+ EmitterProto . applyEmit = function ( event ) {
83
+ this . _cbs = this . _cbs || { }
84
+ var callbacks = this . _cbs [ event ] , args
85
+
86
+ if ( callbacks ) {
87
+ callbacks = callbacks . slice ( 0 )
88
+ args = slice . call ( arguments , 1 )
89
+ for ( var i = 0 , len = callbacks . length ; i < len ; i ++ ) {
90
+ callbacks [ i ] . apply ( this . _ctx , args )
91
+ }
92
+ }
93
+
94
+ return this
95
+ }
96
+
73
97
module . exports = Emitter
0 commit comments