UNPKG

@eversurf/dengine-js

Version:

Debot engine for Java Script

301 lines 11.9 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BinaryBridge = exports.useLibrary = exports.getBridge = exports.ResponseType = void 0; const errors_1 = require("./errors"); var ResponseType; (function (ResponseType) { ResponseType[ResponseType["Success"] = 0] = "Success"; ResponseType[ResponseType["Error"] = 1] = "Error"; ResponseType[ResponseType["Nop"] = 2] = "Nop"; ResponseType[ResponseType["AppRequest"] = 3] = "AppRequest"; ResponseType[ResponseType["AppNotify"] = 4] = "AppNotify"; ResponseType[ResponseType["Custom"] = 100] = "Custom"; })(ResponseType = exports.ResponseType || (exports.ResponseType = {})); let bridge = undefined; function getBridge() { if (!bridge) { throw new errors_1.DebotClientError(1, "Dengine binary bridge isn't set."); } return bridge; } exports.getBridge = getBridge; function useLibrary(loader) { bridge = new BinaryBridge(loader); } exports.useLibrary = useLibrary; function resolveBinding(library) { if ("requestParamsSync" in library) { return { syncLibrary: library }; } if ("requestSync" in library) { return { syncLibrary: new SyncBinaryLibraryAdapter(library) }; } if ("sendRequestParams" in library) { return { library }; } else { return { library: new BinaryLibraryAdapter(library) }; } } class BinaryBridge { constructor(loader) { this.loading = undefined; this.loadError = undefined; this.binding = undefined; this.requests = new Map(); this.nextRequestId = 1; this.contextCount = 0; this.responseHandlerAssigned = false; const libraryOrPromise = loader(); if (libraryOrPromise instanceof Promise) { this.loading = []; libraryOrPromise.then((library) => { const saveLoading = this.loading; this.loading = undefined; const binding = resolveBinding(library); this.binding = binding; saveLoading === null || saveLoading === void 0 ? void 0 : saveLoading.forEach((x) => x.resolve(binding)); }, (reason) => { const saveLoading = this.loading; this.loading = undefined; this.loadError = reason !== null && reason !== void 0 ? reason : undefined; saveLoading === null || saveLoading === void 0 ? void 0 : saveLoading.forEach((x) => x.reject(reason)); }); } else { this.binding = resolveBinding(libraryOrPromise); } } checkResponseHandler() { var _a, _b; const mustBeAssigned = this.contextCount > 0 || this.requests.size > 0; if (this.responseHandlerAssigned !== mustBeAssigned) { if (this.binding) { const { library, syncLibrary } = this.binding; if (mustBeAssigned) { const handler = (requestId, params, responseType, finished) => this.handleLibraryResponse(requestId, params, responseType, finished); (_a = (library !== null && library !== void 0 ? library : syncLibrary)) === null || _a === void 0 ? void 0 : _a.setResponseParamsHandler(handler); } else { (_b = (library !== null && library !== void 0 ? library : syncLibrary)) === null || _b === void 0 ? void 0 : _b.setResponseParamsHandler(); } } this.responseHandlerAssigned = mustBeAssigned; } } getLibName() { return __awaiter(this, void 0, void 0, function* () { const { library, syncLibrary } = yield this.bindingRequired(); if (syncLibrary) { return syncLibrary.getLibName(); } return yield library.getLibName(); }); } getLibNameSync() { return this.syncLibraryRequired().getLibName(); } createContext(config) { return __awaiter(this, void 0, void 0, function* () { const { library, syncLibrary } = yield this.bindingRequired(); this.contextCount += 1; const configJson = JSON.stringify(config); const context = syncLibrary ? syncLibrary.createContext(configJson) : yield library.createContext(configJson); return BinaryBridge.parseResult(context); }); } createContextSync(config) { const library = this.syncLibraryRequired(); this.contextCount += 1; const configJson = JSON.stringify(config); const context = library.createContext(configJson); return BinaryBridge.parseResult(context); } destroyContext(context) { var _a, _b; this.contextCount = Math.max(this.contextCount - 1, 0); this.checkResponseHandler(); if (this.binding) { (_b = ((_a = this.binding.library) !== null && _a !== void 0 ? _a : this.binding.syncLibrary)) === null || _b === void 0 ? void 0 : _b.destroyContext(context); } } request(context, functionName, functionParams, responseHandler) { return __awaiter(this, void 0, void 0, function* () { const { library, syncLibrary } = yield this.bindingRequired(); return new Promise((resolve, reject) => { var _a; const request = { resolve, reject, responseHandler, }; const requestId = this.generateRequestId(); this.requests.set(requestId, request); this.checkResponseHandler(); (_a = (library !== null && library !== void 0 ? library : syncLibrary)) === null || _a === void 0 ? void 0 : _a.sendRequestParams(context, requestId, functionName, functionParams); }); }); } requestSync(context, functionName, functionParams) { const library = this.syncLibraryRequired(); return BinaryBridge.parseResultParams(library.requestParamsSync(context, functionName, functionParams)); } bindingRequired() { if (this.binding) { return Promise.resolve(this.binding); } if (this.loadError) { return Promise.reject(this.loadError); } if (this.loading === undefined) { return Promise.reject(new errors_1.DebotClientError(1, "Dengine binary library isn't set.")); } return new Promise((resolve, reject) => { var _a; (_a = this.loading) === null || _a === void 0 ? void 0 : _a.push({ resolve, reject, }); }); } syncLibraryRequired() { var _a; const library = (_a = this.binding) === null || _a === void 0 ? void 0 : _a.syncLibrary; if (library) { return library; } throw new errors_1.DebotClientError(1, "Dengine binary library does not support sync calls."); } generateRequestId() { const id = this.nextRequestId; do { this.nextRequestId += 1; if (this.nextRequestId >= Number.MAX_SAFE_INTEGER) { this.nextRequestId = 1; } } while (this.requests.has(this.nextRequestId)); return id; } handleLibraryResponse(requestId, params, responseType, finished) { const request = this.requests.get(requestId); if (!request) { return; } if (finished) { this.requests.delete(requestId); this.checkResponseHandler(); } switch (responseType) { case ResponseType.Success: request.resolve(params); break; case ResponseType.Error: request.reject(params); break; default: const isAppObjectOrCustom = responseType === ResponseType.AppNotify || responseType === ResponseType.AppRequest || responseType >= ResponseType.Custom; if (isAppObjectOrCustom && request.responseHandler) { request.responseHandler(params, responseType); } } } static parseResult(resultJson) { if (resultJson === undefined) { return undefined; } return BinaryBridge.parseResultParams(JSON.parse(resultJson)); } static parseResultParams(result) { if (result === undefined) { return undefined; } if ("error" in result) { throw new errors_1.DebotClientError(result.error.code, result.error.message, result.error.data); } return result.result; } } exports.BinaryBridge = BinaryBridge; class BinaryLibraryAdapter { constructor(library) { this.library = library; } getLibName() { return this.library.getLibName(); } createContext(configJson) { return this.library.createContext(configJson); } destroyContext(context) { this.library.destroyContext(context); } setResponseParamsHandler(handler) { if (handler === undefined) { this.library.setResponseHandler(undefined); } else { this.library.setResponseHandler(responseParamsAdapter(handler)); } } sendRequestParams(context, requestId, functionName, functionParams) { this.library.sendRequest(context, requestId, functionName, toJson(functionParams)); } } class SyncBinaryLibraryAdapter { constructor(library) { this.library = library; } getLibName() { return this.library.getLibName(); } createContext(configJson) { return this.library.createContext(configJson); } destroyContext(context) { this.library.destroyContext(context); } setResponseParamsHandler(handler) { if (handler === undefined) { this.library.setResponseHandler(undefined); } else { this.library.setResponseHandler(responseParamsAdapter(handler)); } } sendRequestParams(context, requestId, functionName, functionParams) { this.library.sendRequest(context, requestId, functionName, toJson(functionParams)); } requestParamsSync(context, functionName, functionParams) { return parseJson(this.library.requestSync(context, functionName, toJson(functionParams))); } } function responseParamsAdapter(handler) { return (requestId, paramsJson, responseType, finished) => handler(requestId, parseJson(paramsJson), responseType, finished); } function parseJson(json) { return json !== "" ? JSON.parse(json) : undefined; } function toJson(params) { return params === undefined || params === null ? "" : JSON.stringify(params, (_, value) => typeof value === "bigint" ? value < Number.MAX_SAFE_INTEGER && value > Number.MIN_SAFE_INTEGER ? Number(value) : value.toString() : value); } //# sourceMappingURL=bin.js.map