@@ -173,6 +173,8 @@ export type Response = {
173
173
_prefix : string ,
174
174
_formData : FormData ,
175
175
_chunks : Map < number , SomeChunk< any >> ,
176
+ _closed : boolean ,
177
+ _closedReason : mixed ,
176
178
_temporaryReferences : void | TemporaryReferenceSet ,
177
179
} ;
178
180
@@ -261,6 +263,14 @@ function createResolvedModelChunk<T>(
261
263
return new Chunk ( RESOLVED_MODEL , value , id , response ) ;
262
264
}
263
265
266
+ function createErroredChunk< T > (
267
+ response: Response,
268
+ reason: mixed,
269
+ ): ErroredChunk< T > {
270
+ // $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
271
+ return new Chunk ( ERRORED , null , reason , response ) ;
272
+ }
273
+
264
274
function resolveModelChunk< T > (
265
275
chunk: SomeChunk< T > ,
266
276
value: string,
@@ -501,6 +511,8 @@ function initializeModelChunk<T>(chunk: ResolvedModelChunk<T>): void {
501
511
// Report that any missing chunks in the model is now going to throw this
502
512
// error upon read. Also notify any pending promises.
503
513
export function reportGlobalError ( response : Response , error : Error ) : void {
514
+ response . _closed = true ;
515
+ response . _closedReason = error ;
504
516
response . _chunks . forEach ( chunk => {
505
517
// If this chunk was already resolved or errored, it won't
506
518
// trigger an error but if it wasn't then we need to
@@ -522,6 +534,10 @@ function getChunk(response: Response, id: number): SomeChunk<any> {
522
534
if ( backingEntry != null ) {
523
535
// We assume that this is a string entry for now.
524
536
chunk = createResolvedModelChunk ( response , ( backingEntry : any ) , id ) ;
537
+ } else if ( response . _closed ) {
538
+ // We have already errored the response and we're not going to get
539
+ // anything more streaming in so this will immediately error.
540
+ chunk = createErroredChunk ( response , response . _closedReason ) ;
525
541
} else {
526
542
// We're still waiting on this entry to stream in.
527
543
chunk = createPendingChunk ( response ) ;
@@ -1102,6 +1118,8 @@ export function createResponse(
1102
1118
_prefix : formFieldPrefix ,
1103
1119
_formData : backingFormData ,
1104
1120
_chunks : chunks ,
1121
+ _closed : false ,
1122
+ _closedReason : null ,
1105
1123
_temporaryReferences : temporaryReferences ,
1106
1124
} ;
1107
1125
return response ;
0 commit comments