11import shallowEqual from './utils/shallowEqual' ;
2+ import wrapActionCreators from './utils/wrapActionCreators' ;
3+ import invariant from 'invariant' ;
4+ import _ from 'lodash' ;
25
36export default class Connector {
47
58 constructor ( store ) {
6- this . store = store ;
7- this . unsubscribe = undefined ;
9+ this . _store = store ;
10+ this . _defaultMapStateToTarget = ( ) => ( { } ) ;
11+ this . _defaultMapDispatchToTarget = dispatch => ( { dispatch } ) ;
812 }
913
10- disconnect ( ) {
11- this . unsubscribe ( ) ;
12- }
14+ connect = ( mapStateToTarget , mapDispatchToTarget ) => {
15+
16+ const finalMapStateToTarget = mapStateToTarget || this . _defaultMapStateToTarget ;
17+
18+ const finalMapDispatchToTarget = _ . isPlainObject ( mapDispatchToTarget ) ?
19+ wrapActionCreators ( mapDispatchToTarget ) :
20+ mapDispatchToTarget || this . _defaultMapDispatchToTarget ;
21+
22+ invariant (
23+ _ . isFunction ( finalMapStateToTarget ) ,
24+ 'mapStateToTarget must be a Function. Instead received $s.' , finalMapStateToTarget
25+ ) ;
26+
27+ invariant (
28+ _ . isPlainObject ( finalMapDispatchToTarget ) || _ . isFunction ( finalMapDispatchToTarget ) ,
29+ 'mapDispatchToTarget must be a plain Object or a Function. Instead received $s.' , finalMapDispatchToTarget
30+ ) ;
31+
32+ let slice = this . getStateSlice ( this . _store . getState ( ) , finalMapStateToTarget ) ;
33+
34+ const boundActionCreators = finalMapDispatchToTarget ( this . _store . dispatch ) ;
1335
14- connect ( selectors , callback , disableCaching = false ) {
15- if ( ! Array . isArray ( selectors ) ) {
16- selectors = [ selectors ] ;
36+ return ( target ) => {
37+
38+ invariant (
39+ _ . isFunction ( target ) || _ . isObject ( target ) ,
40+ 'The target parameter passed to connect must be a Function or a plain object.'
41+ ) ;
42+
43+ //Initial update
44+ this . updateTarget ( target , slice , boundActionCreators ) ;
45+
46+ const unsubscribe = this . _store . subscribe ( ( ) => {
47+ const nextSlice = this . getStateSlice ( this . _store . getState ( ) , finalMapStateToTarget ) ;
48+ if ( ! shallowEqual ( slice , nextSlice ) ) {
49+ slice = nextSlice ;
50+ this . updateTarget ( target , slice , boundActionCreators ) ;
51+ }
52+ } ) ;
53+ return unsubscribe ;
1754 }
1855
19- //Initial update
20- let params = selectors . map ( selector => selector ( this . store . getState ( ) ) ) ;
21- callback ( ...params ) ;
56+ }
2257
23- this . unsubscribe = this . store . subscribe ( ( ) => {
24- let nextParams = selectors . map ( selector => selector ( this . store . getState ( ) ) ) ;
25- if ( disableCaching || ! shallowEqual ( params , nextParams ) ) {
26- callback ( ...nextParams ) ;
27- params = nextParams ;
28- }
29- } ) ;
3058
31- return this ;
59+ updateTarget ( target , StateSlice , dispatch ) {
60+ if ( _ . isFunction ( target ) ) {
61+ target ( StateSlice , dispatch ) ;
62+ } else {
63+ _ . assign ( target , StateSlice , dispatch ) ;
64+ }
3265 }
3366
34- getStore ( ) {
35- return this . store ;
67+ getStateSlice ( state , mapStateToScope ) {
68+ const slice = mapStateToScope ( state ) ;
69+
70+ invariant (
71+ _ . isPlainObject ( slice ) ,
72+ '`mapStateToScope` must return an object. Instead received %s.' ,
73+ slice
74+ ) ;
75+
76+ return slice ;
3677 }
3778
3879}
0 commit comments