-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
Context.ts
49 lines (43 loc) · 1.39 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
import { createContext, version as ReactVersion } from 'react'
import type { Context } from 'react'
import type { Action, AnyAction, Store } from 'redux'
import type { Subscription } from '../utils/Subscription'
import type { CheckFrequency } from '../hooks/useSelector'
export interface ReactReduxContextValue<
SS = any,
A extends Action = AnyAction
> {
store: Store<SS, A>
subscription: Subscription
getServerState?: () => SS
stabilityCheck: CheckFrequency
noopCheck: CheckFrequency
}
const ContextKey = Symbol.for(`react-redux-context-${ReactVersion}`)
const gT = globalThis as { [ContextKey]?: Context<ReactReduxContextValue> }
function getContext() {
let realContext = gT[ContextKey]
if (!realContext) {
realContext = createContext<ReactReduxContextValue>(null as any)
if (process.env.NODE_ENV !== 'production') {
realContext.displayName = 'ReactRedux'
}
gT[ContextKey] = realContext
}
return realContext
}
export const ReactReduxContext = /*#__PURE__*/ new Proxy(
{} as Context<ReactReduxContextValue>,
/*#__PURE__*/ new Proxy<ProxyHandler<Context<ReactReduxContextValue>>>(
{},
{
get(_, handler) {
const target = getContext()
// @ts-ignore
return (_target, ...args) => Reflect[handler](target, ...args)
},
}
)
)
export type ReactReduxContextInstance = typeof ReactReduxContext
export default ReactReduxContext