{"version":3,"file":"serverContext.cjs","names":[],"sources":["../../../src/server/serverContext.tsx"],"sourcesContent":["/**\n * Creates a new datastore for a given server context.\n * Attempts to closely mimic the `createContext` API.\n *\n * @example\n * const IntlayerServer = createServerContext<string | null>(null);\n *\n * <IntlayerServer value={locale}>\n *      {children}\n * </IntlayerServer>\n */\nimport react, { type FC, type PropsWithChildren, type ReactNode } from 'react';\n\ntype CacheType<T> = () => { value: T | undefined };\n\nconst cacheFallback = () => () => ({ value: undefined });\n\nexport const createServerContext = <T,>(defaultValue?: T): ServerContext<T> => {\n  throwInClient();\n\n  /** @ts-ignore remove error Property 'cache' does not exist on type 'typeof React'. */\n  const cache = react.cache<CacheType<T>> ?? cacheFallback;\n\n  const getCache = cache(() => ({\n    value: undefined,\n  }));\n\n  const Provider: FC<PropsWithChildren<{ value?: T }>> = ({\n    children,\n    value,\n  }) => {\n    getCache().value = value;\n    return children;\n  };\n\n  const ServerContext = Provider as ServerContext<T>;\n  ServerContext.Provider = Provider;\n  ServerContext.Consumer = (props) => {\n    const store = getCache();\n    return props.children(store ? store.value : defaultValue);\n  };\n  ServerContext._storage = getCache;\n  ServerContext._defaultValue = defaultValue;\n\n  return ServerContext;\n};\n\n/**\n * Fetches a value present in a given server context.\n * Attempts to closely mimic the `useContext` API.\n *\n * @example\n * getServerContext(IntlayerServer);\n */\nexport const getServerContext = <T,>({\n  _storage,\n  _defaultValue,\n}: ServerContext<T>) => {\n  // throwInClient();\n  const store = _storage();\n  if (!store) return _defaultValue;\n  return store.value;\n};\n\nexport type ServerContext<T> = FC<PropsWithChildren<{ value?: T }>> & {\n  Provider: FC<PropsWithChildren<{ value?: T }>>;\n  Consumer: FC<\n    PropsWithChildren<{ children: (context: T | undefined) => ReactNode }>\n  >;\n  _storage: () => { value: T | undefined };\n  _defaultValue: T | undefined;\n};\n\n/**\n * Throws if called within a client component environment.\n * Useful to help prevent mistakes.\n */\nconst throwInClient = (): undefined | never => {\n  // If window.document is defined we're in a client component\n  if (typeof window !== 'undefined') {\n    throw new Error(`createServerContext only works in Server Components`);\n  }\n};\n"],"mappings":";;;;;;;;;;;;;;;;;AAeA,MAAM,6BAA6B,EAAE,OAAO,OAAU;AAEtD,MAAa,uBAA2B,iBAAuC;CAC7E,cAAc;CAKd,MAAM,YAFQ,cAAM,SAAuB,cAErB,QAAQ,EAC5B,OAAO,OACT,EAAE;CAEF,MAAM,YAAkD,EACtD,UACA,YACI;EACJ,SAAS,CAAC,CAAC,QAAQ;EACnB,OAAO;CACT;CAEA,MAAM,gBAAgB;CACtB,cAAc,WAAW;CACzB,cAAc,YAAY,UAAU;EAClC,MAAM,QAAQ,SAAS;EACvB,OAAO,MAAM,SAAS,QAAQ,MAAM,QAAQ,YAAY;CAC1D;CACA,cAAc,WAAW;CACzB,cAAc,gBAAgB;CAE9B,OAAO;AACT;;;;;;;;AASA,MAAa,oBAAwB,EACnC,UACA,oBACsB;CAEtB,MAAM,QAAQ,SAAS;CACvB,IAAI,CAAC,OAAO,OAAO;CACnB,OAAO,MAAM;AACf;;;;;AAeA,MAAM,sBAAyC;CAE7C,IAAI,OAAO,WAAW,aACpB,MAAM,IAAI,MAAM,qDAAqD;AAEzE"}