# Rust ↔ TypeScript Sync

> Live cross-process Rust server and TypeScript client synchronizing ReactiveCollection, dehydrate_json, OptimisticLayer rebase, and 64-bit sequence numbers.

A zero-external-dependency end-to-end example proving live wire-format interoperability between **Rust (`zwire`)** and **TypeScript (`@zwire/core`)** over `spec/protocol.md` JSON:

1. **Out-of-order hydration (`dehydrate_json` → `hydrate`):** The Rust server dehydrates committed state — including a `u64` sequence number above `Number.MAX_SAFE_INTEGER` (`9007199254740995`, encoded as a decimal string for lossless JS `BigInt` decoding) and a `ReactiveCollection` Top-K leader — and streams it to the TypeScript client before the client registers its signals.
2. **Concurrent server push + optimistic rebase (`SyncBridge` → `OptimisticLayer`):** While the TypeScript client holds two overlapping optimistic layers (`-150` and `-50`), the Rust server processes an authoritative deposit (`+500`) and orderbook update (`top_bid = 125`), emitting a coalesced `StatePatch[]` batch. The TypeScript `SyncBridge` applies the remote patches and automatically rebases both active optimistic layers with zero lost updates.
3. **Bidirectional patch sync (`@zwire/core/protocol` → `zwire-protocol`):** A local client mutation flushes as `StatePatch[]` back to Rust, where `StatePatch::vec_from_json` decodes and applies it via Rust's `SyncBridge` while suppressing echoes.

## Running

From the repository root:

```bash
mise run test:examples
# or directly:
node ./examples/rust-ts-sync/run.ts
```
