useChat external store on ChatClient (approach 2/3: subscribe/getSnapshot) - #6
Draft
tombeckenham wants to merge 1 commit into
Draft
Conversation
Promote the external-store shape out of the React hook and onto ChatClient
itself, so every framework binding can share one subscribe/getSnapshot pair
instead of re-wiring seven onXChange callbacks.
ai-client:
- Add ChatClientSnapshot (immutable projection of the reactive state) and
ChatClientStore { subscribe, getSnapshot }, exported from the package.
- ChatClient gains a `store` field backed by the existing setX mutators: each
of setIsLoading/setStatus/setIsSubscribed/setConnectionStatus/
setSessionGenerating/setError and the messages handler now also calls
emitStoreChange(), which rebuilds the cached snapshot and notifies listeners.
- Named `store` (not `subscribe`) to avoid colliding with the existing
live-subscription `subscribe()`.
ai-react:
- useChat drops the hook-local store from the previous rewrite and reads
client.store via useSyncExternalStore. The hook no longer passes any
onXChange callback — only genuine side-effect callbacks remain.
The onXChange callbacks stay intact for existing (solid/vue/svelte/persistor)
consumers; the store is additive. All 376 ai-client + 128 ai-react tests pass.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
🚀 Changeset Version Preview6 package(s) bumped directly, 5 bumped as dependents. 🟥 Major bumps
🟨 Minor bumps
🟩 Patch bumps
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Promote the external store onto
ChatClientApproach 1 (#5) kept the store inside the React hook. But the Solid hook re-implements the identical seven-channel wiring with
createSignal, and Vue/Svelte do the same. This PR moves it ontoChatClientso every framework binding shares one primitive.ai-clientChatClientSnapshot(immutable projection of the reactive state) andChatClientStore { subscribe, getSnapshot }.readonly storefield toChatClient, backed by the existingsetXmutators — each ofsetIsLoading/setStatus/setIsSubscribed/setConnectionStatus/setSessionGenerating/setErrorand the messages handler also callsemitStoreChange(), which rebuilds the cached snapshot and notifies listeners.store(notsubscribe) to avoid colliding with the existing live-subscriptionsubscribe().onXChangecallbacks stay intact for existing (Solid/Vue/Svelte/persistor) consumers — the store is purely additive.ai-reactThe hook collapses to:
No
onXChangecallbacks wired into React at all — only genuine side-effect callbacks remain.Trade-off vs approach 3 (
@tanstack/store)Zero new dependencies, but the subscribe/getSnapshot plumbing and snapshot caching are hand-written, and there's no built-in selector support (any change re-renders the whole consumer).
Verification
tsc, ESLint (0 errors), all 376ai-client+ 128ai-reacttests pass. (Typecheck requires buildingai-clientfirst, sinceai-reactresolves it viadisttypes — CI'stest:prdoes this via thebuilddependency.)🤖 Generated with Claude Code