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

158 lines (155 loc) 6.19 kB
import { TitleBar, Cart, Group, Pos } from '@shopify/app-bridge-core/actions'; import { isAppBridgeAction } from '@shopify/app-bridge-core/actions/validator'; import { API_CLIENT_LOAD } from '../../../actions.js'; import { isPrivateAppAction } from '../../reducers/embeddedApp/appBridge/reducer.js'; import { StoreReadyAction } from '../../reducers/embeddedApp/appBridge/actions.js'; import 'tslib'; import '@shopify/app-bridge-core/actions/TitleBar'; import '@shopify/app-bridge-core/actions/ButtonGroup'; import '@shopify/app-bridge-core/actions/helper'; import '@shopify/app-bridge-core/actions/uuid'; import { SET_APP_INFO, SET_PAGINATION } from '../../reducers/embeddedApp/titleBar/actions.js'; import '@shopify/app-bridge-core/validate/actions/titleBar'; import '@shopify/app-bridge-core/actions/Navigation/Redirect'; import '@remote-ui/core'; import { isMobileMiddlewareSupported, isMobileMiddlewareAvailable, applyPrintToLegacyButton, traverseButtonPayload, fixDiscountInCartResponse } from './helpers.js'; var LEGACY_LOCATION_UPDATE_TYPE = 'PRIVATE_APP::POS::LOCATION::UPDATE'; var LEGACY_USER_UPDATE_TYPE = 'PRIVATE_APP::POS::USER::UPDATE'; var LEGACY_DEVICE_UPDATE_TYPE = 'PRIVATE_APP::POS::DEVICE::UPDATE'; function privateMiddleware(store, next, action) { switch (action.type) { case SET_PAGINATION: return; case SET_APP_INFO: return; default: if (isAppBridgeAction(action) || action.type === API_CLIENT_LOAD) { return window.__MOBILE_MIDDLEWARE__(store, next, action); } return next(action); } } /** * Build middleware for mobile * @internal */ function buildMobileMiddleware() { return function (store) { return function (next) { var queue = []; var storeReady = false; function resolveQueue() { while (queue.length > 0) { store.dispatch(queue.shift()); } } function onStoreReady() { if (storeReady) { return; } storeReady = true; if (isMobileMiddlewareAvailable()) { resolveQueue(); } else { var isResolved_1 = false; window.__MOBILE_MIDDLEWARE_RESOLVER__ = function (middleware) { if (isResolved_1) { return; } isResolved_1 = true; window.__MOBILE_MIDDLEWARE__ = middleware; resolveQueue(); }; } } return function (action) { if (action === StoreReadyAction) { onStoreReady(); return next(action); } if (!isAppBridgeAction(action) && !isPrivateAppAction(action)) { return next(action); } if (storeReady && isMobileMiddlewareAvailable()) { return privateMiddleware(store, next, action); } queue.push(action); return null; }; }; }; } /** * Build middleware for fixing issue on existing mobile releases * @internal */ function preCorrectionMobileMiddleware() { return function (_a) { var getState = _a.getState; return function (next) { return function (action) { if (isAppBridgeAction(action)) { switch (action.type) { case Cart.Action.UPDATE: { var store = getState(); Object.assign(action.payload, { data: fixDiscountInCartResponse(action.payload.data, store.appBridge.isLegacy), }); break; } case TitleBar.Action.UPDATE: { var payload = action.payload; if (payload.buttons) { if (payload.buttons.primary) { applyPrintToLegacyButton(payload.buttons.primary); } if (payload.buttons.secondary) { Object.assign(payload.buttons, { secondary: traverseButtonPayload(payload.buttons.secondary, function (button) { applyPrintToLegacyButton(button); }), }); } } break; } } } if (isPrivateAppAction(action)) { switch (action.type) { case LEGACY_DEVICE_UPDATE_TYPE: Object.assign(action, { group: Group.Pos, type: Pos.Action.DEVICE_UPDATE, }); break; case LEGACY_LOCATION_UPDATE_TYPE: Object.assign(action, { group: Group.Pos, type: Pos.Action.LOCATION_UPDATE, }); break; case LEGACY_USER_UPDATE_TYPE: Object.assign(action, { group: Group.Pos, type: Pos.Action.USER_UPDATE, }); break; } } next(action); }; }; }; } /** * Returns both preCorrectionMiddleware and App Bridge mobile middleware * @internal */ function getMobileMiddleware() { var mobileMiddleware = [preCorrectionMobileMiddleware()]; if (isMobileMiddlewareSupported()) { mobileMiddleware.push(buildMobileMiddleware()); } return mobileMiddleware; } export { getMobileMiddleware };