Skip to content

Commit 3dff8b4

Browse files
committed
extract interface to seperated file
1 parent bfbd637 commit 3dff8b4

File tree

6 files changed

+76
-70
lines changed

6 files changed

+76
-70
lines changed

lib/__tests__/react-most-test.jsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ CounterView.defaultProps = {count: 0, overwritedProps: 'inner'}
2828

2929
const counterWrapper = connect(intent$=>{
3030
return {
31-
sink$: intent$.map(intent=>{
31+
updates: intent$.map(intent=>{
3232
switch(intent.type) {
3333
case 'inc': return state=>({count:state.count+1})
3434
case 'dec':
@@ -43,18 +43,20 @@ const counterWrapper = connect(intent$=>{
4343
return state=>state
4444
}
4545
}),
46-
inc: ()=>({type:'inc'}),
47-
dec: ()=>({type:'dec'}),
48-
changeWrapperProps: (value)=>({type:'changeWrapperProps', value}),
49-
changeDefaultProps: (value)=>({type:'changeDefaultProps', value}),
46+
actions:{
47+
inc: ()=>({type:'inc'}),
48+
dec: ()=>({type:'dec'}),
49+
changeWrapperProps: (value)=>({type:'changeWrapperProps', value}),
50+
changeDefaultProps: (value)=>({type:'changeDefaultProps', value}),
51+
}
5052
}
5153
})
5254

5355
const Counter = counterWrapper(CounterView)
5456

5557
describe('react-most', () => {
5658
describe('actions', ()=>{
57-
it('add intent to intent$ and go through sink$', ()=> {
59+
it.only('add intent to intent$ and go through sink$', ()=> {
5860
let counterWrapper = TestUtils.renderIntoDocument(
5961
<Most engine={Engine}>
6062
<Counter history={true} />
@@ -64,6 +66,7 @@ describe('react-most', () => {
6466
counter.actions.inc()
6567
counter.actions.inc()
6668
counter.actions.inc()
69+
console.log(stateHistoryOf(counter));
6770
expect(stateHistoryOf(counter)[2].count).toBe(3)
6871
})
6972

lib/engine/most.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
import { from, of, mergeArray, Stream, never, Subscription } from 'most'
22
import { async as subject, AsyncSubject } from 'most-subject'
3-
4-
export interface EngineSubject<T> extends AsyncSubject<T> {
5-
send(x: T): this
6-
}
7-
8-
export interface Update<S> {
9-
(current: S): S
10-
}
11-
3+
import { EngineSubject, Update } from '../interfaces'
124
export class Engine<T, S> {
135
intentStream: EngineSubject<T>
146
historyStream: EngineSubject<S>

lib/history.ts

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,31 @@
11
import { from, Stream } from 'most'
2-
import { EngineSubject } from './engine/most'
3-
2+
import { Stamp, EngineSubject } from './interfaces'
43
export class Traveler<S> {
54
cursor: number
65
path: EngineSubject<(n: number) => number>
76
history: Stream<Stamp<S>[]>
7+
travel: Stream<S>
88
constructor(history: Stream<Stamp<S>[]>, path: EngineSubject<(n: number) => number>) {
99
this.history = history
1010
this.path = path
11+
this.travel = from(this.path)
12+
.sample((offset: (n: number) => number, states: Stamp<S>[]) => {
13+
let cursor = offset(states.length + this.cursor)
14+
if (cursor < states.length && cursor >= 0) {
15+
this.cursor = offset(this.cursor)
16+
return states[cursor].value;
17+
}
18+
}, this.path, this.history)
19+
.filter(x => !!x)
1120
}
1221
forward() {
1322
this.path.send(x => x + 1)
1423
}
1524
backward() {
1625
this.path.send(x => x - 1)
1726
}
18-
travel = from(this.path)
19-
.sample((offset: (n: number) => number, states: Stamp<S>[]) => {
20-
let cursor = offset(states.length + this.cursor)
21-
if (cursor < states.length && cursor >= 0) {
22-
this.cursor = offset(this.cursor)
23-
return states[cursor].value;
24-
}
25-
}, this.path, this.history)
26-
.filter(x => !!x)
2727

28-
}
29-
export interface History<S> {
30-
path: EngineSubject<(n: number) => number>
31-
history: Stream<S>
32-
}
3328

34-
export interface Stamp<S> {
35-
value: S
36-
time: number
3729
}
3830
export default function initHistory<S>(engineHistory: EngineSubject<S>, engineTravel: EngineSubject<(n: number) => number>): Traveler<S> {
3931
let history = from(engineHistory)

lib/interfaces.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { Stream } from 'most'
2+
import * as React from 'react'
3+
import { AsyncSubject } from 'most-subject'
4+
5+
export interface Actions<T> {
6+
[propName: string]: (...v: any[]) => T
7+
}
8+
9+
export interface Plan<I, S> {
10+
(intent: EngineSubject<I>, props?: {}): Process<I, S>
11+
}
12+
export interface Update<S> {
13+
(current: S): S
14+
}
15+
export interface Process<I, S> {
16+
actions: Actions<I>,
17+
updates: Stream<Update<S>>
18+
}
19+
20+
export interface ConnectProps<I> {
21+
actions?: Actions<I>
22+
}
23+
24+
export class Connect<I, S> extends React.PureComponent<ConnectProps<I>, S> {
25+
actions: Actions<I>
26+
updates: Stream<Update<S>>
27+
}
28+
29+
export interface ConnectClass<I, S> {
30+
new (props?: ConnectProps<I>, context?: any): Connect<I, S>;
31+
}
32+
33+
export interface History<S> {
34+
path: EngineSubject<(n: number) => number>
35+
history: Stream<S>
36+
}
37+
38+
export interface Stamp<S> {
39+
value: S
40+
time: number
41+
}
42+
43+
export interface EngineSubject<T> extends AsyncSubject<T> {
44+
send(x: T): this
45+
}

lib/react-most.ts

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,17 @@
11
import * as React from 'react';
22
import { PropTypes } from 'prop-types';
33
import initHistory, { Traveler } from './history';
4+
import { Plan, Actions, Connect, ConnectProps, EngineSubject, Update, ConnectClass } from './interfaces'
45
import { 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
79
export const REACT_MOST_ENGINE = '@@reactive-react/react-most.engine';
8-
10+
const h = React.createElement;
911
const 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-
}
3915
export 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

133111
export interface MostProps<T, S> {
134-
engine?: Engine<T, S>
112+
engine?: new () => Engine<T, S>
135113
}
136114
export interface MostEngine<I, H> {
137115
[x: string]: Engine<I, H>
138116
}
139117
export 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 */
161135
function inspect(engine) {
162136
from(engine.intentStream)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"type": "git",
77
"url": "git+https://github.com/jcouyang/react-most.git"
88
},
9-
"main": "react-most.js",
9+
"main": "dist/react-most.js",
1010
"directories": {
1111
"doc": "./docs",
1212
"lib": "./lib"

0 commit comments

Comments
 (0)