UNPKG

@hazae41/box

Version:

Rust-like Box and similar objects for TypeScript

168 lines (164 loc) 4.05 kB
'use strict'; var index = require('../borrow/index.cjs'); var _a; class MovedError extends Error { #class = _a; name = this.#class.name; constructor() { super(`Resource has been moved`); } } _a = MovedError; /** * An ownable and borrowable reference */ class Box { value; #state = "owned"; /** * An ownable reference * @param value */ constructor(value) { this.value = value; } [Symbol.dispose]() { if (this.dropped) return; if (this.owned) this.value[Symbol.dispose](); this.#state = "dropped"; } async [Symbol.asyncDispose]() { this[Symbol.dispose](); } static create(value) { return new Box(value); } static createAsDropped(value) { const box = new Box(value); box.#state = "dropped"; return box; } get owned() { return this.#state === "owned"; } get borrowed() { return this.#state === "borrowed"; } get dropped() { return this.#state === "dropped"; } /** * Get the value * @returns T */ get() { return this.value; } /** * Get the value or null-like if not owned * @returns T or null-like if not owned */ getOrNull() { if (!this.owned) return; return this.value; } /** * Get the value or throw if not owned * @returns T * @throws NotOwnedError if not owned */ getOrThrow() { if (this.borrowed) throw new index.BorrowedError(); if (this.dropped) throw new index.DroppedError(); return this.value; } checkOrNull() { if (!this.owned) return; return this; } checkOrThrow() { if (this.borrowed) throw new index.BorrowedError(); if (this.dropped) throw new index.DroppedError(); return this; } /** * Get the value and set this as moved or null-like if not owned * @returns T or null-like if not owned */ unwrapOrNull() { if (!this.owned) return; this.#state = "dropped"; return this.value; } /** * Get the value and set this as moved or throw if not owned * @returns T * @throws BoxMovedError if not owned */ unwrapOrThrow() { if (this.borrowed) throw new index.BorrowedError(); if (this.dropped) throw new index.DroppedError(); this.#state = "dropped"; return this.value; } /** * Move the value to a new box and set this one as moved or null-like if already moved * @returns Box<T> or null-like if moved */ moveOrNull() { if (!this.owned) return; this.#state = "dropped"; return new Box(this.value); } /** * Move the value to a new box and set this one as moved or throw if already moved * @returns Box<T> * @throws BoxMovedError if already moved */ moveOrThrow() { if (this.borrowed) throw new index.BorrowedError(); if (this.dropped) throw new index.DroppedError(); this.#state = "dropped"; return new Box(this.value); } borrowOrNull() { if (!this.owned) return; this.#state = "borrowed"; return new index.Borrow(this); } borrowOrThrow() { if (this.borrowed) throw new index.BorrowedError(); if (this.dropped) throw new index.DroppedError(); this.#state = "borrowed"; return new index.Borrow(this); } returnOrThrow() { if (this.owned) throw new index.OwnedError(); if (this.borrowed) this.#state = "owned"; else if (this.dropped) this.value[Symbol.dispose](); return; } } exports.Box = Box; exports.MovedError = MovedError; //# sourceMappingURL=index.cjs.map