Advertisement
Guest User

Untitled

a guest
May 6th, 2023
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import "/_nuxt/node_modules/.cache/vite/client/deps/chunk-TFWDKVI3.js?v=bd4495ba";
  2.  
  3. // node_modules/unctx/dist/index.mjs
  4. function createContext(opts = {}) {
  5.   let currentInstance;
  6.   let isSingleton = false;
  7.   const checkConflict = (instance) => {
  8.     if (currentInstance && currentInstance !== instance) {
  9.       throw new Error("Context conflict");
  10.     }
  11.   };
  12.   let als;
  13.   if (opts.asyncContext) {
  14.     const _AsyncLocalStorage = opts.AsyncLocalStorage || globalThis.AsyncLocalStorage;
  15.     if (_AsyncLocalStorage) {
  16.       als = new _AsyncLocalStorage();
  17.     } else {
  18.       console.warn("[unctx] `AsyncLocalStorage` is not provided.");
  19.     }
  20.   }
  21.   const _getCurrentInstance = () => {
  22.     if (als && currentInstance === void 0) {
  23.       const instance = als.getStore();
  24.       if (instance !== void 0) {
  25.         return instance;
  26.       }
  27.     }
  28.     return currentInstance;
  29.   };
  30.   return {
  31.     use: () => {
  32.       const _instance = _getCurrentInstance();
  33.       if (_instance === void 0) {
  34.         throw new Error("Context is not available");
  35.       }
  36.       return _instance;
  37.     },
  38.     tryUse: () => {
  39.       return _getCurrentInstance();
  40.     },
  41.     set: (instance, replace) => {
  42.       if (!replace) {
  43.         checkConflict(instance);
  44.       }
  45.       currentInstance = instance;
  46.       isSingleton = true;
  47.     },
  48.     unset: () => {
  49.       currentInstance = void 0;
  50.       isSingleton = false;
  51.     },
  52.     call: (instance, callback) => {
  53.       checkConflict(instance);
  54.       currentInstance = instance;
  55.       try {
  56.         return als ? als.run(instance, callback) : callback();
  57.       } finally {
  58.         if (!isSingleton) {
  59.           currentInstance = void 0;
  60.         }
  61.       }
  62.     },
  63.     async callAsync(instance, callback) {
  64.       currentInstance = instance;
  65.       const onRestore = () => {
  66.         currentInstance = instance;
  67.       };
  68.       const onLeave = () => currentInstance === instance ? onRestore : void 0;
  69.       asyncHandlers.add(onLeave);
  70.       try {
  71.         const r = als ? als.run(instance, callback) : callback();
  72.         if (!isSingleton) {
  73.           currentInstance = void 0;
  74.         }
  75.         return await r;
  76.       } finally {
  77.         asyncHandlers.delete(onLeave);
  78.       }
  79.     }
  80.   };
  81. }
  82. function createNamespace(defaultOpts = {}) {
  83.   const contexts = {};
  84.   return {
  85.     get(key, opts = {}) {
  86.       if (!contexts[key]) {
  87.         contexts[key] = createContext({ ...defaultOpts, ...opts });
  88.       }
  89.       contexts[key];
  90.       return contexts[key];
  91.     }
  92.   };
  93. }
  94. var _globalThis = typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : typeof global !== "undefined" ? global : typeof window !== "undefined" ? window : {};
  95. var globalKey = "__unctx__";
  96. var defaultNamespace = _globalThis[globalKey] || (_globalThis[globalKey] = createNamespace());
  97. var getContext = (key, opts = {}) => defaultNamespace.get(key, opts);
  98. var useContext = (key, opts = {}) => getContext(key, opts).use;
  99. var asyncHandlersKey = "__unctx_async_handlers__";
  100. var asyncHandlers = _globalThis[asyncHandlersKey] || (_globalThis[asyncHandlersKey] = /* @__PURE__ */ new Set());
  101. function executeAsync(function_) {
  102.   const restores = [];
  103.   for (const leaveHandler of asyncHandlers) {
  104.     const restore2 = leaveHandler();
  105.     if (restore2) {
  106.       restores.push(restore2);
  107.     }
  108.   }
  109.   const restore = () => {
  110.     for (const restore2 of restores) {
  111.       restore2();
  112.     }
  113.   };
  114.   let awaitable = function_();
  115.   if (awaitable && typeof awaitable === "object" && "catch" in awaitable) {
  116.     awaitable = awaitable.catch((error) => {
  117.       restore();
  118.       throw error;
  119.     });
  120.   }
  121.   return [awaitable, restore];
  122. }
  123. function withAsyncContext(function_, transformed) {
  124.   if (!transformed) {
  125.     console.warn(
  126.       "[unctx] `withAsyncContext` needs transformation for async context support in",
  127.       function_,
  128.       "\n",
  129.       function_.toString()
  130.     );
  131.   }
  132.   return function_;
  133. }
  134. export {
  135.   createContext,
  136.   createNamespace,
  137.   defaultNamespace,
  138.   executeAsync,
  139.   getContext,
  140.   useContext,
  141.   withAsyncContext
  142. };
  143. //# sourceMappingURL=unctx.js.map
  144.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement