undux
Version:
Dead simple state management for React
103 lines • 4.99 kB
JavaScript
;
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.createConnectedStoreAs = void 0;
var React = require("react");
var __1 = require("..");
var utils_1 = require("../utils");
function createConnectedStoreAs(initialStates, effects) {
var Context = React.createContext({ __MISSING_PROVIDER__: true });
var Container = /** @class */ (function (_super) {
__extends(Container, _super);
function Container(props) {
var _this = _super.call(this, props) || this;
// Create store definition from initial state
var states = props.initialStates || initialStates;
var stores = (0, utils_1.mapValues)(states, function (_) { return (0, __1.createStore)(_); });
// Apply effects?
var fx = props.effects || effects;
if (fx) {
fx(stores); // TODO
}
_this.state = {
storeDefinitions: stores,
// TODO
storeSnapshots: (0, utils_1.mapValues)(stores, function (_) { return _.getCurrentSnapshot(); }),
subscriptions: (0, utils_1.mapValues)(stores, function (_, k) {
return _.onAll().subscribe(function () {
return _this.setState(function (state) {
var _a;
return ({
storeSnapshots: Object.assign({}, state.storeSnapshots, (_a = {},
_a[k] = _.getCurrentSnapshot(),
_a)),
});
});
});
}),
};
return _this;
}
Container.prototype.componentWillUnmount = function () {
(0, utils_1.mapValues)(this.state.subscriptions, function (_) { return _.unsubscribe(); });
// Let the state get GC'd.
// TODO: Find a more elegant way to do this.
(0, utils_1.mapValues)(this.state.storeSnapshots, function (_) { return (_.state = null); });
(0, utils_1.mapValues)(this.state.storeSnapshots, function (_) { return (_.storeDefinition = null); });
(0, utils_1.mapValues)(this.state.storeDefinitions, function (_) { return (_.storeSnapshot = null); });
};
Container.prototype.render = function () {
return (React.createElement(Context.Provider, { value: this.state.storeSnapshots }, this.props.children));
};
return Container;
}(React.Component));
var Consumer = function (props) { return (React.createElement(Context.Consumer, null, function (stores) {
if (!isInitialized(stores)) {
throw Error("[Undux] Component \"".concat(props.displayName, "\" does not seem to be nested in an Undux <Container>. To fix this error, be sure to render the component in the <Container>...</Container> component that you got back from calling createConnectedStoreAs()."));
}
return props.children(stores);
})); };
function withStores(Component) {
var displayName = (0, utils_1.getDisplayName)(Component);
var f = function (props) { return (React.createElement(Consumer, { displayName: displayName }, function (stores) { return React.createElement(Component, __assign({}, stores, props)); })); };
f.displayName = "withStores(".concat(displayName, ")");
return f;
}
return {
Container: Container,
useStores: function () {
return React.useContext(Context);
},
withStores: withStores,
};
}
exports.createConnectedStoreAs = createConnectedStoreAs;
function isInitialized(store) {
return !('__MISSING_PROVIDER__' in store);
}
//# sourceMappingURL=createConnectedStoreAs.js.map