Skip to content

Commit 591adfa

Browse files
authored
[Flight] Rename Chunk constructor to ReactPromise (#30747)
When printing these in DevTools they show up as the name of the constructor so then you pass a Promise to the client it logs as "Chunk" which is confusing. Ideally we'd probably just name this Promise but 1) there's a slight difference in the .then method atm 2) it's a bit tricky to name a variable and get it from the global in the same scope. Closure compiler doesn't let us just name a function because it removes it and just uses the variable name.
1 parent 9d082b5 commit 591adfa

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

packages/react-client/src/ReactFlightClient.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,12 @@ type SomeChunk<T> =
191191
| ErroredChunk<T>;
192192

193193
// $FlowFixMe[missing-this-annot]
194-
function Chunk(status: any, value: any, reason: any, response: Response) {
194+
function ReactPromise(
195+
status: any,
196+
value: any,
197+
reason: any,
198+
response: Response,
199+
) {
195200
this.status = status;
196201
this.value = value;
197202
this.reason = reason;
@@ -201,9 +206,9 @@ function Chunk(status: any, value: any, reason: any, response: Response) {
201206
}
202207
}
203208
// We subclass Promise.prototype so that we get other methods like .catch
204-
Chunk.prototype = (Object.create(Promise.prototype): any);
209+
ReactPromise.prototype = (Object.create(Promise.prototype): any);
205210
// TODO: This doesn't return a new Promise chain unlike the real .then
206-
Chunk.prototype.then = function <T>(
211+
ReactPromise.prototype.then = function <T>(
207212
this: SomeChunk<T>,
208213
resolve: (value: T) => mixed,
209214
reject?: (reason: mixed) => mixed,
@@ -304,20 +309,20 @@ export function getRoot<T>(response: Response): Thenable<T> {
304309

305310
function createPendingChunk<T>(response: Response): PendingChunk<T> {
306311
// $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
307-
return new Chunk(PENDING, null, null, response);
312+
return new ReactPromise(PENDING, null, null, response);
308313
}
309314

310315
function createBlockedChunk<T>(response: Response): BlockedChunk<T> {
311316
// $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
312-
return new Chunk(BLOCKED, null, null, response);
317+
return new ReactPromise(BLOCKED, null, null, response);
313318
}
314319

315320
function createErrorChunk<T>(
316321
response: Response,
317322
error: Error | Postpone,
318323
): ErroredChunk<T> {
319324
// $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
320-
return new Chunk(ERRORED, null, error, response);
325+
return new ReactPromise(ERRORED, null, error, response);
321326
}
322327

323328
function wakeChunk<T>(listeners: Array<(T) => mixed>, value: T): void {
@@ -391,31 +396,31 @@ function createResolvedModelChunk<T>(
391396
value: UninitializedModel,
392397
): ResolvedModelChunk<T> {
393398
// $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
394-
return new Chunk(RESOLVED_MODEL, value, null, response);
399+
return new ReactPromise(RESOLVED_MODEL, value, null, response);
395400
}
396401

397402
function createResolvedModuleChunk<T>(
398403
response: Response,
399404
value: ClientReference<T>,
400405
): ResolvedModuleChunk<T> {
401406
// $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
402-
return new Chunk(RESOLVED_MODULE, value, null, response);
407+
return new ReactPromise(RESOLVED_MODULE, value, null, response);
403408
}
404409

405410
function createInitializedTextChunk(
406411
response: Response,
407412
value: string,
408413
): InitializedChunk<string> {
409414
// $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
410-
return new Chunk(INITIALIZED, value, null, response);
415+
return new ReactPromise(INITIALIZED, value, null, response);
411416
}
412417

413418
function createInitializedBufferChunk(
414419
response: Response,
415420
value: $ArrayBufferView | ArrayBuffer,
416421
): InitializedChunk<Uint8Array> {
417422
// $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
418-
return new Chunk(INITIALIZED, value, null, response);
423+
return new ReactPromise(INITIALIZED, value, null, response);
419424
}
420425

421426
function createInitializedIteratorResultChunk<T>(
@@ -424,7 +429,12 @@ function createInitializedIteratorResultChunk<T>(
424429
done: boolean,
425430
): InitializedChunk<IteratorResult<T, T>> {
426431
// $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
427-
return new Chunk(INITIALIZED, {done: done, value: value}, null, response);
432+
return new ReactPromise(
433+
INITIALIZED,
434+
{done: done, value: value},
435+
null,
436+
response,
437+
);
428438
}
429439

430440
function createInitializedStreamChunk<
@@ -437,7 +447,7 @@ function createInitializedStreamChunk<
437447
// We use the reason field to stash the controller since we already have that
438448
// field. It's a bit of a hack but efficient.
439449
// $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
440-
return new Chunk(INITIALIZED, value, controller, response);
450+
return new ReactPromise(INITIALIZED, value, controller, response);
441451
}
442452

443453
function createResolvedIteratorResultChunk<T>(
@@ -449,7 +459,7 @@ function createResolvedIteratorResultChunk<T>(
449459
const iteratorResultJSON =
450460
(done ? '{"done":true,"value":' : '{"done":false,"value":') + value + '}';
451461
// $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
452-
return new Chunk(RESOLVED_MODEL, iteratorResultJSON, null, response);
462+
return new ReactPromise(RESOLVED_MODEL, iteratorResultJSON, null, response);
453463
}
454464

455465
function resolveIteratorResultChunk<T>(
@@ -1761,7 +1771,7 @@ function startAsyncIterable<T>(
17611771
if (nextReadIndex === buffer.length) {
17621772
if (closed) {
17631773
// $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
1764-
return new Chunk(
1774+
return new ReactPromise(
17651775
INITIALIZED,
17661776
{done: true, value: undefined},
17671777
null,

0 commit comments

Comments
 (0)