UNPKG

@spedn/sdk

Version:

Spedn compiler SDK

32 lines 914 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Bridge = void 0; class Bridge { constructor(worker) { this.worker = worker; this.requests = {}; this.counter = 0; this.worker.on("message", this.handleMessage.bind(this)); this.worker.on("error", e => { console.log(e); }); } request(func, ...args) { return new Promise((resolve, _) => { const id = this.counter++; this.requests[id] = resolve; this.worker.postMessage({ id, func, args }); }); } dispose() { this.worker.postMessage({ id: -1, func: "dispose" }); } handleMessage({ id, result }) { if (this.requests[id]) { this.requests[id](result); delete this.requests[id]; } } } exports.Bridge = Bridge; //# sourceMappingURL=Bridge.js.map