UNPKG

darian

Version:

A Naive state container for JavaScript apps

132 lines (117 loc) 4.7 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var React = require('react'); var rx = require('rx'); var Store = /** @class */ (function () { function Store(reducer, action) { var _this = this; this.dispatch = function (action) { _this.state = _this.reducer(action); return _this.state; }; this.reducer = reducer; this.state = this.reducer(action); } return Store; }()); var createStore = function (reducer, action) { return new Store(reducer, action || { type: "init" }); }; var combineReducer = function (reduceTree) { return function (action) { var keys = Object.keys(reduceTree); var state = {}; keys.forEach(function (key) { state[key] = reduceTree[key](action); }); return state; }; }; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. See the Apache Version 2.0 License for specific language governing permissions and limitations under the License. ***************************************************************************** */ /* global Reflect, Promise */ var extendStatics = function(d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; function __extends(d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); } var __assign = function() { __assign = Object.assign || function __assign(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var bind = (function (bindLink) { return function (bindCfg) { return function (Target) { return /** @class */ (function (_super) { __extends(class_1, _super); function class_1(props) { var _this = _super.call(this, props) || this; _this.handleChange = function (e) { if (e.event === 'DATA_UPDATE') { _this.setState({ store: bindLink.store.state }); } }; _this.state = { store: bindLink.store.state }; return _this; } class_1.prototype.componentDidMount = function () { bindLink.stream.subscribe(this.handleChange); }; class_1.prototype.componentWillUnmount = function () { bindLink.stream.onCompleted(); }; class_1.prototype.render = function () { var _a; var _b = bindCfg.subjectPath, subjectPath = _b === void 0 ? 'subject' : _b; var _c = bindCfg.dispathPath, dispathPath = _c === void 0 ? 'dispatch' : _c; var store = bindLink.store, dispath = bindLink.dispath; var targetProps = __assign({}, this.props, (_a = {}, _a[subjectPath] = store, _a[dispathPath] = dispath, _a)); return React.createElement(Target, __assign({}, targetProps)); }; return class_1; }(React.Component)); }; }; }); var createStoreSubject = function (reducer, initAction) { var store = new Store(reducer, initAction || { type: "init" }); var stream = new rx.Subject(); var dispatch = function (action) { var nextState = store.dispatch(action); stream.onNext({ event: "DATA_UPDATE", }); return nextState; }; return { dispatch: dispatch, store: store, stream: stream, }; }; exports.createStore = createStore; exports.combineReducers = combineReducer; exports.bind = bind; exports.createStoreSubject = createStoreSubject;