1+ var slice = [ ] . slice
2+
13function Emitter ( ctx ) {
24 this . _ctx = ctx || this
35}
46
57var EmitterProto = Emitter . prototype
68
7- EmitterProto . on = function ( event , fn ) {
9+ EmitterProto . on = function ( event , fn ) {
810 this . _cbs = this . _cbs || { }
911 ; ( this . _cbs [ event ] = this . _cbs [ event ] || [ ] )
1012 . push ( fn )
1113 return this
1214}
1315
14- EmitterProto . once = function ( event , fn ) {
16+ EmitterProto . once = function ( event , fn ) {
1517 var self = this
1618 this . _cbs = this . _cbs || { }
1719
@@ -25,7 +27,7 @@ EmitterProto.once = function(event, fn){
2527 return this
2628}
2729
28- EmitterProto . off = function ( event , fn ) {
30+ EmitterProto . off = function ( event , fn ) {
2931 this . _cbs = this . _cbs || { }
3032
3133 // all
@@ -56,7 +58,11 @@ EmitterProto.off = function(event, fn){
5658 return this
5759}
5860
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 ) {
6066 this . _cbs = this . _cbs || { }
6167 var callbacks = this . _cbs [ event ]
6268
@@ -70,4 +76,22 @@ EmitterProto.emit = function(event, a, b, c){
7076 return this
7177}
7278
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+
7397module . exports = Emitter
0 commit comments