@hazae41/box
Version:
Rust-like Box and similar objects for TypeScript
54 lines (52 loc) • 1.33 kB
JavaScript
/**
* 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;
}
}
/**
* 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;
}
}
export { AsyncAuto, Auto };
//# sourceMappingURL=index.mjs.map