@@ -65,21 +65,28 @@ export default class InternalEventBus {
6565 }
6666
6767 _chain ( key , args , initialValue , fallback ) {
68- var value = initialValue ;
68+ const subs = this . events [ key ] ;
6969
70- if ( ! Array . isArray ( args ) ) {
71- args = [ args ] ;
72- }
70+ if ( subs && subs . length ) {
71+ if ( ! Array . isArray ( args ) ) {
72+ args = [ args ] ;
73+ }
7374
74- if ( this . subscribed ( key ) ) {
75- this . events [ key ] . forEach ( ( subscriber , i ) => {
76- value = subscriber . callback . apply ( this , args . concat ( [ value ] ) ) ;
77- } ) ;
75+ //reuse a single args array across subscribers, mutating only the trailing
76+ //accumulator slot, instead of allocating a fresh concat per subscriber
77+ const callArgs = args . slice ( ) ;
78+ const valueIndex = callArgs . length ;
79+ let value = initialValue ;
80+
81+ for ( let i = 0 ; i < subs . length ; i ++ ) {
82+ callArgs [ valueIndex ] = value ;
83+ value = subs [ i ] . callback . apply ( this , callArgs ) ;
84+ }
7885
7986 return value ;
80- } else {
81- return typeof fallback === "function" ? fallback ( ) : fallback ;
8287 }
88+
89+ return typeof fallback === "function" ? fallback ( ) : fallback ;
8390 }
8491
8592 _confirm ( key , args ) {
@@ -110,53 +117,39 @@ export default class InternalEventBus {
110117 }
111118 }
112119
113- _dispatch ( ) {
114- var args = Array . from ( arguments ) ,
115- key = args . shift ( ) ;
116-
117- if ( this . events [ key ] ) {
118- this . events [ key ] . forEach ( ( subscriber ) => {
120+ _dispatch ( key , ...args ) {
121+ const subs = this . events [ key ] ;
122+ if ( subs ) {
123+ for ( const subscriber of subs ) {
119124 subscriber . callback . apply ( this , args ) ;
120- } ) ;
125+ }
121126 }
122127 }
123128
124- _debugDispatch ( ) {
125- var args = Array . from ( arguments ) ,
126- key = args [ 0 ] ;
127-
128- args [ 0 ] = "InternalEvent:" + key ;
129-
129+ _debugDispatch ( key , ...args ) {
130130 if ( this . debug === true || this . debug . includes ( key ) ) {
131- console . log ( ...args ) ;
131+ const debugArgs = [ "InternalEvent:" + key , ...args ] ;
132+ console . log ( ...debugArgs ) ;
132133 }
133134
134- return this . _dispatch ( ...arguments ) ;
135+ return this . _dispatch ( key , ...args ) ;
135136 }
136137
137- _debugChain ( ) {
138- var args = Array . from ( arguments ) ,
139- key = args [ 0 ] ;
140-
141- args [ 0 ] = "InternalEvent:" + key ;
142-
138+ _debugChain ( key , args , initialValue , fallback ) {
143139 if ( this . debug === true || this . debug . includes ( key ) ) {
144- console . log ( ...args ) ;
140+ const debugArgs = [ "InternalEvent:" + key , args , initialValue , fallback ] ;
141+ console . log ( ...debugArgs ) ;
145142 }
146143
147- return this . _chain ( ... arguments ) ;
144+ return this . _chain ( key , args , initialValue , fallback ) ;
148145 }
149146
150- _debugConfirm ( ) {
151- var args = Array . from ( arguments ) ,
152- key = args [ 0 ] ;
153-
154- args [ 0 ] = "InternalEvent:" + key ;
155-
147+ _debugConfirm ( key , args ) {
156148 if ( this . debug === true || this . debug . includes ( key ) ) {
157- console . log ( ...args ) ;
149+ const debugArgs = [ "InternalEvent:" + key , args ] ;
150+ console . log ( ...debugArgs ) ;
158151 }
159152
160- return this . _confirm ( ... arguments ) ;
153+ return this . _confirm ( key , args ) ;
161154 }
162- }
155+ }
0 commit comments