@@ -22,6 +22,9 @@ import type {
22
22
ServerContextJSONValue ,
23
23
Wakeable ,
24
24
Thenable ,
25
+ PendingThenable ,
26
+ FulfilledThenable ,
27
+ RejectedThenable ,
25
28
} from 'shared/ReactTypes' ;
26
29
import type { LazyComponent } from 'react/src/ReactLazy' ;
27
30
@@ -66,7 +69,6 @@ import {
66
69
getActiveContext ,
67
70
rootContextSnapshot ,
68
71
} from './ReactFlightNewContext' ;
69
- import { trackSuspendedWakeable } from './ReactFlightThenable' ;
70
72
71
73
import {
72
74
REACT_ELEMENT_TYPE ,
@@ -224,10 +226,44 @@ function readThenable<T>(thenable: Thenable<T>): T {
224
226
}
225
227
226
228
function createLazyWrapperAroundWakeable ( wakeable : Wakeable ) {
227
- trackSuspendedWakeable ( wakeable ) ;
229
+ // This is a temporary fork of the `use` implementation until we accept
230
+ // promises everywhere.
231
+ const thenable : Thenable < mixed > = ( wakeable : any ) ;
232
+ switch ( thenable . status ) {
233
+ case 'fulfilled' :
234
+ case 'rejected' :
235
+ break ;
236
+ default : {
237
+ if ( typeof thenable . status === 'string' ) {
238
+ // Only instrument the thenable if the status if not defined. If
239
+ // it's defined, but an unknown value, assume it's been instrumented by
240
+ // some custom userspace implementation. We treat it as "pending".
241
+ break ;
242
+ }
243
+ const pendingThenable : PendingThenable < mixed > = ( thenable : any ) ;
244
+ pendingThenable . status = 'pending' ;
245
+ pendingThenable . then (
246
+ fulfilledValue => {
247
+ if ( thenable . status === 'pending' ) {
248
+ const fulfilledThenable : FulfilledThenable < mixed > = (thenable: any);
249
+ fulfilledThenable.status = 'fulfilled';
250
+ fulfilledThenable.value = fulfilledValue;
251
+ }
252
+ } ,
253
+ ( error : mixed ) => {
254
+ if ( thenable . status === 'pending' ) {
255
+ const rejectedThenable : RejectedThenable < mixed > = (thenable: any);
256
+ rejectedThenable.status = 'rejected';
257
+ rejectedThenable.reason = error;
258
+ }
259
+ } ,
260
+ ) ;
261
+ break ;
262
+ }
263
+ }
228
264
const lazyType : LazyComponent < any , Thenable < any >> = {
229
265
$$typeof : REACT_LAZY_TYPE ,
230
- _payload : ( wakeable : any ) ,
266
+ _payload : thenable ,
231
267
_init : readThenable ,
232
268
} ;
233
269
return lazyType ;
@@ -818,11 +854,7 @@ export function resolveModelToJSON(
818
854
) ;
819
855
const ping = newTask . ping ;
820
856
x . then ( ping , ping ) ;
821
-
822
- const wakeable : Wakeable = x ;
823
- trackSuspendedWakeable ( wakeable ) ;
824
857
newTask . thenableState = getThenableStateAfterSuspending ( ) ;
825
-
826
858
return serializeByRefID ( newTask . id ) ;
827
859
} else {
828
860
// Something errored. We'll still send everything we have up until this point.
@@ -1146,9 +1178,6 @@ function retryTask(request: Request, task: Task): void {
1146
1178
// Something suspended again, let's pick it back up later.
1147
1179
const ping = task . ping ;
1148
1180
x . then ( ping , ping ) ;
1149
-
1150
- const wakeable : Wakeable = x ;
1151
- trackSuspendedWakeable ( wakeable ) ;
1152
1181
task . thenableState = getThenableStateAfterSuspending ( ) ;
1153
1182
return ;
1154
1183
} else {
0 commit comments