Add waitForSync method to TLSyncClient
Problem
When using TLSyncClient from Node.js (e.g. for long-running agents interacting with a canvas via WebSockets), there's no way to know when local changes have been acknowledged by the server. This matters for two scenarios:
- Before making a server request: the client needs to ensure its writes are synced so the server doesn't read stale data
- Before disconnecting: the server-side client needs to ensure all changes are flushed before closing the connection
Currently, TLSyncClient tracks pendingPushRequests and unsentChanges internally but doesn't expose any completion signal. Changes are throttled via FpsScheduler and acknowledged via push_result messages, but consumers can't await this cycle.
Proposed solution
Add a waitForSync(): Promise<void> method that resolves once all currently pending changes (both unsent/buffered and sent-but-unacknowledged) have been acknowledged by the server. Resolves immediately if nothing is pending.
A community member has implemented this as a local patch: https://gist.github.com/mbullington/19b8cad2416341488f7a2350daa680d3
The approach:
- Track
lastAcknowledgedClientClock(updated whenpush_resultis received during rebase) - Determine the target clock:
this.clientClockif there are unsent changes, or the last pending request's clock otherwise - If already acknowledged, resolve immediately
- Otherwise, queue a listener that resolves when that clock is acknowledged
store.put([myNewShape])
await syncClient.waitForSync()
// all changes acknowledged by serverUse cases
- Node.js agents that write to a tldraw room and need to confirm persistence before proceeding
- Server-side sync clients that need to flush before disconnecting
- Any programmatic workflow where fire-and-forget isn't sufficient
References
Source: tldraw/tldraw