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

76 lines (73 loc) 2.9 kB
import * as Actions from '@shopify/app-bridge-core/actions'; import { API_CLIENT_UNLOAD, API_CLIENT_LOAD } from '../../../actions.js'; import { RESET } from './appBridge/actions.js'; /** * 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, API_CLIENT_LOAD, initialState), 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, 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.Group.Navigation: return [Actions.History.Action, Actions.Redirect.Action]; case Actions.Group.ResourcePicker: return [Actions.ResourcePicker.Action]; case Actions.Group.unstable_Picker: return [Actions.unstable_Picker.Action]; case Actions.Group.Menu: return [Actions.NavigationMenu.Action, Actions.ChannelMenu.Action]; case Actions.Group.Link: return [Actions.AppLink.Action]; default: return [Actions[group].Action]; } } export { getGroupActionType, resetAppReducer, resetReducer, resetStateReducer, wrapReducers };