Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Test
  • Loading branch information
sebmarkbage committed Jun 17, 2025
commit 4a72faf53ea7362110bf340c15fe7b45a74ed545
13 changes: 13 additions & 0 deletions packages/react-noop-renderer/src/ReactNoopFlightServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type Options = {
environmentName?: string | (() => string),
filterStackFrame?: (url: string, functionName: string) => boolean,
identifierPrefix?: string,
signal?: AbortSignal,
onError?: (error: mixed) => void,
onPostpone?: (reason: string) => void,
};
Expand All @@ -87,6 +88,18 @@ function render(model: ReactClientValue, options?: Options): Destination {
__DEV__ && options ? options.environmentName : undefined,
__DEV__ && options ? options.filterStackFrame : undefined,
);
const signal = options ? options.signal : undefined;
if (signal) {
if (signal.aborted) {
ReactNoopFlightServer.abort(request, (signal: any).reason);
} else {
const listener = () => {
ReactNoopFlightServer.abort(request, (signal: any).reason);
signal.removeEventListener('abort', listener);
};
signal.addEventListener('abort', listener);
}
}
ReactNoopFlightServer.startWork(request);
ReactNoopFlightServer.startFlowing(request, destination);
return destination;
Expand Down
48 changes: 48 additions & 0 deletions packages/react-reconciler/src/__tests__/ReactCache-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ let React;
let ReactNoopFlightServer;
let ReactNoopFlightClient;
let cache;
let cacheSignal;

describe('ReactCache', () => {
beforeEach(() => {
Expand All @@ -25,6 +26,7 @@ describe('ReactCache', () => {
ReactNoopFlightClient = require('react-noop-renderer/flight-client');

cache = React.cache;
cacheSignal = React.cacheSignal;

jest.resetModules();
__unmockReact();
Expand Down Expand Up @@ -220,4 +222,50 @@ describe('ReactCache', () => {
expect(cachedFoo.length).toBe(0);
expect(cachedFoo.displayName).toBe(undefined);
});

it('cacheSignal() returns null outside a render', async () => {
expect(cacheSignal()).toBe(null);
});

it('cacheSignal() aborts when the render is aborted', async () => {
let renderedCacheSignal = null;

const promise = new Promise(() => {});

async function Test() {
renderedCacheSignal = cacheSignal();
await promise;
return 'Hi';
}

const controller = new AbortController();
const errors = [];
const result = ReactNoopFlightServer.render(<Test />, {
signal: controller.signal,
onError(x) {
errors.push(x);
return 'hi';
},
});
expect(errors).toEqual([]);
expect(renderedCacheSignal).not.toBe(controller.signal); // In the future we might make these the same
expect(renderedCacheSignal.aborted).toBe(false);
const reason = new Error('Timed out');
controller.abort(reason);
expect(errors).toEqual([reason]);
expect(renderedCacheSignal.aborted).toBe(true);
expect(renderedCacheSignal.reason).toBe(reason);

let clientError = null;
try {
await ReactNoopFlightClient.read(result);
} catch (x) {
clientError = x;
}
expect(clientError).not.toBe(null);
if (__DEV__) {
expect(clientError.message).toBe('Timed out');
}
expect(clientError.digest).toBe('hi');
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fatal error case is missing.

});