UNPKG

@hazae41/box

Version:

Rust-like Box and similar objects for TypeScript

56 lines (54 loc) 1.11 kB
/** * 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; } } export { AsyncOnce, Once }; //# sourceMappingURL=index.mjs.map