/
home
/
henzagold
/
site
/
node_modules
/
unctx
/
dist
/
File Upload :
llllll
Current File: /home/henzagold/site/node_modules/unctx/dist/index.mjs
function createContext() { let currentInstance = null; let isSingleton = false; const checkConflict = (instance) => { if (currentInstance && currentInstance !== instance) { throw new Error("Context conflict"); } }; return { use: () => { if (currentInstance == null) { throw new Error("Context is not available"); } return currentInstance; }, tryUse: () => { return currentInstance; }, set: (instance, replace) => { if (!replace) { checkConflict(instance); } currentInstance = instance; isSingleton = true; }, unset: () => { currentInstance = null; isSingleton = false; }, call: (instance, cb) => { checkConflict(instance); currentInstance = instance; try { return cb(); } finally { if (!isSingleton) { currentInstance = null; } } }, async callAsync(instance, cb) { currentInstance = instance; const onRestore = () => { currentInstance = instance; }; const onLeave = () => currentInstance === instance ? onRestore : void 0; asyncHandlers.add(onLeave); try { const r = cb(); if (!isSingleton) { currentInstance = null; } return await r; } finally { asyncHandlers.delete(onLeave); } } }; } function createNamespace() { const contexts = {}; return { get(key) { if (!contexts[key]) { contexts[key] = createContext(); } contexts[key]; return contexts[key]; } }; } const _globalThis = typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : typeof global !== "undefined" ? global : typeof window !== "undefined" ? window : {}; const globalKey = "__unctx__"; const defaultNamespace = _globalThis[globalKey] || (_globalThis[globalKey] = createNamespace()); const getContext = (key) => defaultNamespace.get(key); const useContext = (key) => getContext(key).use; const asyncHandlersKey = "__unctx_async_handlers__"; const asyncHandlers = _globalThis[asyncHandlersKey] || (_globalThis[asyncHandlersKey] = /* @__PURE__ */ new Set()); function executeAsync(fn) { const restores = []; for (const leaveHandler of asyncHandlers) { const restore2 = leaveHandler(); if (restore2) { restores.push(restore2); } } const restore = () => { for (const restore2 of restores) { restore2(); } }; let awaitable = fn(); if ("catch" in awaitable) { awaitable = awaitable.catch((e) => { restore(); throw e; }); } return [awaitable, restore]; } function withAsyncContext(fn, transformed) { if (!transformed) { console.warn("[unctx] `withAsyncContext` needs transformation for async context support in", fn, "\n", fn.toString()); } return fn; } export { createContext, createNamespace, defaultNamespace, executeAsync, getContext, useContext, withAsyncContext };
Copyright ©2k19 -
Hexid
|
Tex7ure