sanity
Version:
Sanity is a real-time content infrastructure with a scalable, hosted backend featuring a Graph Oriented Query Language (GROQ), asset pipelines and fast edge caches
39 lines (36 loc) • 1.69 kB
JavaScript
import { createContext } from "react";
import { SANITY_VERSION } from "./_chunks-es/version.js";
const MISSING_CONTEXT_HELP_URL = "https://www.sanity.io/help/missing-context-error";
function createGlobalScopedContext(key, defaultValue) {
const symbol = Symbol.for(key);
if (typeof document > "u" || typeof window < "u" && typeof window.navigator < "u" && window.navigator.userAgent.includes("jsdom")) {
const context = createContext(defaultValue);
return context.displayName = key, context;
}
if (!globalScope[symbol])
globalScope[symbol] = {
context: createContext(defaultValue),
version: SANITY_VERSION
};
else {
if (globalScope[symbol].version !== SANITY_VERSION)
throw new TypeError(`Duplicate instances of context "${key}" with incompatible versions detected: Expected ${SANITY_VERSION} but got ${globalScope[symbol].version}.
For more information, please visit ${MISSING_CONTEXT_HELP_URL}`);
warned.has(SANITY_VERSION) || (console.warn(`Duplicate instances of context "${key}" detected. This is likely a mistake and may cause unexpected behavior.
For more information, please visit ${MISSING_CONTEXT_HELP_URL}`), warned.add(SANITY_VERSION));
}
return globalScope[symbol].context;
}
const warned = /* @__PURE__ */ new Set();
function getGlobalScope() {
if (typeof globalThis < "u") return globalThis;
if (typeof window < "u") return window;
if (typeof self < "u") return self;
if (typeof global < "u") return global;
throw new Error("sanity: could not locate global scope");
}
const globalScope = getGlobalScope();
export {
createGlobalScopedContext as createContext
};
//# sourceMappingURL=_createContext.js.map