Skip to content

Commit

Permalink
Add iterator support as Replies
Browse files Browse the repository at this point in the history
  • Loading branch information
sebmarkbage committed Mar 10, 2023
1 parent 8f2f516 commit cd0ba41
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
7 changes: 7 additions & 0 deletions packages/react-client/src/ReactFlightReplyClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
REACT_ELEMENT_TYPE,
REACT_LAZY_TYPE,
REACT_PROVIDER_TYPE,
getIteratorFn,
} from 'shared/ReactSymbols';

import {
Expand Down Expand Up @@ -159,6 +160,12 @@ export function processReply(
);
return serializePromiseID(promiseId);
}
if (!isArray(value)) {
const iteratorFn = getIteratorFn(value);
if (iteratorFn) {
return Array.from((value: any));
}
}

if (__DEV__) {
if (value !== null && !isArray(value)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ global.TextDecoder = require('util').TextDecoder;

// let serverExports;
let webpackServerMap;
let act;
let ReactServerDOMServer;
let ReactServerDOMClient;

describe('ReactFlightDOMReply', () => {
beforeEach(() => {
jest.resetModules();
act = require('internal-test-utils').act;
const WebpackMock = require('./utils/WebpackMock');
// serverExports = WebpackMock.serverExports;
webpackServerMap = WebpackMock.webpackServerMap;
Expand Down Expand Up @@ -57,4 +55,23 @@ describe('ReactFlightDOMReply', () => {
expect('3' in object.array).toBe(false);
expect('prop' in object).toBe(false);
});

it('can pass an iterable as a reply', async () => {
const body = await ReactServerDOMClient.encodeReply({
[Symbol.iterator]: function* () {
yield 'A';
yield 'B';
yield 'C';
},
});
const iterable = await ReactServerDOMServer.decodeReply(
body,
webpackServerMap,
);
const items = [];
for (const item of iterable) {
items.push(item);
}
expect(items).toEqual(['A', 'B', 'C']);
});
});

0 comments on commit cd0ba41

Please sign in to comment.