contexta
Version:
A React-inspired Context API for Node.js with async/await support. Supported in nodejs, bun, and deno via async-local-storage.
19 lines (18 loc) • 546 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const node_async_hooks_1 = require("node:async_hooks");
class Context {
asyncLocalStorage;
defaultValue;
constructor(defaultValue) {
this.asyncLocalStorage = new node_async_hooks_1.AsyncLocalStorage();
this.defaultValue = defaultValue;
}
run(value, fn) {
return this.asyncLocalStorage.run(value, fn);
}
get() {
return this.asyncLocalStorage.getStore() ?? this.defaultValue;
}
}
exports.default = Context;