feat(realtime): add generating lifecycle state and event buffering#79
Merged
AdirAmsalem merged 2 commits intomainfrom Feb 10, 2026
Merged
feat(realtime): add generating lifecycle state and event buffering#79AdirAmsalem merged 2 commits intomainfrom
generating lifecycle state and event buffering#79AdirAmsalem merged 2 commits intomainfrom
Conversation
…ering Add 'generating' ConnectionState emitted when the server sends a generation_started signaling message, letting consumers know the session is live and frames are on their way. Event buffering defers connectionChange/error events that fire during connect() so listeners registered on the returned client never miss early state transitions. Events replay via a macrotask after connect() resolves. Changes: - ConnectionState type now includes 'generating' (moved to types.ts) - WebRTCConnection handles generation_started message, makes 'generating' sticky (only 'disconnected' can override) - connect() resolves on 'connected' OR 'generating' - isConnected()/assertConnected() accept 'generating' as a writable state - Events emitted during connect() are buffered and replayed after the client is returned to the caller - disconnect() clears the event buffer to prevent re-entrant replay - 4 new unit tests covering generating state and event replay
c91dc78 to
17a46d6
Compare
commit: |
0f97f80 to
6c2565b
Compare
6c2565b to
87b6fe3
Compare
generating lifecycle state and event buffering
tomershlasky
approved these changes
Feb 10, 2026
Contributor
tomershlasky
left a comment
There was a problem hiding this comment.
LGTM, letf 1 comment
Comment on lines
+347
to
+351
| const nextState = | ||
| s === "connected" ? "connected" : ["connecting", "new"].includes(s) ? "connecting" : "disconnected"; | ||
| // Keep "generating" sticky unless the connection is actually lost. | ||
| if (this.state === "generating" && nextState !== "disconnected") return; | ||
| this.setState(nextState); |
Contributor
There was a problem hiding this comment.
maybe some better if else for more readbility
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.
Summary
Adds
"generating"ConnectionStateand event buffering to the realtime client."generating"state: Emitted when the server sends ageneration_startedsignaling message, letting consumers know the session is live and frames are incoming.connect()now resolves on either"connected"or"generating"(whichever arrives first).connect()are buffered and replayed after the client is returned, so listeners registered on the returned client never miss early state transitions.What Changed
New:
"generating"ConnectionStateConnectionStatetype now includes"generating"(type moved fromwebrtc-connection.tstotypes.tsfor shared use)WebRTCConnectionhandlesgeneration_startedsignaling messages → sets state to"generating""generating"is sticky inonconnectionstatechange— only"disconnected"can override itisConnected()andassertConnected()accept"generating"as a writable stateconnect()resolves on"connected"or"generating"Usage
New: Event Buffering
connectionChangeanderrorevents emitted duringconnect()are bufferedconnect()resolves and returns the client, buffered events replay via a macrotask (setTimeout(0))buffering = falseis set inside the macrotask to prevent ordering issuesdisconnect()clears the event buffer to prevent re-entrant replayPublic API Changes
ConnectionStatetype: added"generating"— additive, non-breaking for most consumersswitchonConnectionStatewill get a compile error (intentional)Events["connectionChange"]: now typed asConnectionStateinstead of a hardcoded string unionTests
4 new unit tests:
connect()resolves when state reaches"generating""generating"→"disconnected"transition works correctly"generating"triggers reconnect flowconnect()returnsAll 80 tests pass. Typecheck and build clean.
Note
Medium Risk
Changes core realtime connection state handling and reconnect decisions; while additive, consumers with exhaustive
ConnectionStatehandling or reliance on event timing could see behavior changes.Overview
Adds a new realtime lifecycle state,
generating, surfaced via the exportedConnectionStatetype and emitted when the server sends ageneration_startedsignaling message;connect()now treatsgeneratingas an established state (handshake completion,isConnected(), reconnect gating, and method send preconditions).Updates the realtime client to buffer
connectionChange/errorevents duringconnect()and replay them asynchronously after the client is returned, preventing missed early state transitions; includes new unit tests coveringgeneratingtransitions/reconnect behavior and event replay ordering.Written by Cursor Bugbot for commit 6c2565b. This will update automatically on new commits. Configure here.