Skip to content

Commit 4196911

Browse files
committed
mv typescript source file to src and compile to lib
1 parent dc8fb08 commit 4196911

File tree

13 files changed

+276
-10
lines changed

13 files changed

+276
-10
lines changed

lib/engine/most.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
var most_1 = require("most");
4+
var most_subject_1 = require("most-subject");
5+
var Engine = (function () {
6+
function Engine() {
7+
this.intentStream = most_subject_1.async();
8+
this.intentStream.send = this.intentStream.next.bind(this.intentStream);
9+
this.historyStream = most_subject_1.async();
10+
this.historyStream.send = this.historyStream.next.bind(this.historyStream);
11+
this.travelStream = most_subject_1.async();
12+
this.travelStream.send = this.travelStream.next.bind(this.historyStream);
13+
}
14+
Engine.prototype.observe = function (actionsSinks, f) {
15+
var subscriptions = actionsSinks
16+
.recoverWith(function (e) {
17+
console.error('There is Error in your reducer:', e, e.stack);
18+
return most_1.of(function (x) { return x; });
19+
})
20+
.subscribe({
21+
next: f,
22+
error: function (e) { return console.error('Something is Wrong:', e, e.stack); },
23+
complete: f
24+
});
25+
return subscriptions;
26+
};
27+
return Engine;
28+
}());
29+
exports.Engine = Engine;

lib/history.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
var most_1 = require("most");
4+
var Traveler = (function () {
5+
function Traveler(history, path) {
6+
var _this = this;
7+
this.history = history;
8+
this.path = path;
9+
this.travel = most_1.from(this.path)
10+
.sample(function (offset, states) {
11+
var cursor = offset(states.length + _this.cursor);
12+
if (cursor < states.length && cursor >= 0) {
13+
_this.cursor = offset(_this.cursor);
14+
return states[cursor].value;
15+
}
16+
}, this.path, this.history)
17+
.filter(function (x) { return !!x; });
18+
}
19+
Traveler.prototype.forward = function () {
20+
this.path.send(function (x) { return x + 1; });
21+
};
22+
Traveler.prototype.backward = function () {
23+
this.path.send(function (x) { return x - 1; });
24+
};
25+
return Traveler;
26+
}());
27+
exports.Traveler = Traveler;
28+
function initHistory(engineHistory, engineTravel) {
29+
var history = most_1.from(engineHistory)
30+
.timestamp()
31+
.scan(function (acc, state) {
32+
acc.push(state);
33+
return acc;
34+
}, [])
35+
.multicast();
36+
return new Traveler(history, engineTravel);
37+
}
38+
exports.default = initHistory;

lib/interfaces.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"use strict";
2+
var __extends = (this && this.__extends) || (function () {
3+
var extendStatics = Object.setPrototypeOf ||
4+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5+
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
6+
return function (d, b) {
7+
extendStatics(d, b);
8+
function __() { this.constructor = d; }
9+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
10+
};
11+
})();
12+
Object.defineProperty(exports, "__esModule", { value: true });
13+
var React = require("react");
14+
var Connect = (function (_super) {
15+
__extends(Connect, _super);
16+
function Connect() {
17+
return _super !== null && _super.apply(this, arguments) || this;
18+
}
19+
return Connect;
20+
}(React.PureComponent));
21+
exports.Connect = Connect;

