# zwire-wasm (Bridge)

> Optional wasm-bindgen bridge connecting a Rust WireScope directly to @zwire/core/protocol/wasm in browsers.

Expose an existing Rust `zwire::core::Scope` to JavaScript through wasm-bindgen. The bridge sends the same committed JSON snapshots and transaction batches as the native protocol. Each connection owns its own transaction history and FIFO. The optional crate builds independently from the dependency-free Rust workspace.

Return a `WireScope` from your wasm-bindgen domain object:

```rust
pub fn connect(&self, peer: String) -> zwire_wasm::WireScope {
    zwire_wasm::WireScope::connect(&self.scope, peer)
}
```

Connect it to the frontend after initializing the generated WASM module:

```ts
import { createScope } from '@zwire/core';
import { connectWasm } from '@zwire/core/protocol/wasm';

const scope = createScope();
const peer = engine.connect(crypto.randomUUID());
const connection = connectWasm(scope, peer);
engine.updateMarket();
connection.pull();
connection.dispose();
peer.free();
scope.dispose();
```

Use one `WireScope` per frontend connection. Local committed writes reach Rust synchronously. Call `pull()` after Rust commands to deliver their commits to JS, including commits produced by Rust subscribers. Dispose the connection before freeing the peer. The [order book example](https://github.com/kjmin-dev/zwire/blob/main/examples/wasm-orderbook) runs this lifecycle with a React UI.
