# zidane-ui/store

Import: `import { … } from 'zidane-ui/store'`. Source: [ui/src/store/index.ts](https://github.com/Tahul/zidane/blob/main/ui/src/store/index.ts).

## Functions

### `constant`

Function.

A store that never changes.

```ts
function constant<T>(value: T): Readable<T>
```

**Type parameters**

- `T`

**Parameters**

- `value` (`T`): The value the store always returns.

**Returns** `Readable<T>`

Source: [ui/src/store/store.ts:52](https://github.com/Tahul/zidane/blob/main/ui/src/store/store.ts#L52)

### `createKeyedStore`

Function.

Creates a [`KeyedStore`](https://zidane.si/docs/reference/ui-store.md#KeyedStore).

```ts
function createKeyedStore<V, Fallback = null>(
  fallback?: Fallback,
): KeyedStore<V, Fallback>
```

**Type parameters**

- `V`
- `Fallback` (default `null`)

**Parameters**

- `fallback?` (`Fallback`): What a key's `readable` reads while it holds no value. Default: `null`.

**Returns** `KeyedStore<V, Fallback>`

Source: [ui/src/store/keyed.ts:31](https://github.com/Tahul/zidane/blob/main/ui/src/store/keyed.ts#L31)

### `createStorageRecord`

Function.

Creates a [`StorageRecord`](https://zidane.si/docs/reference/ui-store.md#StorageRecord). It reads the storage on first use.

```ts
function createStorageRecord<T>(options: StorageRecordOptions<T>): StorageRecord<T>
```

**Type parameters**

- `T`

**Parameters**

- `options` (`StorageRecordOptions<T>`): The key, the version, the fallback, how to validate and migrate, and where it lives.

**Returns** `StorageRecord<T>`

Source: [ui/src/store/storage-record.ts:138](https://github.com/Tahul/zidane/blob/main/ui/src/store/storage-record.ts#L138)

### `pageStorage`

Function.

The page's `localStorage` (or its `sessionStorage`), or `null` where there is none: outside a browser, or in a
page where storage is disabled (privacy mode, a sandboxed frame), whose getter throws.

```ts
function pageStorage(area?: 'localStorage' | 'sessionStorage'): Storage | null
```

**Parameters**

- `area?` (`'localStorage' | 'sessionStorage'`, default `'localStorage'`): Which of the page's storages. Default `localStorage`.

**Returns** `Storage | null`

Source: [ui/src/store/page-storage.ts:13](https://github.com/Tahul/zidane/blob/main/ui/src/store/page-storage.ts#L13)

### `writable`

Function.

A plain value store: the building block for simple hosts and fixtures.

```ts
function writable<T>(initial: T): Writable<T>
```

**Type parameters**

- `T`

**Parameters**

- `initial` (`T`): The starting value.

**Returns** `Writable<T>`

Source: [ui/src/store/store.ts:28](https://github.com/Tahul/zidane/blob/main/ui/src/store/store.ts#L28)

## Interfaces

### `KeyedStore`

Interface.

Values by key, each key with its own subscribers.

```ts
interface KeyedStore<V, Fallback = null>
```

**Type parameters**

- `V`
- `Fallback` (default `null`)

**Properties**

- `clear` (`() => void`): Forgets every key.
- `delete` (`(key: string) => void`): Forgets `key` (a deleted session): its readers see the fallback.
- `get` (`(key: string) => V | undefined`): The value under `key`, `undefined` when unset.
- `readable` (`(key: string) => Readable<V | Fallback>`): The store of one key, the same object for the same key. Reads the fallback while unset.
- `set` (`(key: string, value: V) => void`): Sets the value under `key`; its readers hear it when it changed by identity.
- `subscribe` (`(key: string, listener: () => void) => () => void`): Calls `listener` whenever `key` changes; returns the unsubscribe.

Source: [ui/src/store/keyed.ts:11](https://github.com/Tahul/zidane/blob/main/ui/src/store/keyed.ts#L11)

### `Readable`

Interface.

The shape every host exposes its data in: read the current value and subscribe to changes.

```ts
interface Readable<T>
```

**Type parameters**

- `T`

**Properties**

- `get` (`() => T`): Returns the current value.
- `subscribe` (`(onChange: () => void) => () => void`): Registers a listener called after the value changes. Returns a function that removes the listener.

Source: [ui/src/store/store.ts:10](https://github.com/Tahul/zidane/blob/main/ui/src/store/store.ts#L10)

### `StorageRecord`

Interface.

A value kept in a `Storage`; see [`createStorageRecord`](https://zidane.si/docs/reference/ui-store.md#createStorageRecord).

```ts
interface StorageRecord<T> extends Writable<T>
```

**Type parameters**

- `T`

**Properties**

- `clear` (`() => void`): Removes the stored value: the record reads its fallback.
- `get` (`() => T`, from `Writable`): Returns the current value.
- `set` (`(next: T | ((prev: T) => T)) => void`, from `Writable`): Replaces the value, or computes the next one from the previous. Listeners run only when the value changes by `Object.is`.
- `subscribe` (`(onChange: () => void) => () => void`, from `Writable`): Registers a listener called after the value changes. Returns a function that removes the listener.

Source: [ui/src/store/storage-record.ts:92](https://github.com/Tahul/zidane/blob/main/ui/src/store/storage-record.ts#L92)

### `StorageRecordBaseOptions`

Interface.

The options every [`createStorageRecord`](https://zidane.si/docs/reference/ui-store.md#createStorageRecord) takes, whatever its format.

```ts
interface StorageRecordBaseOptions<T>
```

**Type parameters**

- `T`

**Properties**

- `equals?` (`(a: T, b: T) => boolean`): Whether two values are the same, so a write or another tab's change of nothing notifies nobody. Default: equal JSON.
- `fallback` (`T`): What the record reads while nothing valid is stored.
- `key` (`string`): The storage key.
- `legacy?` (`readonly StorageRecordLegacy<T>[]`): Older keys or formats, tried in order the first time nothing valid is stored.
- `storage?` (`RecordStorage | (() => RecordStorage | null | undefined) | null`): Where the value lives: a storage, or a function read on each access. `null`: memory only.
  Default: the page's `localStorage` when there is one.
- `watch?` (`((key: string, onChange: () => void) => () => void) | null`): Calls `onChange` when another page may have changed `key`; returns how to stop. `null`: never.
  Default: the window's `storage` event.
- `writeError?` (`'keep' | 'throw'`): What a write the storage refuses (full, blocked) does. `'keep'` (default): the value lives in memory for
  this page. `'throw'`: `set` throws the storage's error and the record keeps the stored value, for data
  whose loss the caller reports.

Source: [ui/src/store/storage-record.ts:43](https://github.com/Tahul/zidane/blob/main/ui/src/store/storage-record.ts#L43)

### `StorageRecordCodec`

Interface.

A record's own text format, in place of the envelope: the record stores what `encode` gives and reads it back with `decode`.

```ts
interface StorageRecordCodec<T>
```

**Type parameters**

- `T`

**Properties**

- `decode` (`(raw: string) => T | undefined`): The value in the stored text; `undefined` when it holds nothing usable.
- `encode` (`(value: T) => string`): The text stored for `value`.

Source: [ui/src/store/storage-record.ts:35](https://github.com/Tahul/zidane/blob/main/ui/src/store/storage-record.ts#L35)

### `StorageRecordCodecOptions`

Interface.

A record stored in its own text format; the format carries its own version, if any.

```ts
interface StorageRecordCodecOptions<T> extends StorageRecordBaseOptions<T>
```

**Type parameters**

- `T`

**Properties**

- `codec` (`StorageRecordCodec<T>`): How the value is written and read.
- `equals?` (`(a: T, b: T) => boolean`, from `StorageRecordBaseOptions`): Whether two values are the same, so a write or another tab's change of nothing notifies nobody. Default: equal JSON.
- `fallback` (`T`, from `StorageRecordBaseOptions`): What the record reads while nothing valid is stored.
- `key` (`string`, from `StorageRecordBaseOptions`): The storage key.
- `legacy?` (`readonly StorageRecordLegacy<T>[]`, from `StorageRecordBaseOptions`): Older keys or formats, tried in order the first time nothing valid is stored.
- `storage?` (`RecordStorage | (() => RecordStorage | null | undefined) | null`, from `StorageRecordBaseOptions`): Where the value lives: a storage, or a function read on each access. `null`: memory only.
  Default: the page's `localStorage` when there is one.
- `watch?` (`((key: string, onChange: () => void) => () => void) | null`, from `StorageRecordBaseOptions`): Calls `onChange` when another page may have changed `key`; returns how to stop. `null`: never.
  Default: the window's `storage` event.
- `writeError?` (`'keep' | 'throw'`, from `StorageRecordBaseOptions`): What a write the storage refuses (full, blocked) does. `'keep'` (default): the value lives in memory for
  this page. `'throw'`: `set` throws the storage's error and the record keeps the stored value, for data
  whose loss the caller reports.

Source: [ui/src/store/storage-record.ts:83](https://github.com/Tahul/zidane/blob/main/ui/src/store/storage-record.ts#L83)

### `StorageRecordEnvelopeOptions`

Interface.

A record stored in the versioned envelope `{ "v": version, "data": … }`.

```ts
interface StorageRecordEnvelopeOptions<T> extends StorageRecordBaseOptions<T>
```

**Type parameters**

- `T`

**Properties**

- `codec?` (`undefined`): No codec: the envelope.
- `equals?` (`(a: T, b: T) => boolean`, from `StorageRecordBaseOptions`): Whether two values are the same, so a write or another tab's change of nothing notifies nobody. Default: equal JSON.
- `fallback` (`T`, from `StorageRecordBaseOptions`): What the record reads while nothing valid is stored.
- `key` (`string`, from `StorageRecordBaseOptions`): The storage key.
- `legacy?` (`readonly StorageRecordLegacy<T>[]`, from `StorageRecordBaseOptions`): Older keys or formats, tried in order the first time nothing valid is stored.
- `migrate?` (`(data: unknown, version: number) => T | undefined`): Upgrades the data of an older version; `undefined` drops it. Default: older versions read as the fallback.
- `parse` (`(data: unknown) => T | undefined`): Validates the stored data of the current version; `undefined` for anything unusable.
- `storage?` (`RecordStorage | (() => RecordStorage | null | undefined) | null`, from `StorageRecordBaseOptions`): Where the value lives: a storage, or a function read on each access. `null`: memory only.
  Default: the page's `localStorage` when there is one.
- `version` (`number`): The version written in the envelope. Raise it when the stored shape changes, and `migrate` the older ones.
- `watch?` (`((key: string, onChange: () => void) => () => void) | null`, from `StorageRecordBaseOptions`): Calls `onChange` when another page may have changed `key`; returns how to stop. `null`: never.
  Default: the window's `storage` event.
- `writeError?` (`'keep' | 'throw'`, from `StorageRecordBaseOptions`): What a write the storage refuses (full, blocked) does. `'keep'` (default): the value lives in memory for
  this page. `'throw'`: `set` throws the storage's error and the record keeps the stored value, for data
  whose loss the caller reports.

Source: [ui/src/store/storage-record.ts:71](https://github.com/Tahul/zidane/blob/main/ui/src/store/storage-record.ts#L71)

### `StorageRecordLegacy`

Interface.

Where a record read its value before it lived under its key in the envelope.

```ts
interface StorageRecordLegacy<T>
```

**Type parameters**

- `T`

**Properties**

- `key` (`string`): The old key; the record's own key for an older format under the same key.
- `read` (`(raw: string) => T | undefined`): The value in the old stored text; `undefined` when it holds nothing usable.

Source: [ui/src/store/storage-record.ts:27](https://github.com/Tahul/zidane/blob/main/ui/src/store/storage-record.ts#L27)

### `Writable`

Interface.

A [`Readable`](https://zidane.si/docs/reference/ui-store.md#Readable) you can also write to.

```ts
interface Writable<T> extends Readable<T>
```

**Type parameters**

- `T`

**Properties**

- `get` (`() => T`, from `Readable`): Returns the current value.
- `set` (`(next: T | ((prev: T) => T)) => void`): Replaces the value, or computes the next one from the previous. Listeners run only when the value changes by `Object.is`.
- `subscribe` (`(onChange: () => void) => () => void`, from `Readable`): Registers a listener called after the value changes. Returns a function that removes the listener.

Source: [ui/src/store/store.ts:18](https://github.com/Tahul/zidane/blob/main/ui/src/store/store.ts#L18)

## Types

### `RecordStorage`

Type.

The `Storage` calls a record makes (`localStorage`, `sessionStorage` or a double).

```ts
type RecordStorage = Pick<Storage, 'getItem' | 'setItem'> & Partial<Pick<Storage, 'removeItem'>>
```

Source: [ui/src/store/storage-record.ts:24](https://github.com/Tahul/zidane/blob/main/ui/src/store/storage-record.ts#L24)

### `StorageRecordOptions`

Type.

Options of [`createStorageRecord`](https://zidane.si/docs/reference/ui-store.md#createStorageRecord): the envelope (`version`, `parse`) or a format of its own (`codec`).

```ts
type StorageRecordOptions<T> =
  | StorageRecordEnvelopeOptions<T>
  | StorageRecordCodecOptions<T>
```

**Type parameters**

- `T`

Source: [ui/src/store/storage-record.ts:89](https://github.com/Tahul/zidane/blob/main/ui/src/store/storage-record.ts#L89)
