1
1
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/ban-ts-ignore */
2
2
3
- import React from 'react' ;
3
+ import React , {
4
+ createElement ,
5
+ createContext as createContextOrig ,
6
+ useCallback ,
7
+ useContext as useContextOrig ,
8
+ useMemo ,
9
+ useRef ,
10
+ } from 'react' ;
4
11
5
- const createMutableSource = ( React as any ) . createMutableSource as any ;
6
- const useMutableSource = ( React as any ) . useMutableSource as any ;
12
+ const createMutableSource = 'NOT_AVAILABLE_YET' as any ;
13
+ const useMutableSource = 'NOT_AVAILABLE_YET' as any ;
7
14
8
15
const SOURCE_SYMBOL = Symbol ( ) ;
9
16
@@ -12,16 +19,16 @@ type ContextValue<Value> = {
12
19
[ SOURCE_SYMBOL ] : any ;
13
20
} ;
14
21
15
- const createProvider = < Value > ( OrigProvider : React . Provider < ContextValue < Value > > ) => {
22
+ const createProvider = < Value > ( ProviderOrig : React . Provider < ContextValue < Value > > ) => {
16
23
const Provider : React . FC < { value : Value } > = ( { value, children } ) => {
17
- const ref = React . useRef ( { value, listeners : new Set < ( ) => void > ( ) } ) ;
24
+ const ref = useRef ( { value, listeners : new Set < ( ) => void > ( ) } ) ;
18
25
ref . current . value = value ;
19
26
ref . current . listeners . forEach ( ( listener ) => listener ( ) ) ;
20
- const contextValue = React . useMemo ( ( ) => {
27
+ const contextValue = useMemo ( ( ) => {
21
28
const source = createMutableSource ( ref , ( ) => ref . current . value ) ;
22
29
return { [ SOURCE_SYMBOL ] : source } ;
23
30
} , [ ] ) ;
24
- return React . createElement ( OrigProvider , { value : contextValue } , children ) ;
31
+ return createElement ( ProviderOrig , { value : contextValue } , children ) ;
25
32
} ;
26
33
return React . memo ( Provider ) ;
27
34
} ;
@@ -40,7 +47,7 @@ export const createContext = <Value>(defaultValue: Value) => {
40
47
const source = createMutableSource ( { current : defaultValue } , {
41
48
getVersion : ( ) => defaultValue ,
42
49
} ) ;
43
- const context = React . createContext (
50
+ const context = createContextOrig (
44
51
{ [ SOURCE_SYMBOL ] : source } ,
45
52
) as unknown as React . Context < Value > ; // HACK typing
46
53
context . Provider = createProvider (
@@ -68,16 +75,16 @@ export const useContextSelector = <Value, Selected>(
68
75
context : React . Context < Value > ,
69
76
selector : ( value : Value ) => Selected ,
70
77
) => {
71
- const { [ SOURCE_SYMBOL ] : source } = React . useContext (
78
+ const { [ SOURCE_SYMBOL ] : source } = useContextOrig (
72
79
context ,
73
80
) as unknown as ContextValue < Value > ; // HACK typing
74
81
if ( ! source ) {
75
82
throw new Error ( 'useContextSelector requires special context' ) ;
76
83
}
77
- const getSnapshot = React . useCallback ( (
84
+ const getSnapshot = useCallback ( (
78
85
ref : React . MutableRefObject < { value : Value } > ,
79
86
) => selector ( ref . current . value ) , [ selector ] ) ;
80
- const subscribe = React . useCallback ( (
87
+ const subscribe = useCallback ( (
81
88
ref : React . MutableRefObject < { value : Value ; listeners : Set < ( ) => void > } > ,
82
89
callback : ( ) => void ,
83
90
) => {
0 commit comments