|
| 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; |
0 commit comments