@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
50 lines (47 loc) • 1.58 kB
JavaScript
import * as Redirect from '@shopify/app-bridge-core/actions/Navigation/Redirect';
import { retain } from '@remote-ui/core';
import generateUuid from '@shopify/app-bridge-core/actions/uuid';
var unsubscribe = new Set();
function unsub() {
unsubscribe.forEach(function (unsub) { return unsub(); });
}
function updateButton(action, actionProps, subscribe, dispatch) {
var id = actionProps.id || generateUuid();
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) {
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.Action.REMOTE;
}
return Redirect.Action[target];
}
export { generateRedirect, unsub, updateButton };