UNPKG

@modern-js/runtime-utils

Version:

A Progressive React Framework for modern web development.

44 lines (43 loc) 1.2 kB
import * as ah from "async_hooks"; var createStorage = function() { var storage2; if (typeof ah.AsyncLocalStorage !== "undefined") { storage2 = new ah.AsyncLocalStorage(); } var run = function(context, cb) { if (!storage2) { throw new Error("Unable to use async_hook, please confirm the node version >= 12.17\n "); } return new Promise(function(resolve, reject) { storage2.run(context, function() { try { return resolve(cb()); } catch (error) { return reject(error); } }); }); }; var useContext = function() { if (!storage2) { throw new Error("Unable to use async_hook, please confirm the node version >= 12.17\n "); } var context = storage2 === null || storage2 === void 0 ? void 0 : storage2.getStore(); if (!context) { throw new Error("Can't call useContext out of scope, make sure @modern-js/runtime-utils is a single version in node_modules"); } return context; }; return { run, useContext }; }; var storage = createStorage(); var getAsyncLocalStorage = function() { return storage; }; export { getAsyncLocalStorage, storage };