Skip to content

Commit

Permalink
Add flow types to ReactFiberHooks (#25752)
Browse files Browse the repository at this point in the history
Increase type coverage in ReactFiberHooks file.
  • Loading branch information
sammy-SC authored Dec 2, 2022
1 parent f0bba2d commit 17f6912
Showing 1 changed file with 47 additions and 34 deletions.
81 changes: 47 additions & 34 deletions packages/react-reconciler/src/ReactFiberHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ let hookTypesUpdateIndexDev: number = -1;
// When true, such Hooks will always be "remounted". Only used during hot reload.
let ignorePreviousDependencies: boolean = false;

function mountHookTypesDev() {
function mountHookTypesDev(): void {
if (__DEV__) {
const hookName = ((currentHookNameInDev: any): HookType);

Expand All @@ -280,7 +280,7 @@ function mountHookTypesDev() {
}
}

function updateHookTypesDev() {
function updateHookTypesDev(): void {
if (__DEV__) {
const hookName = ((currentHookNameInDev: any): HookType);

Expand All @@ -293,7 +293,7 @@ function updateHookTypesDev() {
}
}

function checkDepsAreArrayDev(deps: mixed) {
function checkDepsAreArrayDev(deps: mixed): void {
if (__DEV__) {
if (deps !== undefined && deps !== null && !isArray(deps)) {
// Verify deps, but only on mount to avoid extra checks.
Expand All @@ -308,7 +308,7 @@ function checkDepsAreArrayDev(deps: mixed) {
}
}

function warnOnHookMismatchInDev(currentHookName: HookType) {
function warnOnHookMismatchInDev(currentHookName: HookType): void {
if (__DEV__) {
const componentName = getComponentNameFromFiber(currentlyRenderingFiber);
if (!didWarnAboutMismatchedHooksForComponent.has(componentName)) {
Expand Down Expand Up @@ -369,7 +369,7 @@ function throwInvalidHookError() {
function areHookInputsEqual(
nextDeps: Array<mixed>,
prevDeps: Array<mixed> | null,
) {
): boolean {
if (__DEV__) {
if (ignorePreviousDependencies) {
// Only true when this component is being hot reloaded.
Expand Down Expand Up @@ -681,7 +681,7 @@ function renderWithHooksAgain<Props, SecondArg>(
Component: (p: Props, arg: SecondArg) => any,
props: Props,
secondArg: SecondArg,
) {
): any {
// This is used to perform another render pass. It's used when setState is
// called during render, and for double invoking components in Strict Mode
// during development.
Expand Down Expand Up @@ -745,7 +745,7 @@ export function bailoutHooks(
current: Fiber,
workInProgress: Fiber,
lanes: Lanes,
) {
): void {
workInProgress.updateQueue = current.updateQueue;
// TODO: Don't need to reset the flags here, because they're reset in the
// complete phase (bubbleProperties).
Expand Down Expand Up @@ -1732,7 +1732,7 @@ function pushStoreConsistencyCheck<T>(
fiber: Fiber,
getSnapshot: () => T,
renderedSnapshot: T,
) {
): void {
fiber.flags |= StoreConsistency;
const check: StoreConsistencyCheck<T> = {
getSnapshot,
Expand All @@ -1758,7 +1758,7 @@ function updateStoreInstance<T>(
inst: StoreInstance<T>,
nextSnapshot: T,
getSnapshot: () => T,
) {
): void {
// These are updated in the passive phase
inst.value = nextSnapshot;
inst.getSnapshot = getSnapshot;
Expand All @@ -1773,7 +1773,11 @@ function updateStoreInstance<T>(
}
}

function subscribeToStore<T>(fiber, inst: StoreInstance<T>, subscribe) {
function subscribeToStore<T>(
fiber: Fiber,
inst: StoreInstance<T>,
subscribe: (() => void) => () => void,
): any {
const handleStoreChange = () => {
// The store changed. Check if the snapshot changed since the last time we
// read from the store.
Expand Down Expand Up @@ -1843,7 +1847,12 @@ function rerenderState<S>(
return rerenderReducer(basicStateReducer, (initialState: any));
}

function pushEffect(tag, create, destroy, deps: Array<mixed> | void | null) {
function pushEffect(
tag: HookFlags,
create: () => (() => void) | void,
destroy: (() => void) | void,
deps: Array<mixed> | void | null,
): Effect {
const effect: Effect = {
tag,
create,
Expand Down Expand Up @@ -1963,9 +1972,9 @@ function updateRef<T>(initialValue: T): {current: T} {
}

function mountEffectImpl(
fiberFlags,
hookFlags,
create,
fiberFlags: Flags,
hookFlags: HookFlags,
create: () => (() => void) | void,
deps: Array<mixed> | void | null,
): void {
const hook = mountWorkInProgressHook();
Expand All @@ -1980,9 +1989,9 @@ function mountEffectImpl(
}

function updateEffectImpl(
fiberFlags,
hookFlags,
create,
fiberFlags: Flags,
hookFlags: HookFlags,
create: () => (() => void) | void,
deps: Array<mixed> | void | null,
): void {
const hook = updateWorkInProgressHook();
Expand Down Expand Up @@ -2019,14 +2028,14 @@ function mountEffect(
__DEV__ &&
(currentlyRenderingFiber.mode & StrictEffectsMode) !== NoMode
) {
return mountEffectImpl(
mountEffectImpl(
MountPassiveDevEffect | PassiveEffect | PassiveStaticEffect,
HookPassive,
create,
deps,
);
} else {
return mountEffectImpl(
mountEffectImpl(
PassiveEffect | PassiveStaticEffect,
HookPassive,
create,
Expand All @@ -2039,7 +2048,7 @@ function updateEffect(
create: () => (() => void) | void,
deps: Array<mixed> | void | null,
): void {
return updateEffectImpl(PassiveEffect, HookPassive, create, deps);
updateEffectImpl(PassiveEffect, HookPassive, create, deps);
}

function useEventImpl<Args, Return, F: (...Array<Args>) => Return>(
Expand Down Expand Up @@ -2099,7 +2108,7 @@ function mountInsertionEffect(
create: () => (() => void) | void,
deps: Array<mixed> | void | null,
): void {
return mountEffectImpl(UpdateEffect, HookInsertion, create, deps);
mountEffectImpl(UpdateEffect, HookInsertion, create, deps);
}

function updateInsertionEffect(
Expand Down Expand Up @@ -2133,7 +2142,7 @@ function updateLayoutEffect(
function imperativeHandleEffect<T>(
create: () => T,
ref: {current: T | null} | ((inst: T | null) => mixed) | null | void,
) {
): void | (() => void) {
if (typeof ref === 'function') {
const refCallback = ref;
const inst = create();
Expand Down Expand Up @@ -2186,7 +2195,7 @@ function mountImperativeHandle<T>(
) {
fiberFlags |= MountLayoutDevEffect;
}
return mountEffectImpl(
mountEffectImpl(
fiberFlags,
HookLayout,
imperativeHandleEffect.bind(null, create, ref),
Expand All @@ -2213,7 +2222,7 @@ function updateImperativeHandle<T>(
const effectDeps =
deps !== null && deps !== undefined ? deps.concat([ref]) : null;

return updateEffectImpl(
updateEffectImpl(
UpdateEffect,
HookLayout,
imperativeHandleEffect.bind(null, create, ref),
Expand Down Expand Up @@ -2361,7 +2370,11 @@ function updateDeferredValueImpl<T>(hook: Hook, prevValue: T, value: T): T {
}
}

function startTransition(setPending, callback, options) {
function startTransition(
setPending: boolean => void,
callback: () => void,
options?: StartTransitionOptions,
): void {
const previousPriority = getCurrentUpdatePriority();
setCurrentUpdatePriority(
higherEventPriority(previousPriority, ContinuousEventPriority),
Expand Down Expand Up @@ -2483,7 +2496,7 @@ function updateId(): string {
return id;
}

function mountRefresh() {
function mountRefresh(): any {
const hook = mountWorkInProgressHook();
const refresh = (hook.memoizedState = refreshCache.bind(
null,
Expand All @@ -2492,12 +2505,12 @@ function mountRefresh() {
return refresh;
}

function updateRefresh() {
function updateRefresh(): any {
const hook = updateWorkInProgressHook();
return hook.memoizedState;
}

function refreshCache<T>(fiber: Fiber, seedKey: ?() => T, seedValue: T) {
function refreshCache<T>(fiber: Fiber, seedKey: ?() => T, seedValue: T): void {
if (!enableCache) {
return;
}
Expand Down Expand Up @@ -2553,7 +2566,7 @@ function dispatchReducerAction<S, A>(
fiber: Fiber,
queue: UpdateQueue<S, A>,
action: A,
) {
): void {
if (__DEV__) {
if (typeof arguments[3] === 'function') {
console.error(
Expand Down Expand Up @@ -2592,7 +2605,7 @@ function dispatchSetState<S, A>(
fiber: Fiber,
queue: UpdateQueue<S, A>,
action: A,
) {
): void {
if (__DEV__) {
if (typeof arguments[3] === 'function') {
console.error(
Expand Down Expand Up @@ -2670,7 +2683,7 @@ function dispatchSetState<S, A>(
markUpdateInDevTools(fiber, lane, action);
}

function isRenderPhaseUpdate(fiber: Fiber) {
function isRenderPhaseUpdate(fiber: Fiber): boolean {
const alternate = fiber.alternate;
return (
fiber === currentlyRenderingFiber ||
Expand All @@ -2681,7 +2694,7 @@ function isRenderPhaseUpdate(fiber: Fiber) {
function enqueueRenderPhaseUpdate<S, A>(
queue: UpdateQueue<S, A>,
update: Update<S, A>,
) {
): void {
// This is a render phase update. Stash it in a lazily-created map of
// queue -> linked list of updates. After this render pass, we'll restart
// and apply the stashed updates on top of the work-in-progress hook.
Expand All @@ -2702,7 +2715,7 @@ function entangleTransitionUpdate<S, A>(
root: FiberRoot,
queue: UpdateQueue<S, A>,
lane: Lane,
) {
): void {
if (isTransitionLane(lane)) {
let queueLanes = queue.lanes;

Expand All @@ -2723,7 +2736,7 @@ function entangleTransitionUpdate<S, A>(
}
}

function markUpdateInDevTools<A>(fiber, lane, action: A) {
function markUpdateInDevTools<A>(fiber, lane, action: A): void {
if (__DEV__) {
if (enableDebugTracing) {
if (fiber.mode & DebugTracingMode) {
Expand Down

0 comments on commit 17f6912

Please sign in to comment.