@@ -22,7 +22,6 @@ import type {
22
22
Dispatcher ,
23
23
HookType ,
24
24
MemoCache ,
25
- EventFunctionWrapper ,
26
25
} from './ReactInternalTypes' ;
27
26
import type { Lanes , Lane } from './ReactFiberLane.new' ;
28
27
import type { HookFlags } from './ReactHookEffectTags' ;
@@ -189,9 +188,17 @@ type StoreConsistencyCheck<T> = {
189
188
getSnapshot : ( ) => T ,
190
189
} ;
191
190
191
+ type EventFunctionPayload < Args , Return , F : ( ...Array < Args > ) => Return > = {
192
+ ref : {
193
+ eventFn : F ,
194
+ impl : F ,
195
+ } ,
196
+ nextImpl : F ,
197
+ } ;
198
+
192
199
export type FunctionComponentUpdateQueue = {
193
200
lastEffect : Effect | null ,
194
- events : Array < ( ) => mixed > | null ,
201
+ events : Array < EventFunctionPayload < any , any , any > > | null ,
195
202
stores : Array < StoreConsistencyCheck < any >> | null ,
196
203
// NOTE: optional, only set when enableUseMemoCacheHook is enabled
197
204
memoCache ?: MemoCache | null ,
@@ -1909,58 +1916,56 @@ function updateEffect(
1909
1916
}
1910
1917
1911
1918
function useEventImpl< Args , Return , F : ( ...Array < Args > ) => Return > (
1912
- event : EventFunctionWrapper < Args , Return , F > ,
1913
- nextImpl: F,
1919
+ payload : EventFunctionPayload < Args , Return , F > ,
1914
1920
) {
1915
1921
currentlyRenderingFiber . flags |= UpdateEffect ;
1916
1922
let componentUpdateQueue : null | FunctionComponentUpdateQueue = ( currentlyRenderingFiber . updateQueue : any ) ;
1917
1923
if ( componentUpdateQueue === null ) {
1918
1924
componentUpdateQueue = createFunctionComponentUpdateQueue ( ) ;
1919
1925
currentlyRenderingFiber . updateQueue = ( componentUpdateQueue : any ) ;
1920
- componentUpdateQueue . events = [ event , nextImpl ] ;
1926
+ componentUpdateQueue . events = [ payload ] ;
1921
1927
} else {
1922
1928
const events = componentUpdateQueue . events ;
1923
1929
if ( events === null ) {
1924
- componentUpdateQueue . events = [ event , nextImpl ] ;
1930
+ componentUpdateQueue . events = [ payload ] ;
1925
1931
} else {
1926
- events . push ( event , nextImpl ) ;
1932
+ events . push ( payload ) ;
1927
1933
}
1928
1934
}
1929
1935
}
1930
1936
1931
- function wrapEventFunction < Args , Return , F : ( ...Array < Args > ) => Return > (
1937
+ function mountEvent < Args , Return , F : ( ...Array < Args > ) => Return > (
1932
1938
callback : F ,
1933
- ) : EventFunctionWrapper < Args , Return , F > {
1934
- const eventFn : EventFunctionWrapper < Args , Return , F > = function eventFn ( ) {
1939
+ ) : F {
1940
+ const hook = mountWorkInProgressHook ( ) ;
1941
+ const ref = { impl : callback } ;
1942
+ hook . memoizedState = ref ;
1943
+ // $FlowIgnore[incompatible-return]
1944
+ return function eventFn ( ) {
1935
1945
if ( isInvalidExecutionContextForEventFunction ( ) ) {
1936
1946
throw new Error (
1937
1947
"A function wrapped in useEvent can't be called during rendering." ,
1938
1948
) ;
1939
1949
}
1940
- // $FlowFixMe[prop-missing] found when upgrading Flow
1941
- return eventFn . _impl . apply ( undefined , arguments ) ;
1950
+ return ref . impl . apply ( undefined , arguments ) ;
1942
1951
} ;
1943
- eventFn . _impl = callback ;
1944
- return eventFn ;
1945
- }
1946
-
1947
- function mountEvent< Args , Return , F : ( ...Array < Args > ) => Return > (
1948
- callback : F ,
1949
- ) : EventFunctionWrapper < Args , Return , F > {
1950
- const hook = mountWorkInProgressHook ( ) ;
1951
- const eventFn = wrapEventFunction ( callback ) ;
1952
- hook . memoizedState = eventFn ;
1953
- return eventFn ;
1954
1952
}
1955
1953
1956
1954
function updateEvent< Args , Return , F : ( ...Array < Args > ) => Return > (
1957
1955
callback : F ,
1958
- ) : EventFunctionWrapper < Args , Return , F > {
1956
+ ) : F {
1959
1957
const hook = updateWorkInProgressHook ( ) ;
1960
- const eventFn = hook . memoizedState ;
1961
- useEventImpl ( eventFn , callback ) ;
1962
- // Always return a new function
1963
- return wrapEventFunction ( callback ) ;
1958
+ const ref = hook . memoizedState ;
1959
+ useEventImpl ( { ref, nextImpl : callback } ) ;
1960
+ // $FlowIgnore[incompatible-return]
1961
+ return function eventFn ( ) {
1962
+ if ( isInvalidExecutionContextForEventFunction ( ) ) {
1963
+ throw new Error (
1964
+ "A function wrapped in useEvent can't be called during rendering." ,
1965
+ ) ;
1966
+ }
1967
+ return ref . impl . apply ( undefined , arguments ) ;
1968
+ } ;
1964
1969
}
1965
1970
1966
1971
function mountInsertionEffect(
@@ -2922,7 +2927,7 @@ if (__DEV__) {
2922
2927
Args ,
2923
2928
Return ,
2924
2929
F : ( ...Array < Args > ) => Return ,
2925
- > ( callback : F ) : EventFunctionWrapper < Args , Return , F > {
2930
+ > ( callback : F ) : F {
2926
2931
currentHookNameInDev = 'useEvent' ;
2927
2932
mountHookTypesDev ( ) ;
2928
2933
return mountEvent ( callback ) ;
@@ -3079,7 +3084,7 @@ if (__DEV__) {
3079
3084
Args ,
3080
3085
Return ,
3081
3086
F : ( ...Array < Args > ) => Return ,
3082
- > ( callback : F ) : EventFunctionWrapper < Args , Return , F > {
3087
+ > ( callback : F ) : F {
3083
3088
currentHookNameInDev = 'useEvent' ;
3084
3089
updateHookTypesDev ( ) ;
3085
3090
return mountEvent ( callback ) ;
@@ -3236,7 +3241,7 @@ if (__DEV__) {
3236
3241
Args ,
3237
3242
Return ,
3238
3243
F : ( ...Array < Args > ) => Return ,
3239
- > ( callback : F ) : EventFunctionWrapper < Args , Return , F > {
3244
+ > ( callback : F ) : F {
3240
3245
currentHookNameInDev = 'useEvent' ;
3241
3246
updateHookTypesDev ( ) ;
3242
3247
return updateEvent ( callback ) ;
@@ -3394,7 +3399,7 @@ if (__DEV__) {
3394
3399
Args ,
3395
3400
Return ,
3396
3401
F : ( ...Array < Args > ) => Return ,
3397
- > ( callback : F ) : EventFunctionWrapper < Args , Return , F > {
3402
+ > ( callback : F ) : F {
3398
3403
currentHookNameInDev = 'useEvent' ;
3399
3404
updateHookTypesDev ( ) ;
3400
3405
return updateEvent ( callback ) ;
@@ -3578,7 +3583,7 @@ if (__DEV__) {
3578
3583
Args ,
3579
3584
Return ,
3580
3585
F : ( ...Array < Args > ) => Return ,
3581
- > ( callback : F ) : EventFunctionWrapper < Args , Return , F > {
3586
+ > ( callback : F ) : F {
3582
3587
currentHookNameInDev = 'useEvent' ;
3583
3588
warnInvalidHookAccess ( ) ;
3584
3589
mountHookTypesDev ( ) ;
@@ -3763,7 +3768,7 @@ if (__DEV__) {
3763
3768
Args ,
3764
3769
Return ,
3765
3770
F : ( ...Array < Args > ) => Return ,
3766
- > ( callback : F ) : EventFunctionWrapper < Args , Return , F > {
3771
+ > ( callback : F ) : F {
3767
3772
currentHookNameInDev = 'useEvent' ;
3768
3773
warnInvalidHookAccess ( ) ;
3769
3774
updateHookTypesDev ( ) ;
@@ -3949,7 +3954,7 @@ if (__DEV__) {
3949
3954
Args ,
3950
3955
Return ,
3951
3956
F : ( ...Array < Args > ) => Return ,
3952
- > ( callback : F ) : EventFunctionWrapper < Args , Return , F > {
3957
+ > ( callback : F ) : F {
3953
3958
currentHookNameInDev = 'useEvent' ;
3954
3959
warnInvalidHookAccess ( ) ;
3955
3960
updateHookTypesDev ( ) ;
0 commit comments