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

55 lines (51 loc) 1.93 kB
'use strict'; var tslib = require('tslib'); var core = require('@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(); } core.retain(resolver); toBeResolvedLater[apiKey].add(resolver); } } }, {}); }, setApi: function (key, newApi) { return tslib.__awaiter(_this, void 0, void 0, function () { return tslib.__generator(this, function (_a) { api[key] = newApi; if (toBeResolvedLater[key]) { toBeResolvedLater[key].forEach(function (resolver) { resolver(api[key]); core.release(resolver); }); } return [2 /*return*/]; }); }); }, }; } function isValidProxy(api) { return (Object.prototype.hasOwnProperty.call(api, RESOLVE_QUEUE_KEY) && typeof api[RESOLVE_QUEUE_KEY] === 'function'); } exports.RESOLVE_QUEUE_KEY = RESOLVE_QUEUE_KEY; exports.createApiContext = createApiContext;