11import * as React from 'react' ;
22import PropTypes from 'prop-types' ;
33import initHistory , { Traveler } from './history' ;
4- import { from , Stream } from 'most' ;
4+ import { from , Stream , Subscription } from 'most' ;
55import { Engine , EngineSubject } from './engine/most' ;
66// 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
77export const REACT_MOST_ENGINE = '@@reactive-react/react-most.engine' ;
@@ -10,49 +10,35 @@ const CONTEXT_TYPE = {
1010 [ REACT_MOST_ENGINE ] : PropTypes . object
1111} ;
1212
13- interface History < T > extends Stream < T > {
14- cursor : number
15- travel : Stream < T >
16- forward : ( ) => void
17- backward : ( ) => void
18- }
1913interface Actions < T > {
2014 [ propName : string ] : ( ...v : any [ ] ) => T
2115}
22- interface Props {
23- [ propName : string ] : any
24- }
25- interface Plan < T > {
26- ( intent : EngineSubject < T > , props ?: Props ) : Process < T >
16+
17+ interface Plan < I , S > {
18+ ( intent : EngineSubject < I > , props ?: { } ) : Process < I , S >
2719}
2820interface Update < S > {
2921 ( current : S ) : S
3022}
31- interface Process < T > {
32- actions : Actions < T > ,
33- updates : Stream < Update < T > >
23+ interface Process < I , S > {
24+ actions : Actions < I > ,
25+ updates : Stream < Update < S > >
3426}
3527
36- interface ConnectProps < T > {
37- actions : Actions < T >
38- }
39- interface ReactClass { }
40- interface Connect {
41- contextTypes ?: any
42- new ( props ?, context ?) : any
28+ interface ConnectProps < I > {
29+ actions : Actions < I >
4330}
4431const h = React . createElement ;
45- function connect < T > ( main : Plan < T > , opts = { history : false } ) : ( rc : React . ComponentClass < Props > ) => React . ComponentClass < ConnectProps < T > > {
46- return function ( WrappedComponent : React . ComponentClass < Props > ) {
32+ function connect < I , S > ( main : Plan < I , S > , opts = { history : false } ) {
33+ return function ( WrappedComponent : React . ComponentClass < any > ) {
4734 let connectDisplayName = `Connect(${ getDisplayName ( WrappedComponent ) } )` ;
4835 if ( WrappedComponent . contextTypes === CONTEXT_TYPE ) {
49- return class ConnectNode extends React . PureComponent < ConnectProps < T > , any > {
50- actions : Actions < T >
51- updates : Stream < Update < T > >
52- props : ConnectProps < T >
36+ return class ConnectNode extends React . PureComponent < ConnectProps < I > , any > {
37+ actions : Actions < I >
38+ updates : Stream < Update < S > >
5339 static contextTypes = CONTEXT_TYPE
5440 static displayName = connectDisplayName
55- constructor ( props : ConnectProps < T > , context ) {
41+ constructor ( props : ConnectProps < I > , context ) {
5642 super ( props , context ) ;
5743 let { actions, updates } = main ( context [ REACT_MOST_ENGINE ] . historyStream , props )
5844 this . updates = updates
@@ -69,23 +55,23 @@ function connect<T>(main: Plan<T>, opts = { history: false }): (rc: React.Compon
6955 }
7056 }
7157 } else {
72- return class ConnectLeaf < T , S > extends React . PureComponent < ConnectProps < T > , S > {
73- actions : Actions < T >
74- updates : Stream < Update < T > >
75- props : ConnectProps < T >
58+ return class ConnectLeaf extends React . PureComponent < ConnectProps < I > , S > {
59+ actions : Actions < I >
60+ updates : Stream < Update < S > >
7661 traveler : Traveler < S >
62+ subscription : Subscription < S >
7763 constructor ( props , context ) {
7864 super ( props , context ) ;
79- let engine : Engine < T , S > = context [ REACT_MOST_ENGINE ]
65+ let engine : Engine < I , S > = context [ REACT_MOST_ENGINE ]
8066 if ( opts . history || props . history ) {
8167 this . traveler = initHistory ( engine . historyStream , engine . travelStream ) ;
8268 this . traveler . travel . forEach ( state => {
8369 return this . setState ( state ) ;
8470 } ) ;
8571 }
8672
87- let { actions, updates } = main ( engine . intentStream , props )
88- this . updates = updates . merge ( props . updates )
73+ let { actions, updates } = main ( context [ REACT_MOST_ENGINE ] . engine . intentStream , props )
74+ this . updates = props . updates ? updates . merge ( props . updates ) : updates
8975 this . actions = Object . assign ( { } , actions , props . actions ) ;
9076 let defaultKey = Object . keys ( WrappedComponent . defaultProps ) ;
9177 this . state = Object . assign (
@@ -98,14 +84,14 @@ function connect<T>(main: Plan<T>, opts = { history: false }): (rc: React.Compon
9884 this . setState ( state => pick ( Object . keys ( state ) , nextProps ) ) ;
9985 }
10086 componentDidMount ( ) {
101- this . subscriptions = this . context [ REACT_MOST_ENGINE ] . observe (
87+ this . subscription = this . context [ REACT_MOST_ENGINE ] . observe (
10288 this . updates ,
10389 action => {
10490 if ( action instanceof Function ) {
10591 this . setState ( ( prevState , props ) => {
10692 let newState = action . call ( this , prevState , props ) ;
10793 if ( opts . history && newState != prevState ) {
108- opts . history . cursor = - 1 ;
94+ this . traveler . cursor = - 1 ;
10995 this . context [ REACT_MOST_ENGINE ] . historyStream . send ( prevState ) ;
11096 }
11197 return newState ;
@@ -122,7 +108,7 @@ function connect<T>(main: Plan<T>, opts = { history: false }): (rc: React.Compon
122108 ) ;
123109 }
124110 componentWillUnmount ( ) {
125- this . subscriptions . unsubscribe ( ) ;
111+ this . subscription . unsubscribe ( ) ;
126112 }
127113 render ( ) {
128114 return h (
@@ -146,7 +132,7 @@ export interface MostEngine<I, H> {
146132export default class Most < I , H , S > extends React . PureComponent < MostProps < I , H > , S > {
147133 static childContextTypes = CONTEXT_TYPE
148134 getChildContext ( ) : MostEngine < I , H > {
149- let engine : Engine < I , H > = ( this . props && this . props . engine && new this . props . engine ) || new Engine < I , H > ( ) ;
135+ let engine : Engine < I , H > = ( this . props && this . props . engine ) || new Engine < I , H > ( ) ;
150136 /* istanbul ignore if */
151137 if ( process . env . NODE_ENV === 'debug' ) {
152138 inspect ( engine ) ;
0 commit comments