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'
45import { from , Stream , Subscription } from 'most' ;
5- import { Engine , EngineSubject } from './engine/most' ;
6+ import { Engine } from './engine/most' ;
7+
68// 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
79export const REACT_MOST_ENGINE = '@@reactive-react/react-most.engine' ;
8-
10+ const h = React . createElement ;
911const CONTEXT_TYPE = {
1012 [ REACT_MOST_ENGINE ] : PropTypes . object
1113} ;
1214
13- export interface Actions < T > {
14- [ propName : string ] : ( ...v : any [ ] ) => T
15- }
16-
17- export interface Plan < I , S > {
18- ( intent : EngineSubject < I > , props ?: { } ) : Process < I , S >
19- }
20- export interface Update < S > {
21- ( current : S ) : S
22- }
23- export interface Process < I , S > {
24- actions : Actions < I > ,
25- updates : Stream < Update < S > >
26- }
27-
28- export interface ConnectProps < I > {
29- actions : Actions < I >
30- }
31- const h = React . createElement ;
32- export class Connect < I , S > extends React . PureComponent < ConnectProps < I > , S > {
33- actions : Actions < I >
34- updates : Stream < Update < S > >
35- }
36- export interface ConnectClass < I , S > {
37- new ( props ?: ConnectProps < I > , context ?: any ) : Connect < I , S > ;
38- }
3915export function connect < I , S > ( main : Plan < I , S > , opts = { history : false } ) : ( WrappedComponent : React . ComponentClass < any > ) => ConnectClass < I , S > {
4016 return function ( WrappedComponent : React . ComponentClass < any > ) {
4117 let connectDisplayName = `Connect(${ getDisplayName ( WrappedComponent ) } )` ;
@@ -67,6 +43,8 @@ export function connect<I, S>(main: Plan<I, S>, opts = { history: false }): (Wra
6743 updates : Stream < Update < S > >
6844 traveler : Traveler < S >
6945 subscription : Subscription < S >
46+ static contextTypes = CONTEXT_TYPE
47+ static displayName = connectDisplayName
7048 constructor ( props , context ) {
7149 super ( props , context ) ;
7250 let engine : Engine < I , S > = context [ REACT_MOST_ENGINE ]
@@ -77,7 +55,7 @@ export function connect<I, S>(main: Plan<I, S>, opts = { history: false }): (Wra
7755 } ) ;
7856 }
7957
80- let { actions, updates } = main ( context [ REACT_MOST_ENGINE ] . engine . intentStream , props )
58+ let { actions, updates } = main ( context [ REACT_MOST_ENGINE ] . intentStream , props )
8159 this . updates = props . updates ? updates . merge ( props . updates ) : updates
8260 this . actions = Object . assign ( { } , actions , props . actions ) ;
8361 let defaultKey = Object . keys ( WrappedComponent . defaultProps ) ;
@@ -131,15 +109,15 @@ export function connect<I, S>(main: Plan<I, S>, opts = { history: false }): (Wra
131109}
132110
133111export interface MostProps < T , S > {
134- engine ?: Engine < T , S >
112+ engine ?: new ( ) => Engine < T , S >
135113}
136114export interface MostEngine < I , H > {
137115 [ x : string ] : Engine < I , H >
138116}
139117export default class Most < I , H , S > extends React . PureComponent < MostProps < I , H > , S > {
140118 static childContextTypes = CONTEXT_TYPE
141119 getChildContext ( ) : MostEngine < I , H > {
142- let engine : Engine < I , H > = ( this . props && this . props . engine ) || new Engine < I , H > ( ) ;
120+ let engine : Engine < I , H > = ( this . props && this . props . engine && new this . props . engine ( ) ) || new Engine < I , H > ( ) ;
143121 /* istanbul ignore if */
144122 if ( process . env . NODE_ENV === 'debug' ) {
145123 inspect ( engine ) ;
@@ -153,10 +131,6 @@ export default class Most<I, H, S> extends React.PureComponent<MostProps<I, H>,
153131 }
154132}
155133
156- function observable ( obj ) {
157- return ! ! obj . subscribe ;
158- }
159-
160134/* istanbul ignore next */
161135function inspect ( engine ) {
162136 from ( engine . intentStream )
0 commit comments