lib/react-most.js

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
"use strict";
2+
var __extends = (this && this.__extends) || (function () {
3+
var extendStatics = Object.setPrototypeOf ||
4+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5+
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
6+
return function (d, b) {
7+
extendStatics(d, b);
8+
function __() { this.constructor = d; }
9+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
10+
};
11+
})();
12+
Object.defineProperty(exports, "__esModule", { value: true });
13+
var React = require("react");
14+
var prop_types_1 = require("prop-types");
15+
var history_1 = require("./history");
16+
var interfaces_1 = require("./interfaces");
17+
var most_1 = require("most");
18+
var most_2 = require("./engine/most");
19+
// 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
20+
exports.REACT_MOST_ENGINE = '@@reactive-react/react-most.engine';
21+
var h = React.createElement;
22+
var CONTEXT_TYPE = (_a = {},
23+
_a[exports.REACT_MOST_ENGINE] = prop_types_1.PropTypes.object,
24+
_a);
25+
function isConnectClass(ComponentClass) {
26+
return ComponentClass.contextTypes == CONTEXT_TYPE;
27+
}
28+
function connect(main, opts) {
29+
if (opts === void 0) { opts = { history: false }; }
30+
return function (WrappedComponent) {
31+
var connectDisplayName = "Connect(" + getDisplayName(WrappedComponent) + ")";
32+
if (isConnectClass(WrappedComponent)) {
33+
return _a = (function (_super) {
34+
__extends(ConnectNode, _super);
35+
function ConnectNode(props, context) {
36+
var _this = _super.call(this, props, context) || this;
37+
var _a = main(context[exports.REACT_MOST_ENGINE].intentStream, props), actions = _a.actions, update$ = _a.update$;
38+
_this.machine = {
39+
update$: _this.machine.update$.merge(update$),
40+
actions: Object.assign({}, bindActions(actions, context[exports.REACT_MOST_ENGINE].intentStream, _this), _this.machine.actions)
41+
};
42+
return _this;
43+
}
44+
return ConnectNode;
45+
}(WrappedComponent)),
46+
_a.contextTypes = CONTEXT_TYPE,
47+
_a.displayName = connectDisplayName,
48+
_a;
49+
}
50+
else {
51+
return _b = (function (_super) {
52+
__extends(ConnectLeaf, _super);
53+
function ConnectLeaf(props, context) {
54+
var _this = _super.call(this, props, context) || this;
55+
var engine = context[exports.REACT_MOST_ENGINE];
56+
if (opts.history || props.history) {
57+
_this.traveler = history_1.default(engine.historyStream, engine.travelStream);
58+
_this.traveler.travel.forEach(function (state) {
59+
return _this.setState(state);
60+
});
61+
}
62+
var _a = main(engine.intentStream, props), actions = _a.actions, update$ = _a.update$;
63+
_this.machine = {
64+
actions: bindActions(actions, engine.intentStream, _this),
65+
update$: update$
66+
};
67+
var defaultKey = Object.keys(WrappedComponent.defaultProps);
68+
_this.state = Object.assign({}, WrappedComponent.defaultProps, pick(defaultKey, props));
69+
return _this;
70+
}
71+
ConnectLeaf.prototype.componentWillReceiveProps = function (nextProps) {
72+
this.setState(function (state) { return pick(Object.keys(state), nextProps); });
73+
};
74+
ConnectLeaf.prototype.componentDidMount = function () {
75+
var _this = this;
76+
this.subscription = this.context[exports.REACT_MOST_ENGINE].observe(this.machine.update$, function (action) {
77+
if (action instanceof Function) {
78+
_this.setState(function (prevState, props) {
79+
var newState = action.call(_this, prevState, props);
80+
if ((opts.history || props.history) && newState != prevState) {
81+
_this.traveler.cursor = -1;
82+
_this.context[exports.REACT_MOST_ENGINE].historyStream.send(prevState);
83+
}
84+
return newState;
85+
});
86+
}
87+
else {
88+
/* istanbul ignore next */
89+
console.warn('action', action, 'need to be a Function which map from current state to new state');
90+
}
91+
});
92+
};
93+
ConnectLeaf.prototype.componentWillUnmount = function () {
94+
this.subscription.unsubscribe();
95+
};
96+
ConnectLeaf.prototype.render = function () {
97+
return h(WrappedComponent, Object.assign({}, opts, this.props, this.state, {
98+
actions: this.machine.actions,
99+
traveler: this.traveler
100+
}));
101+
};
102+
return ConnectLeaf;
103+
}(interfaces_1.Connect)),
104+
_b.contextTypes = CONTEXT_TYPE,
105+
_b.displayName = connectDisplayName,
106+
_b;
107+
}
108+
var _a, _b;
109+
};
110+
}
111+
exports.connect = connect;
112+
var Most = (function (_super) {
113+
__extends(Most, _super);
114+
function Most() {
115+
return _super !== null && _super.apply(this, arguments) || this;
116+
}
117+
Most.prototype.getChildContext = function () {
118+
var engine = (this.props && this.props.engine && new this.props.engine()) || new most_2.Engine();
119+
/* istanbul ignore if */
120+
if (process.env.NODE_ENV === 'debug') {
121+
inspect(engine);
122+
}
123+
return _a = {},
124+
_a[exports.REACT_MOST_ENGINE] = engine,
125+
_a;
126+
var _a;
127+
};
128+
Most.prototype.render = function () {
129+
return React.Children.only(this.props.children);
130+
};
131+
return Most;
132+
}(React.PureComponent));
133+
Most.childContextTypes = CONTEXT_TYPE;
134+
exports.default = Most;
135+
/* istanbul ignore next */
136+
function inspect(engine) {
137+
most_1.from(engine.intentStream)
138+
.timestamp()
139+
.observe(function (stamp) {
140+
return console.log("[" + new Date(stamp.time).toJSON() + "][INTENT]:}", stamp.value);
141+
});
142+
most_1.from(engine.historyStream)
143+
.timestamp()
144+
.observe(function (stamp) {
145+
return console.log("[" + new Date(stamp.time).toJSON() + "][STATE]:}", stamp.value);
146+
});
147+
}
148+
function bindActions(actions, intent$, self) {
149+
var _actions = {
150+
fromEvent: function (e, f) {
151+
if (f === void 0) { f = function (x) { return x; }; }
152+
return intent$.send(f(e));
153+
},
154+
fromPromise: function (p) {
155+
return p.then(function (x) { return intent$.send(x); });
156+
},
157+
};
158+
var _loop_1 = function (a) {
159+
_actions[a] = function () {
160+
var args = [];
161+
for (var _i = 0; _i < arguments.length; _i++) {
162+
args[_i] = arguments[_i];
163+
}
164+
return intent$.send(actions[a].apply(self, args));
165+
};
166+
};
167+
for (var a in actions) {
168+
_loop_1(a);
169+
}
170+
return _actions;
171+
}
172+
function pick(names, obj) {
173+
var result = {};
174+
for (var _i = 0, names_1 = names; _i < names_1.length; _i++) {
175+
var name_1 = names_1[_i];
176+
if (obj[name_1])
177+
result[name_1] = obj[name_1];
178+
}
179+
return result;
180+
}
181+
function getDisplayName(WrappedComponent) {
182+
return WrappedComponent.displayName || WrappedComponent.name || 'Component';
183+
}
184+
var _a;

package.json

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,10 @@
66
"type": "git",
77
"url": "git+https://github.com/jcouyang/react-most.git"
88
},
9-
"main": "dist/react-most.js",
9+
"main": "lib/react-most.js",
1010
"directories": {
11-
"doc": "./docs",
1211
"lib": "./lib"
1312
},
14-
"files": [
15-
"engine",
16-
"lib",
17-
"test-utils.js"
18-
],
1913
"scripts": {
2014
"license": "(cat LICENSE.txt; cat react-most.js) > react-most.licensed.js && mv react-most.licensed.js react-most.js",
2115
"build": "tsc -p .",
@@ -60,7 +54,7 @@
6054
"es6"
6155
],
6256
"roots": [
63-
"lib"
57+
"src"
6458
],
6559
"coverageDirectory": "./coverage/",
6660
"collectCoverage": true
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)