Skip to content

Commit

Permalink
Unify hooklog push
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Silbermann committed Feb 15, 2024
1 parent 4e77339 commit adea250
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions packages/react-debug-tools/src/ReactDebugHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,10 @@ function useFormState<S, P>(
const hook = nextHook(); // FormState
nextHook(); // ActionQueue
const stackError = new Error();
let state;
let value;
let debugInfo = null;
let error = null;

if (hook !== null) {
const actionResult = hook.memoizedState;
if (
Expand All @@ -507,39 +509,45 @@ function useFormState<S, P>(
const thenable: Thenable<Awaited<S>> = (actionResult: any);
switch (thenable.status) {
case 'fulfilled': {
state = thenable.value;
value = thenable.value;
debugInfo =
thenable._debugInfo === undefined ? null : thenable._debugInfo;
break;
}
case 'rejected': {
const rejectedError = thenable.reason;
throw rejectedError;
error = rejectedError;
break;
}
default:
// If this was an uncached Promise we have to abandon this attempt
// but we can still emit anything up until this point.
hookLog.push({
primitive: 'FormState',
stackError: stackError,
value: thenable,
debugInfo:
thenable._debugInfo === undefined ? null : thenable._debugInfo,
});
throw SuspenseException;
error = SuspenseException;
debugInfo =
thenable._debugInfo === undefined ? null : thenable._debugInfo;
value = thenable;
}
} else {
state = (actionResult: any);
value = (actionResult: any);
}
} else {
state = initialState;
value = initialState;
}

hookLog.push({
primitive: 'FormState',
stackError: stackError,
value: state,
value: value,
debugInfo: debugInfo,
});

if (error !== null) {
throw error;
}

// value being a Thenable is equivalent to error being not null
// i.e. we only reach this point with Awaited<S>
const state = ((value: any): Awaited<S>);
return [state, (payload: P) => {}];
}

Expand Down

0 comments on commit adea250

Please sign in to comment.