47
enableUseDeferredValueInitialArg,
48
disableLegacyMode,
49
enableNoCloningMemoCache,
50
+ enableContextProfiling,
51
} from 'shared/ReactFeatureFlags';
52
import {
53
REACT_CONTEXT_TYPE,
82
ContinuousEventPriority,
83
higherEventPriority,
84
} from './ReactEventPriorities';
84
-import {readContext, checkIfContextChanged} from './ReactFiberNewContext';
85
+import {
86
+ readContext,
87
+ readContextAndCompare,
88
+ checkIfContextChanged,
89
+} from './ReactFiberNewContext';
90
import {HostRoot, CacheComponent, HostComponent} from './ReactWorkTags';
91
import {
92
LayoutStatic as LayoutStaticEffect,
1058
return workInProgressHook;
1059
}
1060
1061
+function unstable_useContextWithBailout<T>(
1062
+ context: ReactContext<T>,
1063
+ select: (T => Array<mixed>) | null,
1064
+): T {
1065
+ if (select === null) {
1066
+ return readContext(context);
1067
+ }
1068
+ return readContextAndCompare(context, select);
1069
+}
1070
+
1071
// NOTE: defining two versions of this function to avoid size impact when this feature is disabled.
1072
// Previously this function was inlined, the additional `memoCache` property makes it not inlined.
1073
let createFunctionComponentUpdateQueue: () => FunctionComponentUpdateQueue;
3704
if (enableAsyncActions) {
3705
(ContextOnlyDispatcher: Dispatcher).useOptimistic = throwInvalidHookError;
3706
}
3707
+if (enableContextProfiling) {
3708
+ (ContextOnlyDispatcher: Dispatcher).unstable_useContextWithBailout =
3709
+ throwInvalidHookError;
3710
+}
3711
3712
const HooksDispatcherOnMount: Dispatcher = {
3713
readContext,
3747
if (enableAsyncActions) {
3748
(HooksDispatcherOnMount: Dispatcher).useOptimistic = mountOptimistic;
3749
}
3750
+if (enableContextProfiling) {
3751
+ (HooksDispatcherOnMount: Dispatcher).unstable_useContextWithBailout =
3752
+ unstable_useContextWithBailout;
3753
+}
3754
3755
const HooksDispatcherOnUpdate: Dispatcher = {
3756
readContext,
3790
if (enableAsyncActions) {
3791
(HooksDispatcherOnUpdate: Dispatcher).useOptimistic = updateOptimistic;
3792
}
3793
+if (enableContextProfiling) {
3794
+ (HooksDispatcherOnUpdate: Dispatcher).unstable_useContextWithBailout =
3795
+ unstable_useContextWithBailout;
3796
+}
3797
3798
const HooksDispatcherOnRerender: Dispatcher = {
3799
readContext,
3833
if (enableAsyncActions) {
3834
(HooksDispatcherOnRerender: Dispatcher).useOptimistic = rerenderOptimistic;
3835
}
3836
+if (enableContextProfiling) {
3837
+ (HooksDispatcherOnRerender: Dispatcher).unstable_useContextWithBailout =
3838
+ unstable_useContextWithBailout;
3839
+}
3840
3841
let HooksDispatcherOnMountInDEV: Dispatcher | null = null;
3842
let HooksDispatcherOnMountWithHookTypesInDEV: Dispatcher | null = null;
4050
return mountOptimistic(passthrough, reducer);
4051
};
4052
}
4053
+ if (enableContextProfiling) {
4054
+ (HooksDispatcherOnMountInDEV: Dispatcher).unstable_useContextWithBailout =
4055
+ function <T>(
4056
+ context: ReactContext<T>,
4057
+ select: (T => Array<mixed>) | null,
4058
+ ): T {
4059
+ currentHookNameInDev = 'useContext';
4060
+ mountHookTypesDev();
4061
+ return unstable_useContextWithBailout(context, select);
4062
+ };
4063
+ }
4064
4065
HooksDispatcherOnMountWithHookTypesInDEV = {
4066
readContext<T>(context: ReactContext<T>): T {
4242
return mountOptimistic(passthrough, reducer);
4243
};
4244
}
4245
+ if (enableContextProfiling) {
4246
+ (HooksDispatcherOnMountWithHookTypesInDEV: Dispatcher).unstable_useContextWithBailout =
4247
+ function <T>(
4248
+ context: ReactContext<T>,
4249
+ select: (T => Array<mixed>) | null,
4250
+ ): T {
4251
+ currentHookNameInDev = 'useContext';
4252
+ updateHookTypesDev();
4253
+ return unstable_useContextWithBailout(context, select);
4254
+ };
4255
+ }
4256
4257
HooksDispatcherOnUpdateInDEV = {
4258
readContext<T>(context: ReactContext<T>): T {
4433
return updateOptimistic(passthrough, reducer);
4434
};
4435
}
4436
+ if (enableContextProfiling) {
4437
+ (HooksDispatcherOnUpdateInDEV: Dispatcher).unstable_useContextWithBailout =
4438
+ function <T>(
4439
+ context: ReactContext<T>,
4440
+ select: (T => Array<mixed>) | null,
4441
+ ): T {
4442
+ currentHookNameInDev = 'useContext';
4443
+ updateHookTypesDev();
4444
+ return unstable_useContextWithBailout(context, select);
4445
+ };
4446
+ }
4447
4448
HooksDispatcherOnRerenderInDEV = {
4449
readContext<T>(context: ReactContext<T>): T {
4624
return rerenderOptimistic(passthrough, reducer);
4625
};
4626
}
4627
+ if (enableContextProfiling) {
4628
+ (HooksDispatcherOnUpdateInDEV: Dispatcher).unstable_useContextWithBailout =
4629
+ function <T>(
4630
+ context: ReactContext<T>,
4631
+ select: (T => Array<mixed>) | null,
4632
+ ): T {
4633
+ currentHookNameInDev = 'useContext';
4634
+ updateHookTypesDev();
4635
+ return unstable_useContextWithBailout(context, select);
4636
+ };
4637
+ }
4638
4639
InvalidNestedHooksDispatcherOnMountInDEV = {
4640
readContext<T>(context: ReactContext<T>): T {
4841
return mountOptimistic(passthrough, reducer);
4842
};
4843
}
4844
+ if (enableContextProfiling) {
4845
+ (HooksDispatcherOnUpdateInDEV: Dispatcher).unstable_useContextWithBailout =
4846
+ function <T>(
4847
+ context: ReactContext<T>,
4848
+ select: (T => Array<mixed>) | null,
4849
+ ): T {
4850
+ currentHookNameInDev = 'useContext';
4851
+ warnInvalidHookAccess();
4852
+ mountHookTypesDev();
4853
+ return unstable_useContextWithBailout(context, select);
4854
+ };
4855
+ }
4856
4857
InvalidNestedHooksDispatcherOnUpdateInDEV = {
4858
readContext<T>(context: ReactContext<T>): T {
5059
return updateOptimistic(passthrough, reducer);
5060
};
5061
}
5062
+ if (enableContextProfiling) {
5063
+ (InvalidNestedHooksDispatcherOnUpdateInDEV: Dispatcher).unstable_useContextWithBailout =
5064
+ function <T>(
5065
+ context: ReactContext<T>,
5066
+ select: (T => Array<mixed>) | null,
5067
+ ): T {
5068
+ currentHookNameInDev = 'useContext';
5069
+ warnInvalidHookAccess();
5070
+ updateHookTypesDev();
5071
+ return unstable_useContextWithBailout(context, select);
5072
+ };
5073
+ }
5074
5075
InvalidNestedHooksDispatcherOnRerenderInDEV = {
5076
readContext<T>(context: ReactContext<T>): T {
5277
return rerenderOptimistic(passthrough, reducer);
5278
};
5279
}
5280
+ if (enableContextProfiling) {
5281
+ (InvalidNestedHooksDispatcherOnRerenderInDEV: Dispatcher).unstable_useContextWithBailout =
5282
+ function <T>(
5283
+ context: ReactContext<T>,
5284
+ select: (T => Array<mixed>) | null,
5285
+ ): T {
5286
+ currentHookNameInDev = 'useContext';
5287
+ warnInvalidHookAccess();
5288
+ updateHookTypesDev();
5289
+ return unstable_useContextWithBailout(context, select);
5290
+ };
5291
+ }
5292
}