@@ -169,6 +169,8 @@ export type Response = {
169
169
_prefix : string ,
170
170
_formData : FormData ,
171
171
_chunks : Map < number , SomeChunk< any >> ,
172
+ _closed : boolean ,
173
+ _closedReason : mixed ,
172
174
_temporaryReferences : void | TemporaryReferenceSet ,
173
175
} ;
174
176
@@ -255,6 +257,14 @@ function createResolvedModelChunk<T>(
255
257
return new Chunk ( RESOLVED_MODEL , value , id , response ) ;
256
258
}
257
259
260
+ function createErroredChunk< T > (
261
+ response: Response,
262
+ reason: mixed,
263
+ ): ErroredChunk< T > {
264
+ // $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
265
+ return new Chunk ( ERRORED , null , reason , response ) ;
266
+ }
267
+
258
268
function resolveModelChunk< T > (
259
269
chunk: SomeChunk< T > ,
260
270
value: string,
@@ -493,6 +503,8 @@ function initializeModelChunk<T>(chunk: ResolvedModelChunk<T>): void {
493
503
// Report that any missing chunks in the model is now going to throw this
494
504
// error upon read. Also notify any pending promises.
495
505
export function reportGlobalError ( response : Response , error : Error ) : void {
506
+ response . _closed = true ;
507
+ response . _closedReason = error ;
496
508
response . _chunks . forEach ( chunk => {
497
509
// If this chunk was already resolved or errored, it won't
498
510
// trigger an error but if it wasn't then we need to
@@ -514,6 +526,10 @@ function getChunk(response: Response, id: number): SomeChunk<any> {
514
526
if ( backingEntry != null ) {
515
527
// We assume that this is a string entry for now.
516
528
chunk = createResolvedModelChunk ( response , ( backingEntry : any ) , id ) ;
529
+ } else if ( response . _closed ) {
530
+ // We have already errored the response and we're not going to get
531
+ // anything more streaming in so this will immediately error.
532
+ chunk = createErroredChunk ( response , response . _closedReason ) ;
517
533
} else {
518
534
// We're still waiting on this entry to stream in.
519
535
chunk = createPendingChunk ( response ) ;
@@ -1082,6 +1098,8 @@ export function createResponse(
1082
1098
_prefix : formFieldPrefix ,
1083
1099
_formData : backingFormData ,
1084
1100
_chunks : chunks ,
1101
+ _closed : false ,
1102
+ _closedReason : null ,
1085
1103
_temporaryReferences : temporaryReferences ,
1086
1104
} ;
1087
1105
return response ;
0 commit comments