UNPKG

@plugjs/plug

Version:
37 lines (36 loc) 1.04 kB
// async.ts import { AsyncLocalStorage } from "node:async_hooks"; import { assert } from "./asserts.mjs"; import { getSingleton } from "./utils/singleton.mjs"; function runAsync(context, callback) { return storage.run(context, async () => { try { tasks.add(context.taskName); return await callback(); } finally { tasks.delete(context.taskName); } }); } function currentContext() { return storage.getStore(); } function requireContext() { const context = storage.getStore(); assert(context, "Unable to retrieve current context"); return context; } function runningTasks() { return [...tasks].sort(); } var storageKey = Symbol.for("plugjs:plug:singleton:contextStorage"); var tasksKey = Symbol.for("plugjs:plug:singleton:runningTasksStorage"); var storage = getSingleton(storageKey, () => new AsyncLocalStorage()); var tasks = getSingleton(tasksKey, () => /* @__PURE__ */ new Set()); export { currentContext, requireContext, runAsync, runningTasks }; //# sourceMappingURL=async.mjs.map