# Store and synchronization reference

State operations, snapshots, notifications, history, and synchronization sessions with explicit cleanup.

The store works with data paths and detached snapshots. Its application facade exposes `get`, `getSnapshot`, `set`, `update`, `on`, and `batch`; administration, history, and synchronization operations belong to the host.

<span id="elegir-la-operación"></span>

<span class="section-anchor" id="section-1"></span>

## Choose the operation

Use `set` to replace data, `update` to compute it synchronously, and `batch` to validate several writes before performing the first. `on` delivers notifications in microtasks; `tap` observes each commit synchronously. `hydrate` replaces the root without adding ring buffer entries or passing through taps.

Values accept plain data, dense arrays, and codec scalars. Renderer objects, functions, Date, Map, cycles, and promises are not shared-state values. A bigint scalar supported by the store is not necessarily serializable by sync.

<span id="una-sesión-con-dueño"></span>

<span class="section-anchor" id="section-2"></span>

## A session with an owner

```js
import { wu } from 'wu-framework';

const sync = wu.store.sync({ transport: 'broadcast', room: 'catalog' });
await sync.ready();
wu.store.set('catalog.selectedId', 'p-42');
console.log(sync.status());
// Al terminar la sesión:
sync.stop();
```

Sync records only writes made after activation. Last Writer Wins ordering applies per path; concurrent parent and child writes do not offer the same convergence as writes to the same path. Keep granularity consistent and authenticate the transport where appropriate.

<span class="section-anchor" id="section-3"></span>

## new WuStore

Creates an independent store with circular history.

`new WuStore(bufferSize?: number): WuStore`

- **Parameters:** bufferSize defaults to 256; selects history capacity, not a state byte limit.
- **Returns:** Instance; buffer size rounded to a power of two.
- **Errors:** Values written later are validated by the store codec.
- **Permissions:** Host only; unavailable in the application facade.
- **Cleanup:** state.dispose() removes sync, listeners, and taps.

```js
import { WuStore } from 'wu-framework';
const state = new WuStore(512);
```

Reference: `src/core/wu-store.js`.

<span class="section-anchor" id="section-4"></span>

## wu.store.tap

Observes each commit synchronously, before on notifications.

`wu.store.tap(callback): () => void`

- **Parameters:** callback(sequence,path,value); useful for a journal or transport.
- **Returns:** Cleanup; a non-function argument produces an empty cleanup function.
- **Errors:** Does not filter by path or deliver an initial snapshot.
- **Permissions:** Host only; unavailable in the application facade.
- **Cleanup:** stop() removes the tap.

```js
const stop = wu.store.tap((sequence, path, value) => {
  console.log(sequence, path, value);
});
```

Reference: `src/core/wu-store.js`.

<span class="section-anchor" id="section-5"></span>

## wu.store.hydrate

Replaces the entire state and notifies selected paths.

`wu.store.hydrate(state, { notifyPaths? }?): void`

- **Parameters:** state: plain object; notifyPaths defaults to the new state's top-level keys.
- **Returns:** void; does not add ring buffer entries or trigger taps.
- **Errors:** WU_STORE_INVALID_VALUE or WU_STORE_INVALID_PATH before replacing state.
- **Permissions:** Host only; unavailable in the application facade.
- **Cleanup:** Creates no resources requiring release.

```js
wu.store.hydrate({ cart: { count: 2 } }, { notifyPaths: ['cart.count'] });
```

Reference: `src/core/wu-store.js`.

<span class="section-anchor" id="section-6"></span>

## wu.store.getMetrics

Reads counts for reads, writes, notifications, history, and listeners.

`wu.store.getMetrics(): object`

- **Parameters:** No arguments.
- **Returns:** Metrics snapshot.
- **Errors:** Does not measure network or rendering.
- **Permissions:** Host only; unavailable in the application facade.
- **Cleanup:** Creates no resources requiring release.

```js
console.log(wu.store.getMetrics().listenerCount);
```

Reference: `src/core/wu-store.js`.

<span class="section-anchor" id="section-7"></span>

## wu.store.getRecentEvents

Queries recent writes with sequence, path, value, and timestamp.

`wu.store.getRecentEvents(count?: number): object[]`

