@plugjs/plug
Version:
PlugJS Build System ===================
52 lines • 2.02 kB
JavaScript
import { AsyncLocalStorage } from 'node:async_hooks';
import { assert } from "./asserts.js";
import { getSingleton } from "./utils/singleton.js";
/* ========================================================================== *
* EXPORTED *
* ========================================================================== */
/**
* Run the specified `callback` associating the specified {@link Context} and task
* name with the current asynchronous invocation context.
*/
export function runAsync(context, callback) {
return storage.run(context, async () => {
try {
tasks.add(context.taskName);
return await callback();
}
finally {
tasks.delete(context.taskName);
}
});
}
/**
* Returns the {@link Context} associated with the current asynchronous
* invocation context or `undefined`.
*/
export function currentContext() {
return storage.getStore();
}
/**
* Returns the {@link Context} associated with the current asynchronous
* invocation context and fail if none was found.
*/
export function requireContext() {
const context = storage.getStore();
assert(context, 'Unable to retrieve current context');
return context;
}
/**
* Return an array of all _task names_ currently running
*/
export function runningTasks() {
return [...tasks].sort();
}
/* ========================================================================== *
* INTERNALS *
* ========================================================================== */
/* Storage and task names must be unique _per process_ */
const storageKey = Symbol.for('plugjs:plug:singleton:contextStorage');
const tasksKey = Symbol.for('plugjs:plug:singleton:runningTasksStorage');
const storage = getSingleton(storageKey, () => new AsyncLocalStorage());
const tasks = getSingleton(tasksKey, () => new Set());
//# sourceMappingURL=async.js.map