UNPKG

@hazae41/box

Version:

Rust-like Box and similar objects for TypeScript

59 lines (56 loc) 1.15 kB
'use strict'; /** * A reference that can only be disposed once */ class Once { value; #disposed = false; /** * A reference that can only be disposed once * @param value */ constructor(value) { this.value = value; } [Symbol.dispose]() { if (this.#disposed) return; this.#disposed = true; this.value[Symbol.dispose](); } async [Symbol.asyncDispose]() { this[Symbol.dispose](); } get disposed() { return this.#disposed; } get() { return this.value; } } class AsyncOnce { value; #disposed = false; /** * A reference that can only be disposed once * @param value */ constructor(value) { this.value = value; } async [Symbol.asyncDispose]() { if (this.#disposed) return; this.#disposed = true; await this.value[Symbol.asyncDispose](); } get disposed() { return this.#disposed; } get() { return this.value; } } exports.AsyncOnce = AsyncOnce; exports.Once = Once; //# sourceMappingURL=index.cjs.map