Skip to content

Commit c35f6f8

Browse files
committed
format code
1 parent bd3f2a6 commit c35f6f8

File tree

1 file changed

+116
-80
lines changed

1 file changed

+116
-80
lines changed

lib/react-most.js

Lines changed: 116 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,152 +1,188 @@
1-
import React from 'react'
2-
import initHistory from './history'
3-
import {from} from 'most'
4-
import mostEngine from './engine/most'
5-
import mergeAll from 'ramda/src/mergeAll'
6-
import pick from 'ramda/src/pick'
7-
import keys from 'ramda/src/keys'
1+
import React from 'react';
2+
import initHistory from './history';
3+
import { from } from 'most';
4+
import mostEngine from './engine/most';
5+
import mergeAll from 'ramda/src/mergeAll';
6+
import pick from 'ramda/src/pick';
7+
import keys from 'ramda/src/keys';
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
9-
export const INTENT_STREAM = "@@reactive-react/react-most.intentStream";
10-
export const HISTORY_STREAM = "@@reactive-react/react-most.historyStream";
11-
const MERGE_OBSERVE = "@@reactive-react/react-most.mergeObserve";
9+
export const INTENT_STREAM = '@@reactive-react/react-most.intentStream';
10+
export const HISTORY_STREAM = '@@reactive-react/react-most.historyStream';
11+
const MERGE_OBSERVE = '@@reactive-react/react-most.mergeObserve';
1212

1313
const CONTEXT_TYPE = {
1414
[INTENT_STREAM]: React.PropTypes.object,
1515
[HISTORY_STREAM]: React.PropTypes.object,
1616
[MERGE_OBSERVE]: React.PropTypes.func,
17-
}
17+
};
1818

