@hypernetlabs/utils
Version:
Utilities used by Hypernet Labs packages
122 lines • 4.44 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ParentProxy = void 0;
const objects_1 = require("@hypernetlabs/objects");
const neverthrow_1 = require("neverthrow");
const postmate_1 = __importDefault(require("postmate"));
class IFrameCallData {
constructor(callId, data) {
this.callId = callId;
this.data = data;
}
}
class IFrameCall {
constructor(callData) {
this.callData = callData;
this.resolveFunc = null;
this.rejectFunc = null;
this.promise = new Promise((resolve, reject) => {
this.resolveFunc = resolve;
this.rejectFunc = reject;
});
}
resolve(result) {
if (this.resolveFunc != null) {
this.resolveFunc(result);
}
}
reject(error) {
if (this.rejectFunc != null) {
this.rejectFunc(error);
}
}
getResult() {
return neverthrow_1.ResultAsync.fromPromise(this.promise, (e) => {
return e;
});
}
}
class ParentProxy {
constructor(element, iframeUrl, iframeName, debug = false) {
this.element = element;
this.iframeUrl = iframeUrl;
this.iframeName = iframeName;
this.debug = debug;
this.callId = 0;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this.calls = [];
this.child = null;
this.active = false;
if (element == null) {
element = document.body;
}
postmate_1.default.debug = debug;
this.handshake = new postmate_1.default({
container: element,
url: iframeUrl,
name: iframeName,
classListArray: ["hypernet-core-iframe-style"], // Classes to add to the iframe
});
}
activate() {
if (this.activateResult != null) {
return this.activateResult;
}
this.activateResult = neverthrow_1.ResultAsync.fromPromise(this.handshake, (e) => new objects_1.ProxyError("Proxy handshake failed in parent", e)).map((child) => {
// Stash the API for future calls
this.child = child;
child.on("callSuccess", (data) => {
// Get the matching calls
const matchingCalls = this.calls.filter((val) => {
return val.callData.callId == data.callId;
});
// Remove the matching calls from the source array
this.calls = this.calls.filter((val) => {
return val.callData.callId != data.callId;
});
// Resolve the calls - should only ever be 1
for (const call of matchingCalls) {
call.resolve(data.data);
}
});
child.on("callError", (data) => {
// Get the matching calls
const matchingCalls = this.calls.filter((val) => {
return val.callData.callId == data.callId;
});
// Remove the matching calls from the source array
this.calls = this.calls.filter((val) => {
return val.callData.callId != data.callId;
});
// Reject the calls - should only ever be 1
for (const call of matchingCalls) {
call.reject(data.data);
}
});
this.active = true;
});
return this.activateResult;
}
destroy() {
var _a;
(_a = this.child) === null || _a === void 0 ? void 0 : _a.destroy();
this.active = false;
}
_createCall(callName, data) {
var _a;
if (!this.active) {
return (0, neverthrow_1.errAsync)(new objects_1.ProxyError("Proxy is not activated or has been destroyed, cannot make a call to the iframe!"));
}
const callId = this.callId++;
const callData = new IFrameCallData(callId, data);
const call = new IFrameCall(callData);
this.calls.push(call);
(_a = this.child) === null || _a === void 0 ? void 0 : _a.call(callName, callData);
return call.getResult();
}
}
exports.ParentProxy = ParentProxy;
//# sourceMappingURL=ParentProxy.js.map