@@ -167,6 +167,9 @@ const HALTED = 'halted'; // DEV-only. Means it never resolves even if connection
167167
168168const __PROTO__ = '__proto__' ;
169169
170+ type RESPONSE_SYMBOL_TYPE = 'RESPONSE_SYMBOL' ; // Fake symbol type.
171+ const RESPONSE_SYMBOL : RESPONSE_SYMBOL_TYPE = Symbol ( ) as any ;
172+
170173type PendingChunk < T > = {
171174 status : 'pending' ,
172175 value : null | Array < InitializationReference | ( T => mixed ) > ,
@@ -188,7 +191,7 @@ type BlockedChunk<T> = {
188191type ResolvedModelChunk < T > = {
189192 status : 'resolved_model' ,
190193 value : UninitializedModel ,
191- reason : Response ,
194+ reason : { [ RESPONSE_SYMBOL_TYPE ] : Response } ,
192195 _children : Array < SomeChunk < any >> | ProfilingResult , // Profiling-only
193196 _debugChunk : null | SomeChunk < ReactDebugInfoEntry > , // DEV-only
194197 _debugInfo : ReactDebugInfo , // DEV-only
@@ -736,7 +739,7 @@ function createResolvedModelChunk<T>(
736739 value: UninitializedModel,
737740): ResolvedModelChunk< T > {
738741 // $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
739- return new ReactPromise ( RESOLVED_MODEL , value , response ) ;
742+ return new ReactPromise ( RESOLVED_MODEL , value , { [ RESPONSE_SYMBOL ] : response } ) ;
740743}
741744
742745function createResolvedModuleChunk< T > (
@@ -794,7 +797,9 @@ function createResolvedIteratorResultChunk<T>(
794797 const iteratorResultJSON =
795798 ( done ? '{"done":true,"value":' : '{"done":false,"value":' ) + value + '}' ;
796799 // $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
797- return new ReactPromise ( RESOLVED_MODEL , iteratorResultJSON , response ) ;
800+ return new ReactPromise ( RESOLVED_MODEL , iteratorResultJSON , {
801+ [ RESPONSE_SYMBOL ] : response ,
802+ } ) ;
798803}
799804
800805function resolveIteratorResultChunk< T > (
@@ -828,7 +833,7 @@ function resolveModelChunk<T>(
828833 const resolvedChunk : ResolvedModelChunk < T > = ( chunk : any ) ;
829834 resolvedChunk . status = RESOLVED_MODEL ;
830835 resolvedChunk . value = value ;
831- resolvedChunk . reason = response ;
836+ resolvedChunk . reason = { [ RESPONSE_SYMBOL ] : response } ;
832837 if ( resolveListeners !== null ) {
833838 // This is unfortunate that we're reading this eagerly if
834839 // we already have listeners attached since they might no
@@ -975,7 +980,7 @@ function initializeModelChunk<T>(chunk: ResolvedModelChunk<T>): void {
975980 initializingHandler = null ;
976981
977982 const resolvedModel = chunk . value ;
978- const response = chunk . reason ;
983+ const response = chunk . reason [ RESPONSE_SYMBOL ] ;
979984
980985 // We go to the BLOCKED state until we've fully resolved this.
981986 // We do this before parsing in case we try to initialize the same chunk
@@ -1747,6 +1752,15 @@ function loadServerReference<A: Iterable<any>, T>(
17471752 parentObject : Object ,
17481753 key : string ,
17491754) : ( ...A ) => Promise < T > {
1755+ if ( key === 'then' ) {
1756+ // This should never happen because we always serialize objects with then-functions
1757+ // as "thenable" which reduces to a chunk with no other fields.
1758+ return null as any ;
1759+ }
1760+ const id = metaData . id ;
1761+ if ( typeof id !== 'string' ) {
1762+ return null as any ;
1763+ }
17501764 if ( ! response . _serverReferenceConfig ) {
17511765 // In the normal case, we can't load this Server Reference in the current environment and
17521766 // we just return a proxy to it.
@@ -1759,27 +1773,27 @@ function loadServerReference<A: Iterable<any>, T>(
17591773 }
17601774 // If we have a module mapping we can load the real version of this Server Reference.
17611775 const serverReference : ClientReference < T > =
1762- resolveServerReference < $FlowFixMe > (
1763- response._serverReferenceConfig,
1764- metaData.id,
1765- );
1776+ resolveServerReference < $FlowFixMe > ( response . _serverReferenceConfig , id ) ;
17661777
1778+ const bound = metaData . bound ;
17671779 let promise : null | Thenable < any > = preloadModule ( serverReference ) ;
17681780 if ( ! promise ) {
1769- if ( ! metaData . bound ) {
1770- const resolvedValue = ( requireModule ( serverReference ) : any ) ;
1781+ if ( bound instanceof ReactPromise ) {
1782+ promise = Promise . resolve ( bound ) ;
1783+ } else {
1784+ // We only await the bound arguments when they are one of our own chunks.
1785+ // Anything else could be a foreign thenable whose `then` we must not call.
1786+ const resolvedValue = requireModule ( serverReference ) as any ;
17711787 registerBoundServerReference (
17721788 resolvedValue ,
1773- metaData . id ,
1774- metaData . bound ,
1789+ id ,
1790+ bound ,
17751791 response . _encodeFormAction ,
17761792 ) ;
17771793 return resolvedValue ;
1778- } else {
1779- promise = Promise . resolve ( metaData . bound ) ;
17801794 }
1781- } else if (metaData. bound) {
1782- promise = Promise . all ( [ promise , metaData . bound ] ) ;
1795+ } else if ( bound instanceof ReactPromise ) {
1796+ promise = Promise . all ( [ promise , bound ] ) ;
17831797 }
17841798
17851799 let handler : InitializationHandler ;
@@ -1800,17 +1814,20 @@ function loadServerReference<A: Iterable<any>, T>(
18001814 function fulfill ( ) : void {
18011815 let resolvedValue = ( requireModule ( serverReference ) : any ) ;
18021816
1803- if ( metaData . bound ) {
1804- // This promise is coming from us and should have initilialized by now.
1805- const boundArgs : Array < any > = ( metaData . bound : any ) . value . slice ( 0 ) ;
1817+ if ( bound ) {
1818+ // This promise is coming from us and should have initialized by now.
1819+ const promiseValue = ( bound as any ) . value ;
1820+ const boundArgs : Array < any > = isArray ( promiseValue )
1821+ ? promiseValue . slice ( 0 )
1822+ : [ ] ;
18061823 boundArgs . unshift ( null ) ; // this
18071824 resolvedValue = resolvedValue . bind . apply ( resolvedValue , boundArgs ) ;
18081825 }
18091826
18101827 registerBoundServerReference (
18111828 resolvedValue ,
1812- metaData . id ,
1813- metaData . bound ,
1829+ id ,
1830+ bound ,
18141831 response . _encodeFormAction ,
18151832 ) ;
18161833
@@ -2227,7 +2244,17 @@ function extractIterator(response: Response, model: Array<any>): Iterator<any> {
22272244 return model [ Symbol . iterator ] ( ) ;
22282245}
22292246
2230- function createModel(response: Response, model: any): any {
2247+ function createModel(
2248+ response: Response,
2249+ model: any,
2250+ parentObject: Object,
2251+ key: string,
2252+ ): any {
2253+ if ( key === 'then' && typeof model === 'function' ) {
2254+ // This should never happen because we always serialize objects with then-functions
2255+ // as "thenable" which reduces to a chunk with no other fields.
2256+ return null ;
2257+ }
22312258 return model ;
22322259}
22332260
0 commit comments