@@ -11,26 +11,18 @@ import { assign } from './util';
1111 * store.setState({ c: 'd' }); // logs { a: 'b', c: 'd' }
1212 */
1313export default function createStore ( state ) {
14- let listeners = [ ] ;
14+ const listeners = [ ] ;
1515 state = state || { } ;
1616
1717 function unsubscribe ( listener ) {
18- let out = [ ] ;
19- for ( let i = 0 ; i < listeners . length ; i ++ ) {
20- if ( listeners [ i ] === listener ) {
21- listener = null ;
22- }
23- else {
24- out . push ( listeners [ i ] ) ;
25- }
26- }
27- listeners = out ;
18+ const i = listeners . indexOf ( listener ) ;
19+ ~ i && listeners . splice ( i , 1 ) ;
2820 }
2921
3022 function setState ( update , overwrite , action ) {
31- state = overwrite ? update : assign ( assign ( { } , state ) , update ) ;
32- let currentListeners = listeners ;
33- for ( let i = 0 ; i < currentListeners . length ; i ++ ) currentListeners [ i ] ( state , action ) ;
23+ state = overwrite ? update : assign ( { } , state , update ) ;
24+ let i = listeners . length ;
25+ while ( i -- > 0 ) listeners [ i ] ( state , action ) ;
3426 }
3527
3628 /** An observable state container, returned from {@link createStore}
@@ -46,15 +38,15 @@ export default function createStore(state) {
4638 * @returns {Function } boundAction()
4739 */
4840 action ( action ) {
49- function apply ( result ) {
50- setState ( result , false , action ) ;
41+ function apply ( update ) {
42+ setState ( update , false , action ) ;
5143 }
5244
5345 // Note: perf tests verifying this implementation: https://esbench.com/bench/5a295e6299634800a0349500
5446 return function ( ) {
55- let args = [ state ] ;
56- for ( let i = 0 ; i < arguments . length ; i ++ ) args . push ( arguments [ i ] ) ;
57- let ret = action . apply ( this , args ) ;
47+ const args = [ state ] ;
48+ for ( let i = 0 ; i < arguments . length ; i ++ ) args . push ( arguments [ i ] ) ;
49+ const ret = action . apply ( this , args ) ;
5850 if ( ret != null ) {
5951 if ( ret . then ) ret . then ( apply ) ;
6052 else apply ( ret ) ;
0 commit comments