Skip to content

Commit

Permalink
Assert that currentlyRenderingFiber is always set
Browse files Browse the repository at this point in the history
When accessed, this should always be set. This could enforced by storing
this on the dispatcher for example.
  • Loading branch information
sebmarkbage committed Nov 28, 2019
1 parent cc1aa9d commit e73d0f2
Showing 1 changed file with 19 additions and 28 deletions.
47 changes: 19 additions & 28 deletions packages/react-reconciler/src/ReactFiberHooks.js
Expand Up @@ -174,7 +174,7 @@ type Dispatch<A> = A => void;
let renderExpirationTime: ExpirationTime = NoWork;
// The work-in-progress fiber. I've named it differently to distinguish it from
// the work-in-progress hook.
let currentlyRenderingFiber: Fiber | null = null;
let currentlyRenderingFiber: Fiber = (null: any);

// Hooks are stored as a linked list on the fiber's memoizedState field. The
// current hook list is the list that belongs to the current fiber. The
Expand Down Expand Up @@ -259,7 +259,7 @@ function checkDepsAreArrayDev(deps: mixed) {
function warnOnHookMismatchInDev(currentHookName: HookType) {
if (__DEV__) {
const componentName = getComponentName(
((currentlyRenderingFiber: any): Fiber).type,
currentlyRenderingFiber.type,
);
if (!didWarnAboutMismatchedHooksForComponent.has(componentName)) {
didWarnAboutMismatchedHooksForComponent.add(componentName);
Expand Down Expand Up @@ -483,7 +483,7 @@ export function renderWithHooks(
currentHook !== null && currentHook.next !== null;

renderExpirationTime = NoWork;
currentlyRenderingFiber = null;
currentlyRenderingFiber = (null: any);

currentHook = null;
workInProgressHook = null;
Expand Down Expand Up @@ -529,7 +529,7 @@ export function resetHooks(): void {
// component is a module-style component.

renderExpirationTime = NoWork;
currentlyRenderingFiber = null;
currentlyRenderingFiber = (null: any);

currentHook = null;
workInProgressHook = null;
Expand Down Expand Up @@ -558,8 +558,7 @@ function mountWorkInProgressHook(): Hook {

if (workInProgressHook === null) {
// This is the first hook in the list
let fiber = ((currentlyRenderingFiber: any): Fiber);
fiber.memoizedState = workInProgressHook = hook;
currentlyRenderingFiber.memoizedState = workInProgressHook = hook;
} else {
// Append to the end of the list
workInProgressHook = workInProgressHook.next = hook;
Expand All @@ -575,8 +574,7 @@ function updateWorkInProgressHook(): Hook {
// the dispatcher used for mounts.
let nextCurrentHook: null | Hook;
if (currentHook === null) {
let fiber = ((currentlyRenderingFiber: any): Fiber);
let current = fiber.alternate;
let current = currentlyRenderingFiber.alternate;
if (current !== null) {
nextCurrentHook = current.memoizedState;
} else {
Expand All @@ -588,8 +586,7 @@ function updateWorkInProgressHook(): Hook {

let nextWorkInProgressHook: null | Hook;
if (workInProgressHook === null) {
let fiber = ((currentlyRenderingFiber: any): Fiber);
nextWorkInProgressHook = fiber.memoizedState;
nextWorkInProgressHook = currentlyRenderingFiber.memoizedState;
} else {
nextWorkInProgressHook = workInProgressHook.next;
}
Expand Down Expand Up @@ -621,8 +618,7 @@ function updateWorkInProgressHook(): Hook {

if (workInProgressHook === null) {
// This is the first hook in the list.
let fiber = ((currentlyRenderingFiber: any): Fiber);
fiber.memoizedState = workInProgressHook = newHook;
currentlyRenderingFiber.memoizedState = workInProgressHook = newHook;
} else {
// Append to the end of the list.
workInProgressHook = workInProgressHook.next = newHook;
Expand Down Expand Up @@ -662,8 +658,7 @@ function mountReducer<S, I, A>(
});
const dispatch: Dispatch<A> = (queue.dispatch = (dispatchAction.bind(
null,
// Flow doesn't know this is non-null, but we do.
((currentlyRenderingFiber: any): Fiber),
currentlyRenderingFiber,
queue,
): any));
return [hook.memoizedState, dispatch];
Expand Down Expand Up @@ -762,9 +757,8 @@ function updateReducer<S, I, A>(
newBaseState = newState;
}
// Update the remaining priority in the queue.
let fiber = ((currentlyRenderingFiber: any): Fiber);
if (updateExpirationTime > fiber.expirationTime) {
fiber.expirationTime = updateExpirationTime;
if (updateExpirationTime > currentlyRenderingFiber.expirationTime) {
currentlyRenderingFiber.expirationTime = updateExpirationTime;
markUnprocessedUpdateTime(updateExpirationTime);
}
} else {
Expand Down Expand Up @@ -835,8 +829,7 @@ function mountState<S>(
BasicStateAction<S>,
> = (queue.dispatch = (dispatchAction.bind(
null,
// Flow doesn't know this is non-null, but we do.
((currentlyRenderingFiber: any): Fiber),
currentlyRenderingFiber,
queue,
): any));
return [hook.memoizedState, dispatch];
Expand All @@ -857,10 +850,10 @@ function pushEffect(tag, create, destroy, deps) {
// Circular
next: (null: any),
};
let fiber = ((currentlyRenderingFiber: any): Fiber);
let componentUpdateQueue: null | FunctionComponentUpdateQueue = (fiber.updateQueue: any);
let componentUpdateQueue: null | FunctionComponentUpdateQueue = (currentlyRenderingFiber.updateQueue: any);
if (componentUpdateQueue === null) {
(fiber: any).updateQueue = componentUpdateQueue = createFunctionComponentUpdateQueue();
componentUpdateQueue = createFunctionComponentUpdateQueue();
currentlyRenderingFiber.updateQueue = (componentUpdateQueue: any);
componentUpdateQueue.lastEffect = effect.next = effect;
} else {
const lastEffect = componentUpdateQueue.lastEffect;
Expand Down Expand Up @@ -894,8 +887,7 @@ function updateRef<T>(initialValue: T): {current: T} {
function mountEffectImpl(fiberEffectTag, hookEffectTag, create, deps): void {
const hook = mountWorkInProgressHook();
const nextDeps = deps === undefined ? null : deps;
let fiber = ((currentlyRenderingFiber: any): Fiber);
fiber.effectTag |= fiberEffectTag;
currentlyRenderingFiber.effectTag |= fiberEffectTag;
hook.memoizedState = pushEffect(hookEffectTag, create, undefined, nextDeps);
}

Expand All @@ -916,8 +908,7 @@ function updateEffectImpl(fiberEffectTag, hookEffectTag, create, deps): void {
}
}

let fiber = ((currentlyRenderingFiber: any): Fiber);
fiber.effectTag |= fiberEffectTag;
currentlyRenderingFiber.effectTag |= fiberEffectTag;

hook.memoizedState = pushEffect(hookEffectTag, create, destroy, nextDeps);
}
Expand All @@ -930,7 +921,7 @@ function mountEffect(
// $FlowExpectedError - jest isn't a global, and isn't recognized outside of tests
if ('undefined' !== typeof jest) {
warnIfNotCurrentlyActingEffectsInDEV(
((currentlyRenderingFiber: any): Fiber),
currentlyRenderingFiber,
);
}
}
Expand All @@ -950,7 +941,7 @@ function updateEffect(
// $FlowExpectedError - jest isn't a global, and isn't recognized outside of tests
if ('undefined' !== typeof jest) {
warnIfNotCurrentlyActingEffectsInDEV(
((currentlyRenderingFiber: any): Fiber),
currentlyRenderingFiber,
);
}
}
Expand Down

0 comments on commit e73d0f2

Please sign in to comment.