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

92 lines (91 loc) 3.5 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 __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 Actions = __importStar(require("@shopify/app-bridge/actions")); var actions_1 = require("../../../actions"); var actions_2 = require("./appBridge/actions"); /** * A utility to reset the reducer state when `APICLIENT::LOAD` * or `APICLIENT::UNLOAD` is dispatched * @internal * @param reducer - the reducer which should be reset * @param initialState - an option value the state should be set to when it's reset * */ function resetAppReducer(reducer, initialState) { return resetReducer(resetReducer(reducer, actions_1.API_CLIENT_LOAD, initialState), actions_1.API_CLIENT_UNLOAD, initialState); } exports.resetAppReducer = resetAppReducer; /** * A utility to reset the reducer state when `PRIVATE_APP::RESET` is dispatched * @internal * @param reducer - the reducer which should be reset * */ function resetStateReducer(reducer) { return resetReducer(reducer, actions_2.RESET); } exports.resetStateReducer = resetStateReducer; /** * A utility to reset the state of a reducer when an action is dispatched * @internal * @param reducer - the reducer which should be reset * @param type - the action type * @param initialState - an option value the state should be set to when it's reset */ function resetReducer(reducer, type, initialState) { return function (state, action) { if (action.type === type) { // NOTE: The reset mechanism depends the reducer's default value for state or a specified state // If a default value is not specified then we rely on the reducer's default state return reducer(initialState, { type: undefined }); } return reducer(state, action); }; } exports.resetReducer = resetReducer; /** * Wrap multiple reducers in a given wrapper function * @internal * @param reducers - a reducer map object * @param wrapper - the function to wrap the reducer map object * @param initialState - an optional value map for the state to be given to the wrapper */ function wrapReducers(reducers, wrapper, initialState) { if (initialState === void 0) { initialState = {}; } return Object.keys(reducers).reduce(function (memo, key) { memo[key] = wrapper(reducers[key], initialState[key]); return memo; }, {}); } exports.wrapReducers = wrapReducers; /** * Return the full action type given a group name * @internal */ function getGroupActionType(group) { switch (group) { case Actions.Group.Navigation: return __assign({}, Actions.History.ActionType, Actions.Redirect.ActionType); case Actions.Group.ResourcePicker: return Actions.ResourcePicker.ActionType; default: return Actions[group].ActionType; } } exports.getGroupActionType = getGroupActionType;