UNPKG

@rxx/core

Version:

React MVI micro framework.

94 lines (93 loc) 4.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var React = tslib_1.__importStar(require("react")); var PropTypes = tslib_1.__importStar(require("prop-types")); var utils_1 = require("../utils"); var identity = function (a) { return a; }; var DEFAULT = { mapStateToProps: undefined, mapIntentToProps: undefined, }; exports.CONTEXT_TYPES = { intent: PropTypes.any, state: PropTypes.any, unobservablifiedStateGetter: PropTypes.any, parent: PropTypes.any, }; function connect(args) { if (args === void 0) { args = DEFAULT; } var _a = args.mapStateToProps, mapStateToProps = _a === void 0 ? (function (a, b, c) { return (tslib_1.__assign({}, a, b)); }) : _a, _b = args.mapIntentToProps, mapIntentToProps = _b === void 0 ? (function () { return ({}); }) : _b; return function (C) { var _a, _b; C.contextTypes = exports.CONTEXT_TYPES; var baseName = "" + (C.displayName || C.name || 'AnonymousComponent'); var rootDisplayName = baseName + "$EnhancedContextConnectorComponent"; var internalContainerName = baseName + "$EnhancedRenderingContainer"; var ContainerComponent = (_a = (function (_super) { tslib_1.__extends(class_1, _super); function class_1(p, c) { var _this = _super.call(this, p, c) || this; _this.mappedProps = utils_1.mergeDeep(_this.mapStateToProps(_this.props), _this.mapIntentToProps(_this.props)); return _this; } class_1.prototype.render = function () { return React.createElement(C, tslib_1.__assign({}, this.mappedProps)); }; class_1.prototype.componentWillReceiveProps = function (nextProps) { this.mappedProps = utils_1.mergeDeep(this.mapStateToProps(nextProps), this.mapIntentToProps(nextProps)); }; class_1.prototype.getChildContext = function () { return { intent: this.context.intent, state: this.context.state, parent: this.context.parent, }; }; class_1.prototype.mapIntentToProps = function (props) { return mapIntentToProps(this.context.intent, props.state, props.props, this); }; class_1.prototype.mapStateToProps = function (props) { return mapStateToProps(props.state, props.props, this); }; Object.defineProperty(class_1, "childContextTypes", { get: function () { return exports.CONTEXT_TYPES; }, enumerable: true, configurable: true }); return class_1; }(React.Component)), _a.displayName = internalContainerName, _a.contextTypes = exports.CONTEXT_TYPES, _a); return _b = (function (_super) { tslib_1.__extends(class_2, _super); function class_2(p) { var _this = _super.call(this, p) || this; _this.state = { state: null, }; return _this; } class_2.prototype.render = function () { return (React.createElement(ContainerComponent, { state: this.state.state, props: this.props })); }; class_2.prototype.componentWillMount = function () { var _this = this; this.unsubscribe = this.context.provisioning.subscribe(function (state) { _this.setState({ state: state.view }); }, true); }; class_2.prototype.componentWillUnmount = function () { this.unsubscribe(); }; return class_2; }(React.Component)), _b.displayName = rootDisplayName, _b.contextTypes = { provisioning: PropTypes.any }, _b; }; } exports.connect = connect;