hakojs
Version:
A secure, embeddable JavaScript engine that runs untrusted code inside WebAssembly sandboxes with fine-grained permissions and resource limits
63 lines (61 loc) • 1.61 kB
JavaScript
// src/helpers/deferred-promise.ts
class HakoDeferredPromise {
owner;
context;
handle;
settled;
resolveHandle;
rejectHandle;
onSettled;
constructor(args) {
this.context = args.context;
this.owner = args.context.runtime;
this.handle = args.promiseHandle;
this.settled = new Promise((resolve) => {
this.onSettled = resolve;
});
this.resolveHandle = args.resolveHandle;
this.rejectHandle = args.rejectHandle;
}
resolve = (value) => {
if (!this.resolveHandle.alive) {
return;
}
this.context.unwrapResult(this.context.callFunction(this.resolveHandle, this.context.undefined(), value || this.context.undefined())).dispose();
this.disposeResolvers();
this.onSettled();
};
reject = (value) => {
if (!this.rejectHandle.alive) {
return;
}
this.context.unwrapResult(this.context.callFunction(this.rejectHandle, this.context.undefined(), value || this.context.undefined())).dispose();
this.disposeResolvers();
this.onSettled();
};
get alive() {
return this.handle.alive || this.resolveHandle.alive || this.rejectHandle.alive;
}
dispose = () => {
if (this.handle.alive) {
this.handle.dispose();
}
this.disposeResolvers();
};
disposeResolvers() {
if (this.resolveHandle.alive) {
this.resolveHandle.dispose();
}
if (this.rejectHandle.alive) {
this.rejectHandle.dispose();
}
}
[Symbol.dispose]() {
this.dispose();
}
}
export {
HakoDeferredPromise
};
//# debugId=133C73EA2BCD558464756E2164756E21
//# sourceMappingURL=deferred-promise.js.map