@hazae41/box
Version:
Rust-like Box and similar objects for TypeScript
47 lines (44 loc) • 999 B
JavaScript
import { Deferred } from '../deferred/index.mjs';
import { Void } from '../void/index.mjs';
class Clone {
value;
clean;
#count = 1;
constructor(value, clean) {
this.value = value;
this.clean = clean;
}
static void() {
return new Clone(undefined, new Void());
}
static wrap(value) {
return new Clone(value, value);
}
static from(value) {
return new Clone(value.get(), value);
}
static with(value, clean) {
return new Clone(value, new Deferred(() => clean(value)));
}
[Symbol.dispose]() {
this.#count--;
if (this.#count > 0)
return;
this.clean[Symbol.dispose]();
}
async [Symbol.asyncDispose]() {
this[Symbol.dispose]();
}
get count() {
return this.#count;
}
get() {
return this.value;
}
clone() {
this.#count++;
return this;
}
}
export { Clone };
//# sourceMappingURL=index.mjs.map