@homeofthings/node-utils
Version:
HomeOfThings - Node Utils: various utilities and common types
32 lines • 885 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AsyncContext = void 0;
const async_hooks_1 = require("async_hooks");
/*
* little wrapper around AsyncLocalStorage which currently has an experimental status
* adds a default value
*/
class AsyncContext {
_storage = new async_hooks_1.AsyncLocalStorage();
_default;
get defaultValue() {
return this._default;
}
set defaultValue(value) {
this._default = value;
}
constructor(defaultValue) {
this._default = defaultValue;
}
set(value) {
this._storage.enterWith(value);
}
get() {
return this._storage.getStore() ?? this._default;
}
run(value, callback, ...args) {
return this._storage.run(value, callback, ...args);
}
}
exports.AsyncContext = AsyncContext;
//# sourceMappingURL=async-context.js.map