UNPKG

@plugjs/plug

Version:
65 lines (63 loc) 2.29 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // async.ts var async_exports = {}; __export(async_exports, { currentContext: () => currentContext, requireContext: () => requireContext, runAsync: () => runAsync, runningTasks: () => runningTasks }); module.exports = __toCommonJS(async_exports); var import_node_async_hooks = require("node:async_hooks"); var import_asserts = require("./asserts.cjs"); var import_singleton = require("./utils/singleton.cjs"); 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(); (0, import_asserts.assert)(context, "Unable to retrieve current context"); return context; } function runningTasks() { return [...tasks].sort(); } var storageKey = /* @__PURE__ */ Symbol.for("plugjs:plug:singleton:contextStorage"); var tasksKey = /* @__PURE__ */ Symbol.for("plugjs:plug:singleton:runningTasksStorage"); var storage = (0, import_singleton.getSingleton)(storageKey, () => new import_node_async_hooks.AsyncLocalStorage()); var tasks = (0, import_singleton.getSingleton)(tasksKey, () => /* @__PURE__ */ new Set()); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { currentContext, requireContext, runAsync, runningTasks }); //# sourceMappingURL=async.cjs.map