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

52 lines (49 loc) 1.88 kB
import { __awaiter, __generator } from 'tslib'; import { retain, release } from '@remote-ui/core'; var RESOLVE_QUEUE_KEY = '__resolveQueue'; function createApiContext() { var _this = this; var api = {}; var toBeResolvedLater = {}; return { get api() { return api; }, resolveApi: function (proxyApi) { Object.keys(proxyApi).forEach(function (key) { var apiKey = key; if (isValidProxy(proxyApi[apiKey])) { var resolver = proxyApi[apiKey][RESOLVE_QUEUE_KEY]; if (Object.prototype.hasOwnProperty.call(api, apiKey)) { resolver(api[apiKey]); } else { // API has not been set so we need to resolve the queue later if (!toBeResolvedLater[apiKey]) { toBeResolvedLater[apiKey] = new Set(); } retain(resolver); toBeResolvedLater[apiKey].add(resolver); } } }, {}); }, setApi: function (key, newApi) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { api[key] = newApi; if (toBeResolvedLater[key]) { toBeResolvedLater[key].forEach(function (resolver) { resolver(api[key]); release(resolver); }); } return [2 /*return*/]; }); }); }, }; } function isValidProxy(api) { return (Object.prototype.hasOwnProperty.call(api, RESOLVE_QUEUE_KEY) && typeof api[RESOLVE_QUEUE_KEY] === 'function'); } export { RESOLVE_QUEUE_KEY, createApiContext };