di-tory
Version:
Compose applications with dependency injection
19 lines (18 loc) • 433 B
JavaScript
import { AsyncLocalStorage } from 'async_hooks';
const diAsyncStorage = new AsyncLocalStorage();
const enter = () => {
diAsyncStorage.enterWith(new WeakMap());
};
const run = (fn) => {
return diAsyncStorage.run(new WeakMap(), fn);
};
const getStore = () => diAsyncStorage.getStore() ?? new WeakMap();
const exit = () => {
diAsyncStorage.exit(() => { });
};
export default {
enter,
run,
getStore,
exit,
};