11import * as React from 'react' ;
22import { PropTypes } from 'prop-types' ;
33import initHistory , { Traveler } from './history' ;
4- import { Plan , Actions , Connect , ConnectProps , EngineSubject , Update , ConnectClass } from './interfaces'
5- import { from , Stream , Subscription , mergeArray } from 'most' ;
4+ import { Plan , Connect , ConnectClass } from './interfaces'
5+ import { from , Stream , Subscription } from 'most' ;
66import { Engine } from './engine/most' ;
77
88// unfortunately React doesn't support symbol as context key yet, so let me just preteding using Symbol until react implement the Symbol version of Object.assign
@@ -12,30 +12,29 @@ const CONTEXT_TYPE = {
1212 [ REACT_MOST_ENGINE ] : PropTypes . object
1313} ;
1414
15- export function connect < I , S > ( main : Plan < I , S > , opts = { history : false } ) : ( WrappedComponent : React . ComponentClass < any > ) => ConnectClass < I , S > {
16- return function ( WrappedComponent : ConnectClass < I , S > ) {
15+ function isConnectClass < I , S > ( ComponentClass : ConnectClass < I , S > | React . ComponentClass < any > ) : ComponentClass is ConnectClass < I , S > {
16+ return ( < ConnectClass < I , S > > ComponentClass ) . contextTypes == CONTEXT_TYPE ;
17+ }
18+ export type ConnectOrReactComponent < I , S > = ConnectClass < I , S > | React . ComponentClass < any >
19+
20+ export function connect < I , S > ( main : Plan < I , S > , opts = { history : false } ) : ( WrappedComponent : ConnectOrReactComponent < I , S > ) => ConnectClass < I , S > {
21+ return function ( WrappedComponent : ConnectOrReactComponent < I , S > ) {
1722 let connectDisplayName = `Connect(${ getDisplayName ( WrappedComponent ) } )` ;
18- if ( WrappedComponent . contextTypes === CONTEXT_TYPE ) {
23+ if ( isConnectClass ( WrappedComponent ) ) {
1924 return class ConnectNode extends WrappedComponent {
20- actions : Actions < I >
21- update$ : Stream < Update < S > >
22- main : Plan < I , S >
2325 static contextTypes = CONTEXT_TYPE
2426 static displayName = connectDisplayName
2527 constructor ( props , context ) {
2628 super ( props , context ) ;
2729 let { actions, update$ } = main ( context [ REACT_MOST_ENGINE ] . intentStream , props )
28- this . update$ = this . update$ . merge ( update$ )
29- this . actions = Object . assign ( { } , bindActions ( actions , context [ REACT_MOST_ENGINE ] . intentStream , this ) , this . actions ) ;
30+ this . machine = {
31+ update$ : this . machine . update$ . merge ( update$ ) ,
32+ actions : Object . assign ( { } , bindActions ( actions , context [ REACT_MOST_ENGINE ] . intentStream , this ) , this . machine . actions )
33+ }
3034 }
3135 }
3236 } else {
3337 return class ConnectLeaf extends Connect < I , S > {
34- actions : Actions < I >
35- update$ : Stream < Update < S > >
36- traveler : Traveler < S >
37- subscription : Subscription < S >
38- main : Plan < I , S >
3938 static contextTypes = CONTEXT_TYPE
4039 static displayName = connectDisplayName
4140 constructor ( props , context ) {
@@ -48,8 +47,10 @@ export function connect<I, S>(main: Plan<I, S>, opts = { history: false }): (Wra
4847 } ) ;
4948 }
5049 let { actions, update$ } = main ( engine . intentStream , props )
51- this . actions = bindActions ( actions , engine . intentStream , this )
52- this . update$ = update$
50+ this . machine = {
51+ actions : bindActions ( actions , engine . intentStream , this ) ,
52+ update$ : update$
53+ }
5354 let defaultKey = Object . keys ( WrappedComponent . defaultProps ) ;
5455 this . state = Object . assign (
5556 { } ,
@@ -62,7 +63,7 @@ export function connect<I, S>(main: Plan<I, S>, opts = { history: false }): (Wra
6263 }
6364 componentDidMount ( ) {
6465 this . subscription = this . context [ REACT_MOST_ENGINE ] . observe (
65- this . update$ ,
66+ this . machine . update$ ,
6667 action => {
6768 if ( action instanceof Function ) {
6869 this . setState ( ( prevState , props ) => {
@@ -91,7 +92,7 @@ export function connect<I, S>(main: Plan<I, S>, opts = { history: false }): (Wra
9192 return h (
9293 WrappedComponent ,
9394 Object . assign ( { } , opts , this . props , this . state , {
94- actions : this . actions ,
95+ actions : this . machine . actions ,
9596 traveler : this . traveler
9697 } )
9798 ) ;
0 commit comments