@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
84 lines (83 loc) • 3.63 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var redux_1 = require("redux");
var middlewares_1 = require("./middlewares");
var helpers_1 = require("./middlewares/mobile/helpers");
var features_1 = require("./reducers/embeddedApp/features");
var utilities_1 = require("./reducers/embeddedApp/utilities");
/**
* The constant key `appBridge`
* @public
*/
exports.APP_BRIDGE_KEY = 'appBridge';
/**
* Returns a combined reducer for the `appBridge` key
* Includes the private reducers 'appInfo', 'features', 'pos', 'staffMember' and 'isLegacy'
* @public
* @param stateReducers - a reducer map for the dynamic app state
* @param initialState - an optional default value for the store
*/
function createReducers(stateReducers, initialState) {
if (stateReducers === void 0) { stateReducers = {}; }
if (initialState === void 0) { initialState = {}; }
var _a;
return redux_1.combineReducers((_a = {},
_a[exports.APP_BRIDGE_KEY] = redux_1.combineReducers(utilities_1.wrapReducers(__assign({ features: features_1.asyncFeaturesReducer }, stateReducers), utilities_1.resetAppReducer, initialState)),
_a));
}
exports.createReducers = createReducers;
/**
* Creates a store containing only the default reducers
* 'appInfo', 'features', 'pos', 'staffMember' and 'isLegacy'
* @public
*/
function createStore(middlewares, initialState) {
if (middlewares === void 0) { middlewares = []; }
if (initialState === void 0) { initialState = {}; }
var _a;
var defaultState = (_a = {}, _a[exports.APP_BRIDGE_KEY] = initialState, _a);
var composeEnhancers = typeof window !== 'undefined' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({ name: 'App Bridge' })
: redux_1.compose;
var mobileMiddlewares = [middlewares_1.preCorrectionMobileMiddleware()];
if (helpers_1.isMobileMiddlewareSupported()) {
mobileMiddlewares.push(middlewares_1.buildMobileMiddleware());
}
return redux_1.createStore(createReducers({}, initialState), defaultState, composeEnhancers(redux_1.applyMiddleware.apply(void 0, mobileMiddlewares.concat(middlewares))));
}
exports.createStore = createStore;
/**
* Creates a method that when called, dynamically adds a reducer to
* the provided store
* @internal
* @param store - a Redux store
* @param globalInitialState - custom overrides for resolving the app state when adding a new reducer
* */
function createAddReducer(store, globalInitialState) {
if (globalInitialState === void 0) { globalInitialState = {}; }
var asyncReducers = {};
return function addReducer(_a) {
var key = _a.key, reducer = _a.reducer, initialState = _a.initialState;
var _b;
if (asyncReducers.hasOwnProperty(key)) {
return;
}
asyncReducers[key] = reducer;
var currentState = store.getState()[exports.APP_BRIDGE_KEY];
var localState = globalInitialState[key] || initialState;
var newState = localState ? __assign({}, currentState, (_b = {}, _b[key] = localState, _b)) : currentState;
store.replaceReducer(createReducers(asyncReducers, newState));
};
}
exports.createAddReducer = createAddReducer;