UNPKG

4.93 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = function (d, b) {
4 extendStatics = Object.setPrototypeOf ||
5 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7 return extendStatics(d, b);
8 };
9 return function (d, b) {
10 extendStatics(d, b);
11 function __() { this.constructor = d; }
12 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13 };
14})();
15Object.defineProperty(exports, "__esModule", { value: true });
16exports.useStore = exports.WithStore = exports.StoreProjection = exports.StoreSlice = exports.StoreConsumer = exports.StoreProvider = void 0;
17var React = require("react");
18var context = React.createContext(undefined);
19var Provider = context.Provider, Consumer = context.Consumer;
20var StoreProvider = /** @class */ (function (_super) {
21 __extends(StoreProvider, _super);
22 function StoreProvider() {
23 return _super !== null && _super.apply(this, arguments) || this;
24 }
25 StoreProvider.prototype.render = function () {
26 return React.createElement(Provider, { value: this.props.store }, this.props.children);
27 };
28 return StoreProvider;
29}(React.Component));
30exports.StoreProvider = StoreProvider;
31exports.StoreConsumer = Consumer;
32var StoreSlice = /** @class */ (function (_super) {
33 __extends(StoreSlice, _super);
34 function StoreSlice() {
35 return _super !== null && _super.apply(this, arguments) || this;
36 }
37 StoreSlice.prototype.componentWillUnmount = function () {
38 this.slice.destroy();
39 };
40 StoreSlice.prototype.render = function () {
41 var _this = this;
42 return (React.createElement(Consumer, null, function (store) {
43 if (!store)
44 throw new Error("StoreSlice used outside of a Store context. Did forget to add a <StoreProvider>?");
45 // we ignore this else due to a limitation in enzyme - we can't trigger a
46 // forceUpdate here to test the else branch;
47 /* istanbul ignore else */
48 if (_this.slice === undefined) {
49 _this.slice = store.createSlice(_this.props.slice(store), _this.props.initialState, _this.props.cleanupState);
50 }
51 return React.createElement(Provider, { value: _this.slice }, _this.props.children);
52 }));
53 };
54 return StoreSlice;
55}(React.Component));
56exports.StoreSlice = StoreSlice;
57exports.StoreProjection = /** @class */ (function (_super) {
58 __extends(StoreProjection, _super);
59 function StoreProjection() {
60 return _super !== null && _super.apply(this, arguments) || this;
61 }
62 StoreProjection.prototype.componentWillUnmount = function () {
63 this.slice.destroy();
64 };
65 StoreProjection.prototype.render = function () {
66 var _this = this;
67 return (React.createElement(Consumer, null, function (store) {
68 if (!store)
69 throw new Error("StoreProjection/Slice used outside of a Store context. Did forget to add a <StoreProvider>?");
70 // we ignore this else due to a limitation in enzyme - we can't trigger a
71 // forceUpdate here to test the else branch;
72 /* istanbul ignore else */
73 if (_this.slice === undefined) {
74 _this.slice = store.createProjection(_this.props.forwardProjection, _this.props.backwardProjection, _this.props.initial, _this.props.cleanup);
75 }
76 return React.createElement(Provider, { value: _this.slice }, _this.props.children);
77 }));
78 };
79 return StoreProjection;
80}(React.Component));
81var WithStore = /** @class */ (function (_super) {
82 __extends(WithStore, _super);
83 function WithStore() {
84 return _super !== null && _super.apply(this, arguments) || this;
85 }
86 WithStore.prototype.render = function () {
87 var _this = this;
88 return (React.createElement(Consumer, null, function (store) {
89 var child = _this.props.children;
90 if (!store)
91 throw new Error("WithStore used but no store could be found in context. Did you suppliy a StoreProvider?");
92 else if (typeof _this.props.children !== "function")
93 throw new Error("WithStore used but its child is not a function.");
94 else
95 return child(store);
96 }));
97 };
98 return WithStore;
99}(React.Component));
100exports.WithStore = WithStore;
101/**
102 * A react hook to obtain the current store, depending on the context.
103 */
104function useStore() {
105 var store = React.useContext(context);
106 if (store === undefined) {
107 throw new Error("No store found in context, did you forget to add a Provider for it?");
108 }
109 return store;
110}
111exports.useStore = useStore;
112//# sourceMappingURL=provider.js.map
\No newline at end of file