Skip to content

Commit

Permalink
[Obs AI Assistant] Abort controller when component unmounts (#172870)
Browse files Browse the repository at this point in the history
When the component unmounts (for instance when moving to a different
chat or navigating to another page), the request to /complete should be
aborted. This PR ensures that that is the case and adds a test for it.
  • Loading branch information
dgieselaar authored Dec 8, 2023
1 parent 4ffaf9f commit c0ab01a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,25 @@ describe('useChat', () => {
});
});

describe('after unmounting the component', () => {
beforeEach(() => {
act(() => {
subject.next({
type: StreamingChatResponseEventType.ChatCompletionChunk,
id: 'my-message-id',
message: {
content: 'good',
},
});
hookResult.unmount();
});
});

it('shows the partial message and sets chatState to aborted', () => {
expect(mockChatService.complete.mock.lastCall?.[0].signal.aborted).toBe(true);
});
});

describe('after a response errors out', () => {
beforeEach(() => {
act(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,8 @@ export function useChat({
);

useEffect(() => {
const controller = abortControllerRef.current;
return () => {
controller.abort();
abortControllerRef.current.abort();
};
}, []);

Expand Down

0 comments on commit c0ab01a

Please sign in to comment.