Skip to content

Commit 2f20c26

Browse files
committed
Encode Iterator separately from Iterable in Flight Reply
1 parent a64c6bf commit 2f20c26

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

packages/react-client/src/ReactFlightReplyClient.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ export type ReactServerValue =
8181
| null
8282
| void
8383
| bigint
84+
| $AsyncIterable<ReactServerValue, ReactServerValue, void>
85+
| $AsyncIterator<ReactServerValue, ReactServerValue, void>
8486
| Iterable<ReactServerValue>
87+
| Iterator<ReactServerValue>
8588
| Array<ReactServerValue>
8689
| Map<ReactServerValue, ReactServerValue>
8790
| Set<ReactServerValue>
@@ -157,6 +160,10 @@ function serializeBlobID(id: number): string {
157160
return '$B' + id.toString(16);
158161
}
159162

163+
function serializeIteratorID(id: number): string {
164+
return '$i' + id.toString(16);
165+
}
166+
160167
function escapeStringValue(value: string): string {
161168
if (value[0] === '$') {
162169
// We need to escape $ prefixed strings since we use those to encode
@@ -448,7 +455,21 @@ export function processReply(
448455

449456
const iteratorFn = getIteratorFn(value);
450457
if (iteratorFn) {
451-
return Array.from((value: any));
458+
const iterator = iteratorFn.call(value);
459+
if (iterator === value) {
460+
// Iterator, not Iterable
461+
const partJSON = JSON.stringify(
462+
Array.from((iterator: any)),
463+
resolveToJSON,
464+
);
465+
if (formData === null) {
466+
formData = new FormData();
467+
}
468+
const iteratorId = nextPartId++;
469+
formData.append(formFieldPrefix + iteratorId, partJSON);
470+
return serializeIteratorID(iteratorId);
471+
}
472+
return Array.from((iterator: any));
452473
}
453474

454475
// Verify that this is a simple plain object.

packages/react-server-dom-webpack/src/__tests__/ReactFlightDOMReply-test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,35 @@ describe('ReactFlightDOMReply', () => {
9797
items.push(item);
9898
}
9999
expect(items).toEqual(['A', 'B', 'C']);
100+
101+
// Multipass
102+
const items2 = [];
103+
// eslint-disable-next-line no-for-of-loops/no-for-of-loops
104+
for (const item of iterable) {
105+
items2.push(item);
106+
}
107+
expect(items2).toEqual(['A', 'B', 'C']);
108+
});
109+
110+
it('can pass an iterator as a reply', async () => {
111+
const iterator = (function* () {
112+
yield 'A';
113+
yield 'B';
114+
yield 'C';
115+
})();
116+
117+
const body = await ReactServerDOMClient.encodeReply(iterator);
118+
const result = await ReactServerDOMServer.decodeReply(
119+
body,
120+
webpackServerMap,
121+
);
122+
123+
// The iterator should be the same as itself.
124+
expect(result[Symbol.iterator]()).toBe(result);
125+
126+
expect(Array.from(result)).toEqual(['A', 'B', 'C']);
127+
// We've already consumed this iterator.
128+
expect(Array.from(result)).toEqual([]);
100129
});
101130

102131
it('can pass weird numbers as a reply', async () => {

packages/react-server/src/ReactFlightReplyServer.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,12 @@ function parseModelString(
477477
});
478478
return data;
479479
}
480+
case 'i': {
481+
// Iterator
482+
const id = parseInt(value.slice(2), 16);
483+
const data = getOutlinedModel(response, id);
484+
return data[Symbol.iterator]();
485+
}
480486
case 'I': {
481487
// $Infinity
482488
return Infinity;

0 commit comments

Comments
 (0)