Skip to content

Commit 9592ad7

Browse files
committed
Add pending state to useFormState (#28514)
## Overview Adds a `pending` state to useFormState, which will be replaced by `useActionState` in the next diff. We will keep `useFormState` around for backwards compatibility, but functionally it will work the same as `useActionState`, which has an `isPending` state returned. DiffTrain build for commit 17eaaca.
1 parent 07a5cf3 commit 9592ad7

File tree

13 files changed

+523
-214
lines changed

13 files changed

+523
-214
lines changed

compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react-test-renderer/cjs/ReactTestRenderer-dev.js

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @noflow
88
* @nolint
99
* @preventMunge
10-
* @generated SignedSource<<a8e13b4ae0027a62588426b400d5b877>>
10+
* @generated SignedSource<<6ab290eb8548e08a74bf8def3f649394>>
1111
*/
1212

1313
"use strict";
@@ -8476,7 +8476,13 @@ if (__DEV__) {
84768476
} // useFormState actions run sequentially, because each action receives the
84778477
// previous state as an argument. We store pending actions on a queue.
84788478

8479-
function dispatchFormState(fiber, actionQueue, setState, payload) {
8479+
function dispatchFormState(
8480+
fiber,
8481+
actionQueue,
8482+
setPendingState,
8483+
setState,
8484+
payload
8485+
) {
84808486
if (isRenderPhaseUpdate(fiber)) {
84818487
throw new Error("Cannot update form state while rendering.");
84828488
}
@@ -8491,7 +8497,7 @@ if (__DEV__) {
84918497
next: null // circular
84928498
};
84938499
newLast.next = actionQueue.pending = newLast;
8494-
runFormStateAction(actionQueue, setState, payload);
8500+
runFormStateAction(actionQueue, setPendingState, setState, payload);
84958501
} else {
84968502
// There's already an action running. Add to the queue.
84978503
var first = last.next;
@@ -8503,7 +8509,12 @@ if (__DEV__) {
85038509
}
85048510
}
85058511

8506-
function runFormStateAction(actionQueue, setState, payload) {
8512+
function runFormStateAction(
8513+
actionQueue,
8514+
setPendingState,
8515+
setState,
8516+
payload
8517+
) {
85078518
var action = actionQueue.action;
85088519
var prevState = actionQueue.state; // This is a fork of startTransition
85098520

@@ -8515,7 +8526,10 @@ if (__DEV__) {
85158526

85168527
{
85178528
ReactCurrentBatchConfig$2.transition._updatedFibers = new Set();
8518-
}
8529+
} // Optimistically update the pending state, similar to useTransition.
8530+
// This will be reverted automatically when all actions are finished.
8531+
8532+
setPendingState(true);
85198533

85208534
try {
85218535
var returnValue = action(prevState, payload);
@@ -8532,18 +8546,26 @@ if (__DEV__) {
85328546
thenable.then(
85338547
function (nextState) {
85348548
actionQueue.state = nextState;
8535-
finishRunningFormStateAction(actionQueue, setState);
8549+
finishRunningFormStateAction(
8550+
actionQueue,
8551+
setPendingState,
8552+
setState
8553+
);
85368554
},
85378555
function () {
8538-
return finishRunningFormStateAction(actionQueue, setState);
8556+
return finishRunningFormStateAction(
8557+
actionQueue,
8558+
setPendingState,
8559+
setState
8560+
);
85398561
}
85408562
);
85418563
setState(thenable);
85428564
} else {
85438565
setState(returnValue);
85448566
var nextState = returnValue;
85458567
actionQueue.state = nextState;
8546-
finishRunningFormStateAction(actionQueue, setState);
8568+
finishRunningFormStateAction(actionQueue, setPendingState, setState);
85478569
}
85488570
} catch (error) {
85498571
// This is a trick to get the `useFormState` hook to rethrow the error.
@@ -8555,7 +8577,7 @@ if (__DEV__) {
85558577
reason: error // $FlowFixMe: Not sure why this doesn't work
85568578
};
85578579
setState(rejectedThenable);
8558-
finishRunningFormStateAction(actionQueue, setState);
8580+
finishRunningFormStateAction(actionQueue, setPendingState, setState);
85598581
} finally {
85608582
ReactCurrentBatchConfig$2.transition = prevTransition;
85618583

@@ -8577,7 +8599,11 @@ if (__DEV__) {
85778599
}
85788600
}
85798601

8580-
function finishRunningFormStateAction(actionQueue, setState) {
8602+
function finishRunningFormStateAction(
8603+
actionQueue,
8604+
setPendingState,
8605+
setState
8606+
) {
85818607
// The action finished running. Pop it from the queue and run the next pending
85828608
// action, if there are any.
85838609
var last = actionQueue.pending;
@@ -8593,7 +8619,12 @@ if (__DEV__) {
85938619
var next = first.next;
85948620
last.next = next; // Run the next action.
85958621

8596-
runFormStateAction(actionQueue, setState, next.payload);
8622+
runFormStateAction(
8623+
actionQueue,
8624+
setPendingState,
8625+
setState,
8626+
next.payload
8627+
);
85978628
}
85988629
}
85998630
}
@@ -8623,7 +8654,16 @@ if (__DEV__) {
86238654
currentlyRenderingFiber$1,
86248655
stateQueue
86258656
);
8626-
stateQueue.dispatch = setState; // Action queue hook. This is used to queue pending actions. The queue is
8657+
stateQueue.dispatch = setState; // Pending state. This is used to store the pending state of the action.
8658+
// Tracked optimistically, like a transition pending state.
8659+
8660+
var pendingStateHook = mountStateImpl(false);
8661+
var setPendingState = dispatchOptimisticSetState.bind(
8662+
null,
8663+
currentlyRenderingFiber$1,
8664+
false,
8665+
pendingStateHook.queue
8666+
); // Action queue hook. This is used to queue pending actions. The queue is
86278667
// shared between all instances of the hook. Similar to a regular state queue,
86288668
// but different because the actions are run sequentially, and they run in
86298669
// an event instead of during render.
@@ -8641,14 +8681,15 @@ if (__DEV__) {
86418681
null,
86428682
currentlyRenderingFiber$1,
86438683
actionQueue,
8684+
setPendingState,
86448685
setState
86458686
);
86468687
actionQueue.dispatch = dispatch; // Stash the action function on the memoized state of the hook. We'll use this
86478688
// to detect when the action function changes so we can update it in
86488689
// an effect.
86498690

86508691
actionQueueHook.memoizedState = action;
8651-
return [initialState, dispatch];
8692+
return [initialState, dispatch, false];
86528693
}
86538694

86548695
function updateFormState(action, initialState, permalink) {
@@ -8669,7 +8710,10 @@ if (__DEV__) {
86698710
currentStateHook,
86708711
formStateReducer
86718712
),
8672-
actionResult = _updateReducerImpl[0]; // This will suspend until the action finishes.
8713+
actionResult = _updateReducerImpl[0];
8714+
8715+
var _updateState = updateState(),
8716+
isPending = _updateState[0]; // This will suspend until the action finishes.
86738717

86748718
var state =
86758719
typeof actionResult === "object" &&
@@ -8693,7 +8737,7 @@ if (__DEV__) {
86938737
);
86948738
}
86958739

8696-
return [state, dispatch];
8740+
return [state, dispatch, isPending];
86978741
}
86988742

86998743
function formStateActionEffect(actionQueue, action) {
@@ -8721,8 +8765,9 @@ if (__DEV__) {
87218765
var actionQueue = actionQueueHook.queue;
87228766
var dispatch = actionQueue.dispatch; // This may have changed during the rerender.
87238767

8724-
actionQueueHook.memoizedState = action;
8725-
return [state, dispatch];
8768+
actionQueueHook.memoizedState = action; // For mount, pending is always false.
8769+
8770+
return [state, dispatch, false];
87268771
}
87278772

87288773
function pushEffect(tag, create, inst, deps) {
@@ -9201,8 +9246,8 @@ if (__DEV__) {
92019246
}
92029247

92039248
function updateTransition() {
9204-
var _updateState = updateState(),
9205-
booleanOrThenable = _updateState[0];
9249+
var _updateState2 = updateState(),
9250+
booleanOrThenable = _updateState2[0];
92069251

92079252
var hook = updateWorkInProgressHook();
92089253
var start = hook.memoizedState;
@@ -25409,7 +25454,7 @@ if (__DEV__) {
2540925454
return root;
2541025455
}
2541125456

25412-
var ReactVersion = "18.3.0-canary-3e6bc7d2d-20240312";
25457+
var ReactVersion = "18.3.0-canary-17eaacaac-20240312";
2541325458

2541425459
// Might add PROFILE later.
2541525460

0 commit comments

Comments
 (0)