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

77 lines (70 loc) 2.42 kB
'use strict'; var Redirect = require('@shopify/app-bridge-core/actions/Navigation/Redirect'); var core = require('@remote-ui/core'); var generateUuid = require('@shopify/app-bridge-core/actions/uuid'); function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; } function _interopNamespace(e) { if (e && e.__esModule) return e; var n = Object.create(null); if (e) { Object.keys(e).forEach(function (k) { if (k !== 'default') { var d = Object.getOwnPropertyDescriptor(e, k); Object.defineProperty(n, k, d.get ? d : { enumerable: true, get: function () { return e[k]; } }); } }); } n.default = e; return Object.freeze(n); } var Redirect__namespace = /*#__PURE__*/_interopNamespace(Redirect); var generateUuid__default = /*#__PURE__*/_interopDefault(generateUuid); var unsubscribe = new Set(); function unsub() { unsubscribe.forEach(function (unsub) { return unsub(); }); } function updateButton(action, actionProps, subscribe, dispatch) { var id = actionProps.id || generateUuid__default.default(); var redirect = generateRedirect(dispatch, actionProps.url, actionProps.target, actionProps.external); if (redirect) { var unSub_1 = subscribe(action, redirect, id); unsubscribe.add(function () { unSub_1(); }); } if (actionProps === null || actionProps === void 0 ? void 0 : actionProps.onAction) { core.retain(actionProps.onAction); var unSub_2 = subscribe(action, actionProps.onAction, id); unsubscribe.add(function () { unSub_2(); }); } return id; } function generateRedirect(dispatch, url, target, external) { if (target === void 0) { target = 'APP'; } if (url == null) { return undefined; } var payload = external === true ? { url: url, newContext: true, } : url; return function () { dispatch({ type: redirectAction(target, external), payload: payload }); }; } function redirectAction(target, external) { if (external === true) { return Redirect__namespace.Action.REMOTE; } return Redirect__namespace.Action[target]; } exports.generateRedirect = generateRedirect; exports.unsub = unsub; exports.updateButton = updateButton;