@hazae41/box
Version:
Rust-like Box and similar objects for TypeScript
87 lines (83 loc) • 2.23 kB
JavaScript
;
var tslib_es6 = require('../../../node_modules/tslib/tslib.es6.cjs');
/**
* 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;
}
unwrap() {
const env_1 = { stack: [], error: void 0, hasError: false };
try {
const _ = tslib_es6.__addDisposableResource(env_1, this, false);
return this.value;
}
catch (e_1) {
env_1.error = e_1;
env_1.hasError = true;
}
finally {
tslib_es6.__disposeResources(env_1);
}
}
}
/**
* 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;
}
unwrap() {
const env_2 = { stack: [], error: void 0, hasError: false };
try {
const _ = tslib_es6.__addDisposableResource(env_2, this, false);
return this.value;
}
catch (e_2) {
env_2.error = e_2;
env_2.hasError = true;
}
finally {
tslib_es6.__disposeResources(env_2);
}
}
}
exports.AsyncAuto = AsyncAuto;
exports.Auto = Auto;
//# sourceMappingURL=index.cjs.map