UNPKG

2.8 kBJavaScriptView Raw
1"use strict";
2var __assign = (this && this.__assign) || function () {
3 __assign = Object.assign || function(t) {
4 for (var s, i = 1, n = arguments.length; i < n; i++) {
5 s = arguments[i];
6 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7 t[p] = s[p];
8 }
9 return t;
10 };
11 return __assign.apply(this, arguments);
12};
13var __rest = (this && this.__rest) || function (s, e) {
14 var t = {};
15 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
16 t[p] = s[p];
17 if (s != null && typeof Object.getOwnPropertySymbols === "function")
18 for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
19 t[p[i]] = s[p[i]];
20 return t;
21};
22var __importStar = (this && this.__importStar) || function (mod) {
23 if (mod && mod.__esModule) return mod;
24 var result = {};
25 if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
26 result["default"] = mod;
27 return result;
28};
29Object.defineProperty(exports, "__esModule", { value: true });
30var react_1 = __importStar(require("react"));
31var redux_1 = require("redux");
32var error_1 = require("./components/utilities/error");
33var HostProvider_1 = require("./HostProvider");
34/**
35 * A higher order component that takes a feature and make its actions and reducer available
36 * @public
37 * @returns a component which has access to the feature actions and local state
38 */
39function withFeature(feature) {
40 return function (WrappedComponent) {
41 return function AsyncConnector(props) {
42 var actions = feature.actions, key = feature.key, reducer = feature.reducer, initialState = feature.initialState;
43 var context = react_1.useContext(HostProvider_1.HostContext);
44 var localActions = react_1.useMemo(function () {
45 if (!context) {
46 error_1.throwMissingHostProvider();
47 return;
48 }
49 if (!actions) {
50 return;
51 }
52 var app = context.app, addReducer = context.addReducer;
53 addReducer({ key: key, reducer: reducer, initialState: initialState });
54 return redux_1.bindActionCreators(actions, app.dispatch);
55 }, [context]);
56 var globalStore = props.globalStore, localProps = __rest(props, ["globalStore"]);
57 var localStore = props.globalStore[key];
58 if (localStore === undefined) {
59 return null;
60 }
61 return (react_1.default.createElement(WrappedComponent, __assign({}, localProps, { actions: localActions, store: localStore })));
62 };
63 };
64}
65exports.default = withFeature;