UNPKG

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