@@ -21,6 +21,7 @@ import type {
21
21
Dispatcher ,
22
22
HookType ,
23
23
MemoCache ,
24
+ EventFunctionWrapper ,
24
25
} from './ReactInternalTypes' ;
25
26
import type { Lanes , Lane } from './ReactFiberLane.new' ;
26
27
import type { HookFlags } from './ReactHookEffectTags' ;
@@ -86,7 +87,6 @@ import {
86
87
Layout as HookLayout ,
87
88
Passive as HookPassive ,
88
89
Insertion as HookInsertion ,
89
- Snapshot as HookSnapshot ,
90
90
} from './ReactHookEffectTags' ;
91
91
import {
92
92
getWorkInProgressRoot ,
@@ -184,6 +184,7 @@ type StoreConsistencyCheck<T> = {
184
184
185
185
export type FunctionComponentUpdateQueue = {
186
186
lastEffect : Effect | null ,
187
+ events : Array < ( ) => mixed > | null ,
187
188
stores : Array < StoreConsistencyCheck < any >> | null ,
188
189
// NOTE: optional, only set when enableUseMemoCacheHook is enabled
189
190
memoCache ?: MemoCache | null ,
@@ -727,6 +728,7 @@ if (enableUseMemoCacheHook) {
727
728
createFunctionComponentUpdateQueue = ( ) => {
728
729
return {
729
730
lastEffect : null ,
731
+ events : null ,
730
732
stores : null ,
731
733
memoCache : null ,
732
734
} ;
@@ -735,6 +737,7 @@ if (enableUseMemoCacheHook) {
735
737
createFunctionComponentUpdateQueue = ( ) => {
736
738
return {
737
739
lastEffect : null ,
740
+ events : null ,
738
741
stores : null ,
739
742
} ;
740
743
} ;
@@ -1871,49 +1874,52 @@ function updateEffect(
1871
1874
return updateEffectImpl ( PassiveEffect , HookPassive , create , deps ) ;
1872
1875
}
1873
1876
1874
- function mountEvent< T > (callback: () => T ) : ( ) => T {
1875
- const hook = mountWorkInProgressHook ( ) ;
1876
- const ref = { current : callback } ;
1877
+ function useEventImpl< Args , Return , F : ( ...Array < Args > ) => Return > (
1878
+ event : EventFunctionWrapper < Args , Return , F > ,
1879
+ nextImpl: F,
1880
+ ) {
1881
+ currentlyRenderingFiber . flags |= UpdateEffect ;
1882
+ let componentUpdateQueue : null | FunctionComponentUpdateQueue = ( currentlyRenderingFiber . updateQueue : any ) ;
1883
+ if ( componentUpdateQueue === null ) {
1884
+ componentUpdateQueue = createFunctionComponentUpdateQueue ( ) ;
1885
+ currentlyRenderingFiber . updateQueue = ( componentUpdateQueue : any ) ;
1886
+ componentUpdateQueue . events = [ event , nextImpl ] ;
1887
+ } else {
1888
+ const events = componentUpdateQueue . events ;
1889
+ if ( events === null ) {
1890
+ componentUpdateQueue . events = [ event , nextImpl ] ;
1891
+ } else {
1892
+ events . push ( event , nextImpl ) ;
1893
+ }
1894
+ }
1895
+ }
1877
1896
1878
- function event ( ) {
1897
+ function mountEvent < Args , Return , F : ( ...Array < Args > ) => Return > (
1898
+ callback : F ,
1899
+ ) : EventFunctionWrapper < Args , Return , F > {
1900
+ const hook = mountWorkInProgressHook ( ) ;
1901
+ const eventFn : EventFunctionWrapper < Args , Return , F > = function eventFn ( ) {
1879
1902
if ( isInvalidExecutionContextForEventFunction ( ) ) {
1880
1903
throw new Error (
1881
1904
"A function wrapped in useEvent can't be called during rendering." ,
1882
1905
) ;
1883
1906
}
1884
- return ref . current . apply ( undefined , arguments ) ;
1885
- }
1886
-
1887
- // TODO: We don't need all the overhead of an effect object since there are no deps and no
1888
- // clean up functions.
1889
- mountEffectImpl (
1890
- UpdateEffect ,
1891
- HookSnapshot ,
1892
- ( ) => {
1893
- ref . current = callback ;
1894
- } ,
1895
- [ ref , callback ] ,
1896
- ) ;
1897
-
1898
- hook . memoizedState = [ ref , event ] ;
1907
+ return eventFn . _impl . apply ( undefined , arguments ) ;
1908
+ } ;
1909
+ eventFn . _impl = callback ;
1899
1910
1900
- return event ;
1911
+ useEventImpl ( eventFn , callback ) ;
1912
+ hook . memoizedState = eventFn ;
1913
+ return eventFn ;
1901
1914
}
1902
1915
1903
- function updateEvent< T > (callback: () => T ) : ( ) => T {
1916
+ function updateEvent< Args , Return , F : ( ...Array < Args > ) => Return > (
1917
+ callback : F ,
1918
+ ) : EventFunctionWrapper < Args , Return , F > {
1904
1919
const hook = updateWorkInProgressHook ( ) ;
1905
- const ref = hook . memoizedState [ 0 ] ;
1906
-
1907
- updateEffectImpl (
1908
- UpdateEffect ,
1909
- HookSnapshot ,
1910
- ( ) => {
1911
- ref . current = callback ;
1912
- } ,
1913
- [ ref , callback ] ,
1914
- ) ;
1915
-
1916
- return hook . memoizedState [ 1 ] ;
1920
+ const eventFn = hook . memoizedState ;
1921
+ useEventImpl ( eventFn , callback ) ;
1922
+ return eventFn ;
1917
1923
}
1918
1924
1919
1925
function mountInsertionEffect(
@@ -2890,9 +2896,11 @@ if (__DEV__) {
2890
2896
( HooksDispatcherOnMountInDEV : Dispatcher ) . useMemoCache = useMemoCache ;
2891
2897
}
2892
2898
if (enableUseEventHook) {
2893
- ( HooksDispatcherOnMountInDEV : Dispatcher ) . useEvent = function useEvent < T > (
2894
- callback: () => T ,
2895
- ) : ( ) => T {
2899
+ ( HooksDispatcherOnMountInDEV : Dispatcher ) . useEvent = function useEvent <
2900
+ Args ,
2901
+ Return ,
2902
+ F : ( ...Array < Args > ) => Return ,
2903
+ > ( callback : F ) : EventFunctionWrapper < Args , Return , F > {
2896
2904
currentHookNameInDev = 'useEvent' ;
2897
2905
mountHookTypesDev ( ) ;
2898
2906
return mountEvent ( callback ) ;
@@ -3048,8 +3056,10 @@ if (__DEV__) {
3048
3056
}
3049
3057
if (enableUseEventHook) {
3050
3058
( HooksDispatcherOnMountWithHookTypesInDEV : Dispatcher ) . useEvent = function useEvent <
3051
- T ,
3052
- > ( callback : ( ) = > T ) : ( ) = > T {
3059
+ Args ,
3060
+ Return ,
3061
+ F : ( ...Array < Args > ) => Return ,
3062
+ > ( callback : F ) : EventFunctionWrapper < Args , Return , F > {
3053
3063
currentHookNameInDev = 'useEvent' ;
3054
3064
updateHookTypesDev ( ) ;
3055
3065
return mountEvent ( callback ) ;
@@ -3204,9 +3214,11 @@ if (__DEV__) {
3204
3214
( HooksDispatcherOnUpdateInDEV : Dispatcher ) . useMemoCache = useMemoCache ;
3205
3215
}
3206
3216
if (enableUseEventHook) {
3207
- ( HooksDispatcherOnUpdateInDEV : Dispatcher ) . useEvent = function useEvent < T > (
3208
- callback: () => T ,
3209
- ) : ( ) => T {
3217
+ ( HooksDispatcherOnUpdateInDEV : Dispatcher ) . useEvent = function useEvent <
3218
+ Args ,
3219
+ Return ,
3220
+ F : ( ...Array < Args > ) => Return ,
3221
+ > ( callback : F ) : EventFunctionWrapper < Args , Return , F > {
3210
3222
currentHookNameInDev = 'useEvent' ;
3211
3223
updateHookTypesDev ( ) ;
3212
3224
return updateEvent ( callback ) ;
@@ -3363,8 +3375,10 @@ if (__DEV__) {
3363
3375
}
3364
3376
if (enableUseEventHook) {
3365
3377
( HooksDispatcherOnRerenderInDEV : Dispatcher ) . useEvent = function useEvent <
3366
- T ,
3367
- > ( callback : ( ) = > T ) : ( ) = > T {
3378
+ Args ,
3379
+ Return ,
3380
+ F : ( ...Array < Args > ) => Return ,
3381
+ > ( callback : F ) : EventFunctionWrapper < Args , Return , F > {
3368
3382
currentHookNameInDev = 'useEvent' ;
3369
3383
updateHookTypesDev ( ) ;
3370
3384
return updateEvent ( callback ) ;
@@ -3547,8 +3561,10 @@ if (__DEV__) {
3547
3561
}
3548
3562
if ( enableUseEventHook ) {
3549
3563
( InvalidNestedHooksDispatcherOnMountInDEV : Dispatcher ) . useEvent = function useEvent <
3550
- T ,
3551
- > ( callback : ( ) = > T ) : ( ) = > T {
3564
+ Args ,
3565
+ Return ,
3566
+ F : ( ...Array < Args > ) => Return ,
3567
+ > ( callback : F ) : EventFunctionWrapper < Args , Return , F > {
3552
3568
currentHookNameInDev = 'useEvent' ;
3553
3569
warnInvalidHookAccess ( ) ;
3554
3570
mountHookTypesDev ( ) ;
@@ -3732,8 +3748,10 @@ if (__DEV__) {
3732
3748
}
3733
3749
if ( enableUseEventHook ) {
3734
3750
( InvalidNestedHooksDispatcherOnUpdateInDEV : Dispatcher ) . useEvent = function useEvent <
3735
- T ,
3736
- > ( callback : ( ) = > T ) : ( ) = > T {
3751
+ Args ,
3752
+ Return ,
3753
+ F : ( ...Array < Args > ) => Return ,
3754
+ > ( callback : F ) : EventFunctionWrapper < Args , Return , F > {
3737
3755
currentHookNameInDev = 'useEvent' ;
3738
3756
warnInvalidHookAccess ( ) ;
3739
3757
updateHookTypesDev ( ) ;
@@ -3918,8 +3936,10 @@ if (__DEV__) {
3918
3936
}
3919
3937
if ( enableUseEventHook ) {
3920
3938
( InvalidNestedHooksDispatcherOnRerenderInDEV : Dispatcher ) . useEvent = function useEvent <
3921
- T ,
3922
- > ( callback : ( ) = > T ) : ( ) = > T {
3939
+ Args ,
3940
+ Return ,
3941
+ F : ( ...Array < Args > ) => Return ,
3942
+ > ( callback : F ) : EventFunctionWrapper < Args , Return , F > {
3923
3943
currentHookNameInDev = 'useEvent' ;
3924
3944
warnInvalidHookAccess ( ) ;
3925
3945
updateHookTypesDev ( ) ;
0 commit comments