| 1 | import type { Context } from 'react'
|
|---|
| 2 | import { React } from '../utils/react'
|
|---|
| 3 | import type { Action, Store, UnknownAction } from 'redux'
|
|---|
| 4 | import type { Subscription } from '../utils/Subscription'
|
|---|
| 5 | import type { ProviderProps } from './Provider'
|
|---|
| 6 |
|
|---|
| 7 | export interface ReactReduxContextValue<
|
|---|
| 8 | SS = any,
|
|---|
| 9 | A extends Action<string> = UnknownAction,
|
|---|
| 10 | > extends Pick<ProviderProps, 'stabilityCheck' | 'identityFunctionCheck'> {
|
|---|
| 11 | store: Store<SS, A>
|
|---|
| 12 | subscription: Subscription
|
|---|
| 13 | getServerState?: () => SS
|
|---|
| 14 | }
|
|---|
| 15 |
|
|---|
| 16 | const ContextKey = /* @__PURE__ */ Symbol.for(`react-redux-context`)
|
|---|
| 17 | const gT: {
|
|---|
| 18 | [ContextKey]?: Map<
|
|---|
| 19 | typeof React.createContext,
|
|---|
| 20 | Context<ReactReduxContextValue | null>
|
|---|
| 21 | >
|
|---|
| 22 | } = (
|
|---|
| 23 | typeof globalThis !== 'undefined'
|
|---|
| 24 | ? globalThis
|
|---|
| 25 | : /* fall back to a per-module scope (pre-8.1 behaviour) if `globalThis` is not available */ {}
|
|---|
| 26 | ) as any
|
|---|
| 27 |
|
|---|
| 28 | function getContext(): Context<ReactReduxContextValue | null> {
|
|---|
| 29 | if (!React.createContext) return {} as any
|
|---|
| 30 |
|
|---|
| 31 | const contextMap = (gT[ContextKey] ??= new Map<
|
|---|
| 32 | typeof React.createContext,
|
|---|
| 33 | Context<ReactReduxContextValue | null>
|
|---|
| 34 | >())
|
|---|
| 35 | let realContext = contextMap.get(React.createContext)
|
|---|
| 36 | if (!realContext) {
|
|---|
| 37 | realContext = React.createContext<ReactReduxContextValue | null>(
|
|---|
| 38 | null as any,
|
|---|
| 39 | )
|
|---|
| 40 | if (process.env.NODE_ENV !== 'production') {
|
|---|
| 41 | realContext.displayName = 'ReactRedux'
|
|---|
| 42 | }
|
|---|
| 43 | contextMap.set(React.createContext, realContext)
|
|---|
| 44 | }
|
|---|
| 45 | return realContext
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | export const ReactReduxContext = /*#__PURE__*/ getContext()
|
|---|
| 49 |
|
|---|
| 50 | export type ReactReduxContextInstance = typeof ReactReduxContext
|
|---|