@@ -22,6 +22,9 @@ import type {
2222 ServerContextJSONValue ,
2323 Wakeable ,
2424 Thenable ,
25+ PendingThenable ,
26+ FulfilledThenable ,
27+ RejectedThenable ,
2528} from 'shared/ReactTypes' ;
2629import type { LazyComponent } from 'react/src/ReactLazy' ;
2730
@@ -66,7 +69,6 @@ import {
6669 getActiveContext ,
6770 rootContextSnapshot ,
6871} from './ReactFlightNewContext' ;
69- import { trackSuspendedWakeable } from './ReactFlightThenable' ;
7072
7173import {
7274 REACT_ELEMENT_TYPE ,
@@ -224,10 +226,44 @@ function readThenable<T>(thenable: Thenable<T>): T {
224226}
225227
226228function 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+ }
228264 const lazyType : LazyComponent < any , Thenable < any >> = {
229265 $$typeof : REACT_LAZY_TYPE ,
230- _payload : ( wakeable : any ) ,
266+ _payload : thenable ,
231267 _init : readThenable ,
232268 } ;
233269 return lazyType ;
@@ -818,11 +854,7 @@ export function resolveModelToJSON(
818854 ) ;
819855 const ping = newTask . ping ;
820856 x . then ( ping , ping ) ;
821-
822- const wakeable : Wakeable = x ;
823- trackSuspendedWakeable ( wakeable ) ;
824857 newTask . thenableState = getThenableStateAfterSuspending ( ) ;
825-
826858 return serializeByRefID ( newTask . id ) ;
827859 } else {
828860 // Something errored. We'll still send everything we have up until this point.
@@ -1146,9 +1178,6 @@ function retryTask(request: Request, task: Task): void {
11461178 // Something suspended again, let's pick it back up later.
11471179 const ping = task . ping ;
11481180 x . then ( ping , ping ) ;
1149-
1150- const wakeable : Wakeable = x ;
1151- trackSuspendedWakeable ( wakeable ) ;
11521181 task . thenableState = getThenableStateAfterSuspending ( ) ;
11531182 return ;
11541183 } else {
0 commit comments