Skip to content

zidane-ui/store

import { … } from 'zidane-ui/store'ui/src/store/index.ts

Functions#

constantFunction#

A store that never changes.

TypeScript
function constant<T>(value: T): Readable<T>
Type parameters
NameDescription
T
Parameters
NameTypeDescription
valueT

The value the store always returns.

ui/src/store/store.ts:52

createKeyedStoreFunction#

Creates a KeyedStore.

TypeScript
function createKeyedStore<V, Fallback = null>(
  fallback?: Fallback,
): KeyedStore<V, Fallback>
Type parameters
NameDefaultDescription
V
Fallbacknull
Parameters
NameTypeDescription
fallback?Fallback

What a key's readable reads while it holds no value. Default: null.

KeyedStore<V, Fallback>

ui/src/store/keyed.ts:31

createStorageRecordFunction#

Creates a StorageRecord. It reads the storage on first use.

TypeScript
function createStorageRecord<T>(options: StorageRecordOptions<T>): StorageRecord<T>
Type parameters
NameDescription
T
Parameters
NameTypeDescription
optionsStorageRecordOptions<T>

The key, the version, the fallback, how to validate and migrate, and where it lives.

ui/src/store/storage-record.ts:138

pageStorageFunction#

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.

TypeScript
function pageStorage(area?: 'localStorage' | 'sessionStorage'): Storage | null
Parameters
NameTypeDefaultDescription
area?'localStorage' | 'sessionStorage''localStorage'

Which of the page's storages. Default localStorage.

Storage | null

ui/src/store/page-storage.ts:13

writableFunction#

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

TypeScript
function writable<T>(initial: T): Writable<T>
Type parameters
NameDescription
T
Parameters
NameTypeDescription
initialT

The starting value.

ui/src/store/store.ts:28

Interfaces#

KeyedStoreInterface#

Values by key, each key with its own subscribers.

TypeScript
interface KeyedStore<V, Fallback = null>
KeyedStore type parameters
NameDefaultDescription
V
Fallbacknull
KeyedStore properties
NameTypeDescription
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.

ui/src/store/keyed.ts:11

ReadableInterface#

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

TypeScript
interface Readable<T>
Readable type parameters
NameDescription
T
Readable properties
NameTypeDescription
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.

ui/src/store/store.ts:10

StorageRecordInterface#

A value kept in a Storage; see createStorageRecord.

TypeScript
interface StorageRecord<T> extends Writable<T>
StorageRecord type parameters
NameDescription
T
StorageRecord properties
NameTypeDescription
clear() => void

Removes the stored value: the record reads its fallback.

getfrom Writable() => T

Returns the current value.

setfrom Writable(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.

subscribefrom Writable(onChange: () => void) => () => void

Registers a listener called after the value changes. Returns a function that removes the listener.

ui/src/store/storage-record.ts:92

StorageRecordBaseOptionsInterface#

The options every createStorageRecord takes, whatever its format.

TypeScript
interface StorageRecordBaseOptions<T>
StorageRecordBaseOptions type parameters
NameDescription
T
StorageRecordBaseOptions properties
NameTypeDescription
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.

fallbackT

What the record reads while nothing valid is stored.

keystring

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.

ui/src/store/storage-record.ts:43

StorageRecordCodecInterface#

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

TypeScript
interface StorageRecordCodec<T>
StorageRecordCodec type parameters
NameDescription
T
StorageRecordCodec properties
NameTypeDescription
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.

ui/src/store/storage-record.ts:35

StorageRecordCodecOptionsInterface#

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

TypeScript
interface StorageRecordCodecOptions<T> extends StorageRecordBaseOptions<T>
StorageRecordCodecOptions type parameters
NameDescription
T
StorageRecordCodecOptions properties
NameTypeDescription
codecStorageRecordCodec<T>

How the value is written and read.

equals?from StorageRecordBaseOptions(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.

fallbackfrom StorageRecordBaseOptionsT

What the record reads while nothing valid is stored.

keyfrom StorageRecordBaseOptionsstring

The storage key.

legacy?from StorageRecordBaseOptionsreadonly StorageRecordLegacy<T>[]

Older keys or formats, tried in order the first time nothing valid is stored.

storage?from StorageRecordBaseOptionsRecordStorage | (() => 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?from StorageRecordBaseOptions((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?from StorageRecordBaseOptions'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.

ui/src/store/storage-record.ts:83

StorageRecordEnvelopeOptionsInterface#

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

TypeScript
interface StorageRecordEnvelopeOptions<T> extends StorageRecordBaseOptions<T>
StorageRecordEnvelopeOptions type parameters
NameDescription
T
StorageRecordEnvelopeOptions properties
NameTypeDescription
codec?undefined

No codec: the envelope.

equals?from StorageRecordBaseOptions(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.

fallbackfrom StorageRecordBaseOptionsT

What the record reads while nothing valid is stored.

keyfrom StorageRecordBaseOptionsstring

The storage key.

legacy?from StorageRecordBaseOptionsreadonly StorageRecordLegacy<T>[]

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?from StorageRecordBaseOptionsRecordStorage | (() => 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.

versionnumber

The version written in the envelope. Raise it when the stored shape changes, and migrate the older ones.

watch?from StorageRecordBaseOptions((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?from StorageRecordBaseOptions'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.

ui/src/store/storage-record.ts:71

StorageRecordLegacyInterface#

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

TypeScript
interface StorageRecordLegacy<T>
StorageRecordLegacy type parameters
NameDescription
T
StorageRecordLegacy properties
NameTypeDescription
keystring

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.

ui/src/store/storage-record.ts:27

WritableInterface#

A Readable you can also write to.

TypeScript
interface Writable<T> extends Readable<T>
Writable type parameters
NameDescription
T
Writable properties
NameTypeDescription
getfrom Readable() => T

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.

subscribefrom Readable(onChange: () => void) => () => void

Registers a listener called after the value changes. Returns a function that removes the listener.

ui/src/store/store.ts:18

Types#

RecordStorageType#

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

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

ui/src/store/storage-record.ts:24

StorageRecordOptionsType#

Options of createStorageRecord: the envelope (version, parse) or a format of its own (codec).

TypeScript
type StorageRecordOptions<T> =
  | StorageRecordEnvelopeOptions<T>
  | StorageRecordCodecOptions<T>
StorageRecordOptions type parameters
NameDescription
T

ui/src/store/storage-record.ts:89