contexta
Version:
A React-inspired Context API for Node.js with async/await support. Supported in nodejs, bun, and deno via async-local-storage.
16 lines (15 loc) • 436 B
JavaScript
import { AsyncLocalStorage } from 'node:async_hooks';
export default class Context {
asyncLocalStorage;
defaultValue;
constructor(defaultValue) {
this.asyncLocalStorage = new AsyncLocalStorage();
this.defaultValue = defaultValue;
}
run(value, fn) {
return this.asyncLocalStorage.run(value, fn);
}
get() {
return this.asyncLocalStorage.getStore() ?? this.defaultValue;
}
}