@appsemble/sdk
Advanced tools
Comparing version 0.0.0-alpha.3 to 0.0.0-alpha.4
@@ -10,2 +10,6 @@ import { rpc } from './lib/communication' | ||
* @returns {Object} The extension context. | ||
* @property {boolean} isBootstrap An indication on whether or not the extension is running in the | ||
* bootstrap app. | ||
* @property {boolean} hasMenu A boolean indicating whether or not the menu can be toggled. This | ||
* should be used to determine whether or not a menu button should be rendered. | ||
* @property {Object} app The user app. | ||
@@ -12,0 +16,0 @@ * @property {Object} part The part for which the extension is running. |
34
index.js
import { makeRPC } from './lib/communication'; | ||
var readyPromise = void 0; | ||
var bootstrap = void 0; | ||
let readyPromise; | ||
let bootstrap; | ||
let shouldShowMenu; | ||
@@ -14,3 +15,3 @@ /** | ||
*/ | ||
export var app = {}; | ||
export const app = {}; | ||
@@ -25,3 +26,3 @@ /** | ||
*/ | ||
export var part = {}; | ||
export const part = {}; | ||
@@ -37,4 +38,5 @@ /** | ||
if (!readyPromise) { | ||
readyPromise = makeRPC('core#ready').then(function (result) { | ||
readyPromise = makeRPC('core#ready').then(result => { | ||
bootstrap = result.isBootstrap; | ||
shouldShowMenu = result.hasMenu; | ||
Object.assign(app, result.app); | ||
@@ -48,2 +50,9 @@ Object.assign(part, result.part); | ||
function safeValue(value) { | ||
if (value === undefined) { | ||
throw new Error('The Appsemble SDK hasn’t been initialized yet. Make sure to call ‘ready’ first.'); | ||
} | ||
return value; | ||
} | ||
/** | ||
@@ -56,6 +65,13 @@ * Check whether or not the extension is running in the Appsemble bootstrap app. | ||
export function isBootstrap() { | ||
if (bootstrap === undefined) { | ||
throw new Error('The Appsemble SDK hasn’t been initialized yet. Make sure to call ‘ready’ first.'); | ||
} | ||
return bootstrap; | ||
return safeValue(bootstrap); | ||
} | ||
/** | ||
* Check whether or not the extension has a menu. | ||
* | ||
* @returns {Boolean} True if a menu should be rendered. | ||
* @throws If the promise from {@link ready} has not been resolved yet. | ||
*/ | ||
export function hasMenu() { | ||
return safeValue(shouldShowMenu); | ||
} |
@@ -10,7 +10,7 @@ // This module is autogenerated, so it can be imported. | ||
*/ | ||
export var NOT_AN_IFRAME = 'The extension is not running in an iframe'; | ||
export const NOT_AN_IFRAME = 'The extension is not running in an iframe'; | ||
var rpcCounter = 0; | ||
var resolvers = new Map(); | ||
var check = '🚀appsembleSDKLoaded🚀'; | ||
let rpcCounter = 0; | ||
const resolvers = new Map(); | ||
const check = '🚀appsembleSDKLoaded🚀'; | ||
if (window[check]) { | ||
@@ -33,3 +33,3 @@ throw new Error('The Appsemble SDK has been multiple times. Make sure it gets loaded only once.'); | ||
export function makeRPC(name, args) { | ||
return new Promise(function (resolve, reject) { | ||
return new Promise((resolve, reject) => { | ||
if (window.parent === window) { | ||
@@ -42,9 +42,9 @@ reject(NOT_AN_IFRAME); | ||
// eslint-disable-next-line no-plusplus | ||
var id = rpcCounter++; | ||
const id = rpcCounter++; | ||
parent.postMessage({ | ||
name: name, | ||
id: id, | ||
args: args | ||
name, | ||
id, | ||
args | ||
}, '*'); | ||
resolvers.set(id, { resolve: resolve, reject: reject }); | ||
resolvers.set(id, { resolve, reject }); | ||
}); | ||
@@ -57,9 +57,3 @@ } | ||
export function rpc(name) { | ||
return function () { | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
return makeRPC(name, args); | ||
}; | ||
return (...args) => makeRPC(name, args); | ||
} | ||
@@ -71,9 +65,7 @@ | ||
export function handleRPCResponse(response) { | ||
var callbacks = resolvers.get(response.id); | ||
const callbacks = resolvers.get(response.id); | ||
if (callbacks === undefined) { | ||
return; | ||
} | ||
var resolve = callbacks.resolve, | ||
reject = callbacks.reject; | ||
const { resolve, reject } = callbacks; | ||
resolvers.delete(response.id); | ||
@@ -92,3 +84,3 @@ switch (response.status) { | ||
addEventListener('message', function (event) { | ||
addEventListener('message', event => { | ||
// The message is from a source which isn't Appsemble. | ||
@@ -95,0 +87,0 @@ if (event.source !== window.parent) { |
{ | ||
"name": "@appsemble/sdk", | ||
"description": "The JavaScript SDK for communication with the Appsemble extension host.", | ||
"version": "0.0.0-alpha.3", | ||
"version": "0.0.0-alpha.4", | ||
"license": "MIT", | ||
"author": "Appsemble <support@appsemble.com> (https://www.appsemble.com)" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8799
264