UNPKG

@rxx/core

Version:

React MVI micro framework.

145 lines (144 loc) 6.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var react_1 = tslib_1.__importDefault(require("react")); var prop_types_1 = tslib_1.__importDefault(require("prop-types")); var rxjs_1 = require("rxjs"); var utils_1 = require("../utils"); var provisioning_1 = require("../provisioning"); var operators_1 = require("rxjs/operators"); var Provider = (function (_super) { tslib_1.__extends(Provider, _super); function Provider(p, c) { var _this = _super.call(this, p, c) || this; _this.provisioning = new provisioning_1.Provisioning(_this.props.name || "Anonymouse#" + utils_1.IDGenerator.genNextId(), _this.context, _this.props.intent ? utils_1.isObject(_this.props.intent) ? _this.props.intent : { intent: _this.props.intent } : {}, _this.props.store ? Array.isArray(_this.props.store) ? _this.props.store : [_this.props.store] : [], _this.props.app, _this.props.service, _this.props.stateHandlers || {}); var _a = _this, provisioning = _a.provisioning, context = _a.context, props = _a.props; _this.ProviderComponent = (function (_super) { tslib_1.__extends(ProviderComponent, _super); function ProviderComponent() { return _super !== null && _super.apply(this, arguments) || this; } ProviderComponent.prototype.render = function () { return this.props.children; }; ProviderComponent.prototype.getChildContext = function () { return { provisioning: provisioning, intent: provisioning.getIntentHandler(), state: provisioning.getState(), parent: context, __intent: provisioning.getIntent(), __subject: provisioning.getSubject(), }; }; Object.defineProperty(ProviderComponent, "childContextTypes", { get: function () { return Provider.contextTypes; }, enumerable: true, configurable: true }); return ProviderComponent; }(react_1.default.Component)); _this.state = { prepared: false }; return _this; } Provider.prototype.render = function () { return this.state.prepared ? (react_1.default.createElement(this.ProviderComponent, null, react_1.default.cloneElement(this.props.children))) : null; }; Provider.prototype.componentDidMount = function () { this.provisioning.prepare(); this.setState({ prepared: true }); }; Provider.prototype.componentWillUnmount = function () { this.provisioning.dispose(!this.props.useCache); this.setState({ prepared: false }); }; Provider.contextTypes = { provisioning: prop_types_1.default.any, intent: prop_types_1.default.any, state: prop_types_1.default.any, parent: prop_types_1.default.any, __subject: prop_types_1.default.any, __intent: prop_types_1.default.any, }; return Provider; }(react_1.default.Component)); exports.Provider = Provider; function directConnect(Component) { return (function (_super) { tslib_1.__extends(class_1, _super); function class_1() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.subject = new rxjs_1.Subject(); _this.subscriptions = new rxjs_1.Subscription(); return _this; } class_1.prototype.render = function () { var _this = this; return (react_1.default.createElement(Provider, { store: [this.store], intent: (function () { function class_2() { } return class_2; }()), app: this.props.app ? this.props.app : undefined, stateHandlers: this.props.stateHandlers }, react_1.default.createElement(Component, tslib_1.__assign({}, this.props.props, { ref: function (c) { return c && (_this.reference = c); } })))); }; class_1.prototype.componentWillMount = function () { var that = this; var _a = this, props = _a.props, subject = _a.subject, subscriptions = _a.subscriptions; this.store = (function () { function class_3(intent, services, subject) { this.intent = intent; this.services = services; this.subject = subject; } class_3.prototype.initialize = function () { var convertedStates = {}; var _loop_1 = function (key) { var v = props.state[key]; convertedStates[key] = that.subject.pipe(operators_1.map(function (v) { return v[key]; }), operators_1.startWith(v)); }; for (var key in props.state) { _loop_1(key); } var handlersArray = that.props.handlers || []; that.subscriptions.add(this.subject.subscribe(function (args) { handlersArray.forEach(function (handlers) { if (handlers[args.type]) { handlers[args.type](args); } }); })); return { view: convertedStates, }; }; return class_3; }()); }; Object.defineProperty(class_1.prototype, "component", { get: function () { return this.reference; }, enumerable: true, configurable: true }); class_1.prototype.componentWillReceiveProps = function (nextProps) { this.subject.next(nextProps.state); }; class_1.prototype.componentWillUnmount = function () { this.subscriptions.unsubscribe(); this.subscriptions = new rxjs_1.Subscription(); }; return class_1; }(react_1.default.Component)); } exports.directConnect = directConnect;