UNPKG

@shopify/app-bridge-host

Version:

App Bridge Host contains components and middleware to be consumed by the app's host, as well as the host itself. The middleware and `Frame` component are responsible for facilitating communication between the client and host, and used to act on actions se

102 lines (96 loc) 3.75 kB
'use strict'; var Actions = require('@shopify/app-bridge-core/actions'); var actions = require('../../../actions.js'); var actions$1 = require('./appBridge/actions.js'); function _interopNamespace(e) { if (e && e.__esModule) return e; var n = Object.create(null); if (e) { Object.keys(e).forEach(function (k) { if (k !== 'default') { var d = Object.getOwnPropertyDescriptor(e, k); Object.defineProperty(n, k, d.get ? d : { enumerable: true, get: function () { return e[k]; } }); } }); } n.default = e; return Object.freeze(n); } var Actions__namespace = /*#__PURE__*/_interopNamespace(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.API_CLIENT_LOAD, initialState), actions.API_CLIENT_UNLOAD, initialState); } /** * 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$1.RESET); } /** * 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); }; } /** * 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; }, {}); } /** * Return all action types for an action group name * @internal */ function getGroupActionType(group) { switch (group) { case Actions__namespace.Group.Navigation: return [Actions__namespace.History.Action, Actions__namespace.Redirect.Action]; case Actions__namespace.Group.ResourcePicker: return [Actions__namespace.ResourcePicker.Action]; case Actions__namespace.Group.unstable_Picker: return [Actions__namespace.unstable_Picker.Action]; case Actions__namespace.Group.Menu: return [Actions__namespace.NavigationMenu.Action, Actions__namespace.ChannelMenu.Action]; case Actions__namespace.Group.Link: return [Actions__namespace.AppLink.Action]; default: return [Actions__namespace[group].Action]; } } exports.getGroupActionType = getGroupActionType; exports.resetAppReducer = resetAppReducer; exports.resetReducer = resetReducer; exports.resetStateReducer = resetStateReducer; exports.wrapReducers = wrapReducers;