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