@@ -135,6 +135,8 @@ export type Response = {
135
135
_formData : FormData ,
136
136
_chunks : Map < number , SomeChunk< any >> ,
137
137
_fromJSON : ( key : string , value : JSONValue ) => any ,
138
+ _closed : boolean ,
139
+ _closedReason : mixed ,
138
140
} ;
139
141
140
142
export function getRoot < T > ( response : Response ) : Thenable < T > {
@@ -198,6 +200,14 @@ function createResolvedModelChunk<T>(
198
200
return new Chunk ( RESOLVED_MODEL , value , null , response ) ;
199
201
}
200
202
203
+ function createErroredChunk< T > (
204
+ response: Response,
205
+ reason: mixed,
206
+ ): ErroredChunk< T > {
207
+ // $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
208
+ return new Chunk ( ERRORED , null , reason , response ) ;
209
+ }
210
+
201
211
function resolveModelChunk< T > (chunk: SomeChunk< T > , value: string): void {
202
212
if ( chunk . status !== PENDING ) {
203
213
// We already resolved. We didn't expect to see this.
@@ -297,6 +307,8 @@ function initializeModelChunk<T>(chunk: ResolvedModelChunk<T>): void {
297
307
// Report that any missing chunks in the model is now going to throw this
298
308
// error upon read. Also notify any pending promises.
299
309
export function reportGlobalError ( response : Response , error : Error ) : void {
310
+ response . _closed = true ;
311
+ response . _closedReason = error ;
300
312
response . _chunks . forEach ( chunk => {
301
313
// If this chunk was already resolved or errored, it won't
302
314
// trigger an error but if it wasn't then we need to
@@ -318,6 +330,10 @@ function getChunk(response: Response, id: number): SomeChunk<any> {
318
330
if ( backingEntry != null ) {
319
331
// We assume that this is a string entry for now.
320
332
chunk = createResolvedModelChunk ( response , ( backingEntry : any ) ) ;
333
+ } else if ( response . _closed ) {
334
+ // We have already errored the response and we're not going to get
335
+ // anything more streaming in so this will immediately error.
336
+ chunk = createErroredChunk ( response , response . _closedReason ) ;
321
337
} else {
322
338
// We're still waiting on this entry to stream in.
323
339
chunk = createPendingChunk ( response ) ;
@@ -519,6 +535,8 @@ export function createResponse(
519
535
}
520
536
return value ;
521
537
} ,
538
+ _closed : false ,
539
+ _closedReason : null ,
522
540
} ;
523
541
return response ;
524
542
}
0 commit comments