UNPKG

@shopify/app-bridge-host

Version:

App Bridge Host contains middleware and components that are meant to be consumed by the app's host. The middleware and `Frame` component are responsible for facilitating messages posted between the client and host, and used to act on actions sent from the

62 lines (61 loc) 2.69 kB
"use strict"; 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); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0) t[p[i]] = s[p[i]]; return t; }; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; Object.defineProperty(exports, "__esModule", { value: true }); var react_1 = __importStar(require("react")); var redux_1 = require("redux"); var error_1 = require("./components/utilities/error"); var HostProvider_1 = require("./HostProvider"); /** * A higher order component that takes a feature and make its actions and reducer available * @public * @returns a component which has access to the feature actions and local state */ function withFeature(feature) { return function (WrappedComponent) { return function AsyncConnector(props) { var actions = feature.actions, key = feature.key, reducer = feature.reducer, initialState = feature.initialState; var context = react_1.useContext(HostProvider_1.HostContext); var localActions = react_1.useMemo(function () { if (!context) { return error_1.throwMissingHostProvider(); } var app = context.app, addReducer = context.addReducer; addReducer({ key: key, reducer: reducer, initialState: initialState }); return redux_1.bindActionCreators(actions, app.dispatch); }, [context]); var globalStore = props.globalStore, localProps = __rest(props, ["globalStore"]); var localStore = props.globalStore[key]; if (!localStore) { return null; } return (react_1.default.createElement(WrappedComponent, __assign({}, localProps, { actions: localActions, store: localStore }))); }; }; } exports.default = withFeature;