3
3
import { isNotFoundError } from "next/dist/client/components/not-found.js" ;
4
4
import { isRedirectError } from "next/dist/client/components/redirect.js" ;
5
5
import * as React from "react" ;
6
- import * as ReactDOM from "react-dom" ;
7
6
import { } from "react/experimental" ;
8
7
import type { } from "zod" ;
9
8
import type { InferIn , Schema } from "./adapters/types" ;
@@ -41,6 +40,12 @@ export const useAction = <
41
40
42
41
const execute = React . useCallback (
43
42
( input : S extends Schema ? InferIn < S > : void ) => {
43
+ setTimeout ( ( ) => {
44
+ setIsIdle ( false ) ;
45
+ setClientInput ( input ) ;
46
+ setIsExecuting ( true ) ;
47
+ } , 0 ) ;
48
+
44
49
startTransition ( ( ) => {
45
50
safeActionFn ( input as S extends Schema ? InferIn < S > : undefined )
46
51
. then ( ( res ) => setResult ( res ?? { } ) )
@@ -55,19 +60,19 @@ export const useAction = <
55
60
setIsExecuting ( false ) ;
56
61
} ) ;
57
62
} ) ;
58
-
59
- ReactDOM . flushSync ( ( ) => {
60
- setIsIdle ( false ) ;
61
- setClientInput ( input ) ;
62
- setIsExecuting ( true ) ;
63
- } ) ;
64
63
} ,
65
64
[ safeActionFn ]
66
65
) ;
67
66
68
67
const executeAsync = React . useCallback (
69
68
( input : S extends Schema ? InferIn < S > : void ) => {
70
69
const fn = new Promise < Awaited < ReturnType < typeof safeActionFn > > > ( ( resolve , reject ) => {
70
+ setTimeout ( ( ) => {
71
+ setIsIdle ( false ) ;
72
+ setClientInput ( input ) ;
73
+ setIsExecuting ( true ) ;
74
+ } , 0 ) ;
75
+
71
76
startTransition ( ( ) => {
72
77
safeActionFn ( input as S extends Schema ? InferIn < S > : undefined )
73
78
. then ( ( res ) => {
@@ -88,12 +93,6 @@ export const useAction = <
88
93
} ) ;
89
94
} ) ;
90
95
91
- ReactDOM . flushSync ( ( ) => {
92
- setIsIdle ( false ) ;
93
- setClientInput ( input ) ;
94
- setIsExecuting ( true ) ;
95
- } ) ;
96
-
97
96
return fn ;
98
97
} ,
99
98
[ safeActionFn ]
@@ -165,6 +164,12 @@ export const useOptimisticAction = <
165
164
166
165
const execute = React . useCallback (
167
166
( input : S extends Schema ? InferIn < S > : void ) => {
167
+ setTimeout ( ( ) => {
168
+ setIsIdle ( false ) ;
169
+ setClientInput ( input ) ;
170
+ setIsExecuting ( true ) ;
171
+ } , 0 ) ;
172
+
168
173
startTransition ( ( ) => {
169
174
setOptimisticValue ( input as S extends Schema ? InferIn < S > : undefined ) ;
170
175
safeActionFn ( input as S extends Schema ? InferIn < S > : undefined )
@@ -180,19 +185,19 @@ export const useOptimisticAction = <
180
185
setIsExecuting ( false ) ;
181
186
} ) ;
182
187
} ) ;
183
-
184
- ReactDOM . flushSync ( ( ) => {
185
- setIsIdle ( false ) ;
186
- setClientInput ( input ) ;
187
- setIsExecuting ( true ) ;
188
- } ) ;
189
188
} ,
190
189
[ safeActionFn , setOptimisticValue ]
191
190
) ;
192
191
193
192
const executeAsync = React . useCallback (
194
193
( input : S extends Schema ? InferIn < S > : void ) => {
195
194
const fn = new Promise < Awaited < ReturnType < typeof safeActionFn > > > ( ( resolve , reject ) => {
195
+ setTimeout ( ( ) => {
196
+ setIsIdle ( false ) ;
197
+ setClientInput ( input ) ;
198
+ setIsExecuting ( true ) ;
199
+ } , 0 ) ;
200
+
196
201
startTransition ( ( ) => {
197
202
setOptimisticValue ( input as S extends Schema ? InferIn < S > : undefined ) ;
198
203
safeActionFn ( input as S extends Schema ? InferIn < S > : undefined )
@@ -214,12 +219,6 @@ export const useOptimisticAction = <
214
219
} ) ;
215
220
} ) ;
216
221
217
- ReactDOM . flushSync ( ( ) => {
218
- setIsIdle ( false ) ;
219
- setClientInput ( input ) ;
220
- setIsExecuting ( true ) ;
221
- } ) ;
222
-
223
222
return fn ;
224
223
} ,
225
224
[ safeActionFn , setOptimisticValue ]
0 commit comments