Skip to content

[Flight] Simulate fetch to third party in fixture #33484

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions fixtures/flight/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async function Bar({children}) {
}

async function ThirdPartyComponent() {
return delay('hello from a 3rd party', 30);
return await delay('hello from a 3rd party', 30);
}

let cachedThirdPartyStream;
Expand All @@ -52,16 +52,35 @@ let cachedThirdPartyStream;
// That way it gets the owner from the call to createFromNodeStream.
const thirdPartyComponent = <ThirdPartyComponent />;

function fetchThirdParty(noCache) {
function simulateFetch(cb, latencyMs) {
return new Promise(resolve => {
// Request latency
setTimeout(() => {
const result = cb();
// Response latency
setTimeout(() => {
resolve(result);
}, latencyMs);
}, latencyMs);
});
}

async function fetchThirdParty(noCache) {
// We're using the Web Streams APIs for tee'ing convenience.
const stream =
cachedThirdPartyStream && !noCache
? cachedThirdPartyStream
: renderToReadableStream(
let stream;
if (cachedThirdPartyStream && !noCache) {
stream = cachedThirdPartyStream;
} else {
stream = await simulateFetch(
() =>
renderToReadableStream(
thirdPartyComponent,
{},
{environmentName: 'third-party'}
);
),
25
);
}

const [stream1, stream2] = stream.tee();
cachedThirdPartyStream = stream1;
Expand Down
Loading