UNPKG

@hazae41/box

Version:

Rust-like Box and similar objects for TypeScript

84 lines (81 loc) 2.19 kB
import { __addDisposableResource, __disposeResources } from '../../../node_modules/tslib/tslib.es6.mjs'; /** * A reference that will be disposed when garbage collected */ class Auto { value; static cleanup = (x) => x[Symbol.dispose](); static registry = new FinalizationRegistry(Auto.cleanup); /** * A reference that will be disposed when garbage collected * @param value */ constructor(value) { this.value = value; Auto.registry.register(this, value, this); } [Symbol.dispose]() { Auto.registry.unregister(this); } async [Symbol.asyncDispose]() { this[Symbol.dispose](); } get() { return this.value; } unwrap() { const env_1 = { stack: [], error: void 0, hasError: false }; try { const _ = __addDisposableResource(env_1, this, false); return this.value; } catch (e_1) { env_1.error = e_1; env_1.hasError = true; } finally { __disposeResources(env_1); } } } /** * A reference that will be disposed when garbage collected */ class AsyncAuto { value; static cleanup = (x) => x[Symbol.asyncDispose]().then(undefined, console.error); static registry = new FinalizationRegistry(AsyncAuto.cleanup); /** * A reference that will be disposed when garbage collected * @param value */ constructor(value) { this.value = value; AsyncAuto.registry.register(this, value, this); } [Symbol.dispose]() { AsyncAuto.registry.unregister(this); } async [Symbol.asyncDispose]() { this[Symbol.dispose](); } get() { return this.value; } unwrap() { const env_2 = { stack: [], error: void 0, hasError: false }; try { const _ = __addDisposableResource(env_2, this, false); return this.value; } catch (e_2) { env_2.error = e_2; env_2.hasError = true; } finally { __disposeResources(env_2); } } } export { AsyncAuto, Auto }; //# sourceMappingURL=index.mjs.map