- **Parameters:** Finite positive count, default 10, capped by the buffer.
- **Returns:** List with newest first; values detached from internal state.
- **Errors:** Nonpositive or nonfinite values return [].
- **Permissions:** Host only; unavailable in the application facade.
- **Cleanup:** Creates no resources requiring release.

```js
console.table(wu.store.getRecentEvents(10));
```

Runtime method not listed in the WuStore declaration.

Reference: `src/core/wu-store.js`.

<span class="section-anchor" id="section-8"></span>

## wu.store.reset

Clears state and history while keeping subscriptions and taps.

`wu.store.reset(): void`

- **Parameters:** No arguments.
- **Returns:** void.
- **Errors:** Does not constitute persistent deletion outside this instance.
- **Permissions:** Host only; unavailable in the application facade.
- **Cleanup:** Does not automatically notify an empty initial value; sync remains active.

```js
wu.store.reset();
```

Reference: `src/core/wu-store.js`.

<span class="section-anchor" id="section-9"></span>

## wu.store.dispose

Stops sync and removes state, listeners, and taps.

`wu.store.dispose(): void`

- **Parameters:** No arguments.
- **Returns:** void.
- **Errors:** Does not constitute persistent deletion outside this instance.
- **Permissions:** Host only; unavailable in the application facade.
- **Cleanup:** Use when removing a store you manage; not on a store shared by another consumer.

```js
wu.store.dispose();
```

Reference: `src/core/wu-store.js`.

<span class="section-anchor" id="section-10"></span>

## wu.store.clear

Performs the same complete release as dispose.

`wu.store.clear(): void`

- **Parameters:** No arguments.
- **Returns:** void.
- **Errors:** Does not constitute persistent deletion outside this instance.
- **Permissions:** Host only; unavailable in the application facade.
- **Cleanup:** Also removes subscriptions; it does more than operate on values.

```js
wu.store.clear();
```

Reference: `src/core/wu-store.js`.

<span class="section-anchor" id="section-11"></span>

## WuSyncHandle.ready

Waits for the module to load and the transport to start.

`sync.ready(): Promise<unknown>`

- **Parameters:** Handle returned by wu.store.sync(options).
- **Returns:** Promise<unknown>
- **Errors:** ready may reject; status.error exposes loading/authorization/transport errors.
- **Permissions:** Host session; the relay must authenticate peers when using WebSocket.
- **Cleanup:** stop() cancels the session when its owner finishes.

```js
await sync.ready();
```

Reference: `src/core/wu-store.js`.

<span class="section-anchor" id="section-12"></span>

## WuSyncHandle.status

Queries connection, clocks, peers, and synchronization counters.

`sync.status(): WuSyncStatus`

- **Parameters:** Handle returned by wu.store.sync(options).
- **Returns:** WuSyncStatus
- **Errors:** ready may reject; status.error exposes loading/authorization/transport errors.
- **Permissions:** Host session; the relay must authenticate peers when using WebSocket.
- **Cleanup:** Read operation without additional resources.

```js
console.log(sync.status());
```

Reference: `src/core/wu-store.js`.

<span class="section-anchor" id="section-13"></span>

## WuSyncHandle.stop

Stops message exchange and write observation.

`sync.stop(): void`

- **Parameters:** Handle returned by wu.store.sync(options).
- **Returns:** void
- **Errors:** ready may reject; status.error exposes loading/authorization/transport errors.
- **Permissions:** Host session; the relay must authenticate peers when using WebSocket.
- **Cleanup:** Removes the tap and closes this session's transport.

```js
sync.stop();
```

Reference: `src/core/wu-store.js`.

<span class="section-anchor" id="section-14"></span>

## WuSyncHandle.connected

Checks whether the session transport is connected.

`sync.connected: boolean`

- **Parameters:** Handle returned by wu.store.sync(options).
- **Returns:** boolean
- **Errors:** ready may reject; status.error exposes loading/authorization/transport errors.
- **Permissions:** Host session; the relay must authenticate peers when using WebSocket.
- **Cleanup:** State read; release the session with stop.

```js
console.log(sync.connected);
```

Reference: `src/core/wu-store.js`.