19-
export function connect(main, opts={}) {
20-
return function(WrappedComponent){
21-
let connectDisplayName = `Connect(${getDisplayName(WrappedComponent)})`
19+
export function connect(main, opts = {}) {
20+
return function(WrappedComponent) {
21+
let connectDisplayName = `Connect(${getDisplayName(WrappedComponent)})`;
2222
if (WrappedComponent.contextTypes === CONTEXT_TYPE) {
2323
class Connect extends React.PureComponent {
2424
constructor(props, context) {
2525
super(props, context);
26-
let [actions, sink$] = actionsAndSinks(main(context[INTENT_STREAM],props), this)
27-
this.sink$ = sink$.concat(props.sink$||[])
28-
this.actions = mergeAll([actions, props.actions])
26+
let [actions, sink$] = actionsAndSinks(
27+
main(context[INTENT_STREAM], props),
28+
this
29+
);
30+
this.sink$ = sink$.concat(props.sink$ || []);
31+
this.actions = mergeAll([actions, props.actions]);
2932
}
30-
render(){
31-
return <WrappedComponent {...this.props} {...opts} sink$={this.sink$} actions={this.actions}/>
33+
render() {
34+
return (
35+
<WrappedComponent
36+
{...this.props}
37+
{...opts}
38+
sink$={this.sink$}
39+
actions={this.actions}
40+
/>
41+
);
3242
}
3343
}
3444
Connect.contextTypes = CONTEXT_TYPE;
3545
Connect.displayName = connectDisplayName;
3646
return Connect;
37-
}else{
47+
} else {
3848
class Connect extends React.PureComponent {
3949
constructor(props, context) {
4050
super(props, context);
41-
if(opts.history || props.history){
42-
opts.history = initHistory(context[HISTORY_STREAM])
43-
opts.history.travel.forEach(state=>{
44-
return this.setState(state)
45-
})
51+
if (opts.history || props.history) {
52+
opts.history = initHistory(context[HISTORY_STREAM]);
53+
opts.history.travel.forEach(state => {
54+
return this.setState(state);
55+
});
4656
}
4757

48-
let [actions, sink$] = actionsAndSinks(main(context[INTENT_STREAM],props), this)
49-
this.sink$ = sink$.concat(props.sink$||[])
50-
this.actions = mergeAll([actions, props.actions])
51-
let defaultKey = keys(WrappedComponent.defaultProps)
52-
this.state = mergeAll([WrappedComponent.defaultProps, pick(defaultKey, props)])
58+
let [actions, sink$] = actionsAndSinks(
59+
main(context[INTENT_STREAM], props),
60+
this
61+
);
62+
this.sink$ = sink$.concat(props.sink$ || []);
63+
this.actions = mergeAll([actions, props.actions]);
64+
let defaultKey = keys(WrappedComponent.defaultProps);
65+
this.state = mergeAll([
66+
WrappedComponent.defaultProps,
67+
pick(defaultKey, props),
68+
]);
5369
}
54-
componentWillReceiveProps(nextProps){
55-
this.setState(state=>pick(keys(state), nextProps))
70+
componentWillReceiveProps(nextProps) {
71+
this.setState(state => pick(keys(state), nextProps));
5672
}
57-
componentDidMount(){
58-
this.subscriptions = this.context[MERGE_OBSERVE](this.sink$, (action)=>{
59-
if(action instanceof Function) {
60-
this.setState((prevState, props)=>{
61-
let newState = action.call(this, prevState,props);
62-
if(opts.history && newState != prevState){
63-
opts.history.cursor = -1;
64-
this.context[HISTORY_STREAM].send(prevState);
65-
}
66-
return newState;
67-
});
68-
} else {
69-
/* istanbul ignore next */
70-
console.warn('action', action,'need to be a Function which map from current state to new state');
73+
componentDidMount() {
74+
this.subscriptions = this.context[MERGE_OBSERVE](
75+
this.sink$,
76+
action => {
77+
if (action instanceof Function) {
78+
this.setState((prevState, props) => {
79+
let newState = action.call(this, prevState, props);
80+
if (opts.history && newState != prevState) {
81+
opts.history.cursor = -1;
82+
this.context[HISTORY_STREAM].send(prevState);
83+
}
84+
return newState;
85+
});
86+
} else {
87+
/* istanbul ignore next */
88+
console.warn(
89+
'action',
90+
action,
91+
'need to be a Function which map from current state to new state'
92+
);
93+
}
7194
}
72-
});
95+
);
7396
}
74-
componentWillUnmount(){
75-
this.subscriptions.unsubscribe()
97+
componentWillUnmount() {
98+
this.subscriptions.unsubscribe();
7699
}
77100
render() {
78-
return <WrappedComponent {...this.props} {...this.state} {...opts} actions={this.actions} />
101+
return (
102+
<WrappedComponent
103+
{...this.props}
104+
{...this.state}
105+
{...opts}
106+
actions={this.actions}
107+
/>
108+
);
79109
}
80110
}
81111
Connect.contextTypes = CONTEXT_TYPE;
82112
Connect.displayName = connectDisplayName;
83113
return Connect;
84114
}
85-
}
115+
};
86116
}
87117

88118
let Most = React.createClass({
89119
childContextTypes: CONTEXT_TYPE,
90-
getChildContext(){
91-
let engineClass = this.props && this.props.engine || mostEngine
120+
getChildContext() {
121+
let engineClass = (this.props && this.props.engine) || mostEngine;
92122
let engine = engineClass();
93123
/* istanbul ignore if */
94-
if(process.env.NODE_ENV==='debug') {
95-
inspect(engine)
124+
if (process.env.NODE_ENV === 'debug') {
125+
inspect(engine);
96126
}
97127
return {
98128
[INTENT_STREAM]: engine.intentStream,
99129
[MERGE_OBSERVE]: engine.mergeObserve,
100130
[HISTORY_STREAM]: engine.historyStream,
101-
}
131+
};
102132
},
103133
render() {
104134
return React.Children.only(this.props.children);
105-
}
135+
},
106136
});
107137

108138
export default Most;
109139

110-
function observable(obj){
111-
return !!obj.subscribe
140+
function observable(obj) {
141+
return !!obj.subscribe;
112142
}
113143

114144
/* istanbul ignore next */
115145
function inspect(engine) {
116-
from(engine.intentStream).timestamp()
117-
.observe(stamp=>console.log(`[${new Date(stamp.time).toJSON()}][INTENT]:}`, stamp.value));
118-
from(engine.historyStream).timestamp()
119-
.observe(stamp=>console.log(`[${new Date(stamp.time).toJSON()}][STATE]:}`, stamp.value));
146+
from(engine.intentStream)
147+
.timestamp()
148+
.observe(stamp =>
149+
console.log(`[${new Date(stamp.time).toJSON()}][INTENT]:}`, stamp.value)
150+
);
151+
from(engine.historyStream)
152+
.timestamp()
153+
.observe(stamp =>
154+
console.log(`[${new Date(stamp.time).toJSON()}][STATE]:}`, stamp.value)
155+
);
120156
}
121157

122-
function actionsAndSinks(sinks, self){
158+
function actionsAndSinks(sinks, self) {
123159
let _sinks = [];
124160
let _actions = {
125-
fromEvent(e, f=x=>x){
161+
fromEvent(e, f = x => x) {
126162
return self.context[INTENT_STREAM].send(f(e));
127163
},
128-
fromPromise(p){
129-
return p.then(x=>self.context[INTENT_STREAM].send(x));
130-
}
164+
fromPromise(p) {
165+
return p.then(x => self.context[INTENT_STREAM].send(x));
166+
},
131167
};
132-
for(let name in sinks){
133-
let value = sinks[name]
134-
if(observable(value)){
168+
for (let name in sinks) {
169+
let value = sinks[name];
170+
if (observable(value)) {
135171
_sinks.push(value);
136-
} else if(value instanceof Function){
137-
_actions[name] = (...args)=>{
172+
} else if (value instanceof Function) {
173+
_actions[name] = (...args) => {
138174
return self.context[INTENT_STREAM].send(value.apply(self, args));
139-
}
140-
} else if(name === 'actions') {
141-
for(let a in value)
142-
_actions[a] = (...args)=>{
175+
};
176+
} else if (name === 'actions') {
177+
for (let a in value)
178+
_actions[a] = (...args) => {
143179
return self.context[INTENT_STREAM].send(value[a].apply(self, args));
144-
}
180+
};
145181
}
146182
}
147-
return [_actions, _sinks]
183+
return [_actions, _sinks];
148184
}
149185

150186
function getDisplayName(WrappedComponent) {
151-
return WrappedComponent.displayName || WrappedComponent.name || 'Component'
187+
return WrappedComponent.displayName || WrappedComponent.name || 'Component';
152188
}

0 commit comments

Comments
 (0)