Skip to content

Commit 9b50003

Browse files
authoredJun 27, 2024··
fix (ai/react): useObject stop & isLoading reset at end of stream (#2126)
1 parent 83204b7 commit 9b50003

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed
 

‎.changeset/gentle-dolphins-boil.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ai-sdk/react': patch
3+
---
4+
5+
fix (ai/react): useObject stop & isLoading reset at end of stream

‎packages/react/src/use-object.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,17 @@ function useObject<RESULT, INPUT = any>({
148148
mutate(currentObject);
149149
}
150150
},
151+
152+
close() {
153+
setIsLoading(false);
154+
abortControllerRef.current = null;
155+
},
151156
}),
152157
);
153158

154159
setError(undefined);
155160
} catch (error) {
156161
setError(error);
157-
} finally {
158-
setIsLoading(false);
159-
abortControllerRef.current = null;
160162
}
161163
};
162164

‎packages/react/src/use-object.ui.test.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,10 @@ describe('text stream', () => {
111111
streamController.enqueue('"Hello, world!"}');
112112
streamController.close();
113113

114-
await screen.findByTestId('loading');
115-
expect(screen.getByTestId('loading')).toHaveTextContent('false');
114+
// wait for element "loading" to have text content "false":
115+
await waitFor(() => {
116+
expect(screen.getByTestId('loading')).toHaveTextContent('true');
117+
});
116118
});
117119
});
118120

0 commit comments

Comments
 (0)
Please sign in to comment.