undux
Version:
Dead simple state management for React
91 lines • 4.1 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.createConnectedStore = void 0;
var React = require("react");
var __1 = require("..");
var utils_1 = require("../utils");
function createConnectedStore(initialState, 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 state = props.initialState || initialState;
_this.storeDefinition = (0, __1.createStore)(state);
// Apply effects?
var fx = props.effects || effects;
if (fx) {
fx(_this.storeDefinition);
}
_this.state = {
storeSnapshot: _this.storeDefinition.getCurrentSnapshot(),
};
_this.subscription = _this.storeDefinition.onAll().subscribe(function () {
return _this.setState({
storeSnapshot: _this.storeDefinition.getCurrentSnapshot(),
});
});
return _this;
}
Container.prototype.componentWillUnmount = function () {
this.subscription.unsubscribe();
this.storeDefinition.storeSnapshot = null;
this.storeDefinition = null;
};
Container.prototype.render = function () {
return (React.createElement(Context.Provider, { value: this.state.storeSnapshot }, this.props.children));
};
return Container;
}(React.Component));
var Consumer = function (props) { return (React.createElement(Context.Consumer, null, function (store) {
if (!isInitialized(store)) {
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 createConnectedStore()."));
}
return props.children(store);
})); };
function withStore(Component) {
var displayName = (0, utils_1.getDisplayName)(Component);
var f = function (props) { return (React.createElement(Consumer, { displayName: displayName }, function (storeSnapshot) { return (React.createElement(Component, __assign({ store: storeSnapshot }, props))); })); };
f.displayName = "withStore(".concat(displayName, ")");
return f;
}
return {
Container: Container,
useStore: function () {
return React.useContext(Context);
},
withStore: withStore,
};
}
exports.createConnectedStore = createConnectedStore;
function isInitialized(store) {
return !('__MISSING_PROVIDER__' in store);
}
//# sourceMappingURL=createConnectedStore.js.map