UNPKG

@plattar/context-messenger

Version:

Context Messenger framework for cross-iframe communication (https://www.plattar.com)

86 lines (75 loc) 2.69 kB
const Util = require("../util/util.js"); /** * WrappedLocalFunction represents a container that holds and maintains a specific function * that was defined in the current web context. It can also be executed by other web contexts * using the Messenger framework. */ class WrappedLocalFunction { constructor(funcName) { this._value = undefined; this._callback = undefined; this._funcName = funcName; } /** * executes the internally stored function with the provided arguments */ _execute(...args) { const rData = this._value(...args); if (this._callback) { this._callback(rData, ...args); } return rData; } /** * Executes the internal function in a Promise chain. Results of the execution * will be evaluated in the promise chain itself */ exec(...args) { return new Promise((accept, reject) => { if (!this._value) { return reject(new Error("WrappedLocalFunction.exec() function with name " + this._funcName + "() is not defined")); } try { // otherwise execute the function const rObject = this._execute(...args); // we need to check if the returned object is a Promise, if so, handle it // differently. This can happen if the function wants to execute asyn if (Util.isPromise(rObject)) { rObject.then((res) => { return accept(res); }).catch((err) => { return reject(err); }); } else { // otherwise, its a non async object so just execute and return the results return accept(rObject); } } catch (e) { return reject(e); } }); } /** * Stores a function for later execution */ set value(newValue) { if (typeof newValue !== "function") { throw new TypeError("WrappedLocalFunction.value must be a function. To store values use Plattar.memory"); } this._value = newValue; } /** * Watches for when this function is executed by some context */ set watch(newValue) { if (typeof newValue === "function") { this._callback = newValue; } else { throw new TypeError("WrappedLocalFunction.watch must be a type of function. Try using WrappedLocalFunction.watch = (rData, ...args) => {}"); } } } module.exports = WrappedLocalFunction;