@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
160 lines (156 loc) • 6.36 kB
JavaScript
;
var Actions = require('@shopify/app-bridge-core/actions');
var validator = require('@shopify/app-bridge-core/actions/validator');
var actions$1 = require('../../../actions.js');
var store_reducers_embeddedApp_appBridge_reducer = require('../../reducers/embeddedApp/appBridge/reducer.js');
var actions = require('../../reducers/embeddedApp/appBridge/actions.js');
require('tslib');
require('@shopify/app-bridge-core/actions/TitleBar');
require('@shopify/app-bridge-core/actions/ButtonGroup');
require('@shopify/app-bridge-core/actions/helper');
require('@shopify/app-bridge-core/actions/uuid');
var actions$2 = require('../../reducers/embeddedApp/titleBar/actions.js');
require('@shopify/app-bridge-core/validate/actions/titleBar');
require('@shopify/app-bridge-core/actions/Navigation/Redirect');
require('@remote-ui/core');
var helpers = require('./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 actions$2.SET_PAGINATION:
return;
case actions$2.SET_APP_INFO:
return;
default:
if (validator.isAppBridgeAction(action) || action.type === actions$1.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 (helpers.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 === actions.StoreReadyAction) {
onStoreReady();
return next(action);
}
if (!validator.isAppBridgeAction(action) && !store_reducers_embeddedApp_appBridge_reducer.isPrivateAppAction(action)) {
return next(action);
}
if (storeReady && helpers.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 (validator.isAppBridgeAction(action)) {
switch (action.type) {
case Actions.Cart.Action.UPDATE: {
var store = getState();
Object.assign(action.payload, {
data: helpers.fixDiscountInCartResponse(action.payload.data, store.appBridge.isLegacy),
});
break;
}
case Actions.TitleBar.Action.UPDATE: {
var payload = action.payload;
if (payload.buttons) {
if (payload.buttons.primary) {
helpers.applyPrintToLegacyButton(payload.buttons.primary);
}
if (payload.buttons.secondary) {
Object.assign(payload.buttons, {
secondary: helpers.traverseButtonPayload(payload.buttons.secondary, function (button) {
helpers.applyPrintToLegacyButton(button);
}),
});
}
}
break;
}
}
}
if (store_reducers_embeddedApp_appBridge_reducer.isPrivateAppAction(action)) {
switch (action.type) {
case LEGACY_DEVICE_UPDATE_TYPE:
Object.assign(action, {
group: Actions.Group.Pos,
type: Actions.Pos.Action.DEVICE_UPDATE,
});
break;
case LEGACY_LOCATION_UPDATE_TYPE:
Object.assign(action, {
group: Actions.Group.Pos,
type: Actions.Pos.Action.LOCATION_UPDATE,
});
break;
case LEGACY_USER_UPDATE_TYPE:
Object.assign(action, {
group: Actions.Group.Pos,
type: Actions.Pos.Action.USER_UPDATE,
});
break;
}
}
next(action);
};
};
};
}
/**
* Returns both preCorrectionMiddleware and App Bridge mobile middleware
* @internal
*/
function getMobileMiddleware() {
var mobileMiddleware = [preCorrectionMobileMiddleware()];
if (helpers.isMobileMiddlewareSupported()) {
mobileMiddleware.push(buildMobileMiddleware());
}
return mobileMiddleware;
}
exports.getMobileMiddleware = getMobileMiddleware;