UNPKG

undux

Version:

Dead simple state management for React

83 lines 3.87 kB
"use strict"; var __extends = (this && this.__extends) || (function () { 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 (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(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); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.connectAs = void 0; var React = require("react"); var utils_1 = require("../utils"); /** * @deprecated Use `createConnectedStoreAs` instead. */ function connectAs(stores) { return function (Component) { var _a; return _a = /** @class */ (function (_super) { __extends(class_1, _super); function class_1() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.state = { stores: (0, utils_1.mapValues)(stores, function (_) { return _.getCurrentSnapshot(); }), subscriptions: (0, utils_1.keys)(stores).map(function (k) { return stores[k].onAll().subscribe(function (_b) { var previousValue = _b.previousValue, value = _b.value; if ((0, utils_1.equals)(previousValue, value)) { return false; } _this.setState(function (state) { var _b; return ({ stores: Object.assign({}, state.stores, (_b = {}, _b[k] = stores[k].getCurrentSnapshot(), _b)), }); }); }); }), }; return _this; } class_1.prototype.componentWillUnmount = function () { this.state.subscriptions.forEach(function (_) { return _.unsubscribe(); }); }; class_1.prototype.shouldComponentUpdate = function (props, state) { var _this = this; return ((0, utils_1.some)(state.stores, function (s, k) { return s !== _this.state.stores[k]; }) || Object.keys(props).some(function (_) { return props[_] !== _this.props[_]; })); }; class_1.prototype.render = function () { return React.createElement(Component, __assign({}, this.props, this.state.stores)); }; return class_1; }(React.Component)), _a.displayName = "withStore(".concat((0, utils_1.getDisplayName)(Component), ")"), _a; }; } exports.connectAs = connectAs; //# sourceMappingURL=connectAs.js.map