@test-org122/utils
Version:
Utilities used by Hypernet Labs packages
116 lines • 4.08 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 postmate_1 = __importDefault(require("postmate"));
const neverthrow_1 = require("neverthrow");
const errors_1 = require("./errors");
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) {
this.callId = 0;
this.calls = [];
this.child = null;
this.active = false;
if (element == null) {
element = document.body;
}
this.handshake = new postmate_1.default({
container: element,
url: iframeUrl,
name: "hypernet-core-merchant-connector-iframe",
classListArray: ["hypernet-core-iframe-style"],
});
}
activate() {
if (this.activateResult != null) {
return this.activateResult;
}
this.activateResult = neverthrow_1.ResultAsync.fromPromise(this.handshake, (e) => 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 neverthrow_1.errAsync(new errors_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