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

103 lines (102 loc) 4.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var actions_1 = require("@shopify/app-bridge/actions"); var validator_1 = require("@shopify/app-bridge/actions/validator"); var actions_2 = require("../../../actions"); var appBridge_1 = require("../../reducers/embeddedApp/appBridge"); var reducer_1 = require("../../reducers/embeddedApp/appBridge/reducer"); var titleBar_1 = require("../../reducers/embeddedApp/titleBar"); var helpers_1 = require("./helpers"); function dispatchToMobileMiddleware(store, next, action) { var hasCartPermission = store.getState().appBridge.appInfo.hasCartPermission; if (helpers_1.isCartAction(action) && !hasCartPermission) { next(actions_1.Error.permissionAction(action, helpers_1.CART_PERMISSION_MESSAGE)); return; } window.__MOBILE_MIDDLEWARE__(store, next, action); } function privateMiddleware(store, next, action) { switch (action.type) { case titleBar_1.SET_PAGINATION: return; case titleBar_1.SET_APP_INFO: return; default: if (validator_1.isAppBridgeAction(action) || action.type === actions_2.API_CLIENT_LOAD) { return dispatchToMobileMiddleware(store, next, action); } return next(action); } } 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 (helpers_1.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 === appBridge_1.StoreReadyAction) { onStoreReady(); return next(action); } if (!validator_1.isAppBridgeAction(action) && !reducer_1.isPrivateAppAction(action)) { return next(action); } if (storeReady && helpers_1.isMobileMiddlewareAvailable()) { return privateMiddleware(store, next, action); } queue.push(action); return null; }; }; }; } exports.default = buildMobileMiddleware; function preCorrectionMobileMiddleware() { return function (_a) { var getState = _a.getState; return function (next) { return function (action) { if (validator_1.isAppBridgeAction(action) && action.type === actions_1.Cart.ActionType.UPDATE) { var store = getState(); action.payload.data = helpers_1.fixDiscountInCartResponse(action.payload.data, store.appBridge.isLegacy); } if (action.type === actions_1.TitleBar.ActionType.UPDATE) { var payload = action.payload; if (payload.buttons) { if (payload.buttons.primary) { helpers_1.applyPrintToLegacyButton(payload.buttons.primary); } if (payload.buttons.secondary) { payload.buttons.secondary = helpers_1.traverseButtonPayload(payload.buttons.secondary, function (button) { helpers_1.applyPrintToLegacyButton(button); }); } } } next(action); }; }; }; } exports.preCorrectionMobileMiddleware = preCorrectionMobileMiddleware;