acuminous/x-pool

View on GitHub
lib/context.js

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
const { AsyncLocalStorage } = require('node:async_hooks');

const als = new AsyncLocalStorage();

let index = 0;

function runInContext(fn) {
  const contextId = als.getStore() || ++index;
  return als.run(contextId, fn);
}

function getContextId() {
  return als.getStore();
}

module.exports = {
  runInContext,
  getContextId,
};