zidane-ui/ store
import { … } from 'zidane-ui/store'ui/src/store/index.ts
Functions#
constantFunction#
createKeyedStoreFunction#
Creates a KeyedStore.
function createKeyedStore<V, Fallback = null>(
fallback?: Fallback,
): KeyedStore<V, Fallback>Type parameters
| Name | Default | Description |
|---|---|---|
| V | ||
| Fallback | null |
Parameters
| Name | Type | Description |
|---|---|---|
| fallback? | Fallback | What a key's |
Returns
KeyedStore<V, Fallback>createStorageRecordFunction#
Creates a StorageRecord. It reads the storage on first use.
function createStorageRecord<T>(options: StorageRecordOptions<T>): StorageRecord<T>Type parameters
| Name | Description |
|---|---|
| T |
Parameters
| Name | Type | Description |
|---|---|---|
| options | StorageRecordOptions<T> | The key, the version, the fallback, how to validate and migrate, and where it lives. |
Returns
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.
function pageStorage(area?: 'localStorage' | 'sessionStorage'): Storage | nullParameters
| Name | Type | Default | Description |
|---|---|---|---|
| area? | 'localStorage' | 'sessionStorage' | 'localStorage' | Which of the page's storages. Default |
Returns
Storage | nullwritableFunction#
Interfaces#
KeyedStoreInterface#
Values by key, each key with its own subscribers.
interface KeyedStore<V, Fallback = null>Type parameters
| Name | Default | Description |
|---|---|---|
| V | ||
| Fallback | null |
Properties
| Name | Type | Description |
|---|---|---|
| clear | () => void | Forgets every key. |
| delete | (key: string) => void | Forgets |
| get | (key: string) => V | undefined | The value under |
| 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 |
| subscribe | (key: string, listener: () => void) => () => void | Calls |
ReadableInterface#
The shape every host exposes its data in: read the current value and subscribe to changes.
interface Readable<T>Type parameters
| Name | Description |
|---|---|
| T |
Properties
| Name | Type | Description |
|---|---|---|
| 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. |
StorageRecordInterface#
A value kept in a Storage; see createStorageRecord.
interface StorageRecord<T> extends Writable<T>Type parameters
| Name | Description |
|---|---|
| T |
Properties
| Name | Type | Description |
|---|---|---|
| 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 |
subscribefrom Writable | (onChange: () => void) => () => void | Registers a listener called after the value changes. Returns a function that removes the listener. |
StorageRecordBaseOptionsInterface#
The options every createStorageRecord takes, whatever its format.
interface StorageRecordBaseOptions<T>Type parameters
| Name | Description |
|---|---|
| T |
Properties
| Name | Type | Description |
|---|---|---|
| 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. |
| watch? | ((key: string, onChange: () => void) => () => void) | null | Calls |
| writeError? | 'keep' | 'throw' | What a write the storage refuses (full, blocked) does. |
StorageRecordCodecInterface#
A record's own text format, in place of the envelope: the record stores what encode gives and reads it back with decode.
interface StorageRecordCodec<T>Type parameters
| Name | Description |
|---|---|
| T |
Properties
| Name | Type | Description |
|---|---|---|
| decode | (raw: string) => T | undefined | The value in the stored text; |
| encode | (value: T) => string | The text stored for |
StorageRecordCodecOptionsInterface#
A record stored in its own text format; the format carries its own version, if any.
interface StorageRecordCodecOptions<T> extends StorageRecordBaseOptions<T>Type parameters
| Name | Description |
|---|---|
| T |
Properties
| Name | Type | Description |
|---|---|---|
| codec | StorageRecordCodec<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 StorageRecordBaseOptions | T | What the record reads while nothing valid is stored. |
keyfrom StorageRecordBaseOptions | string | The storage key. |
legacy?from StorageRecordBaseOptions | readonly StorageRecordLegacy<T>[] | Older keys or formats, tried in order the first time nothing valid is stored. |
storage?from StorageRecordBaseOptions | RecordStorage | (() => RecordStorage | null | undefined) | null | Where the value lives: a storage, or a function read on each access. |
watch?from StorageRecordBaseOptions | ((key: string, onChange: () => void) => () => void) | null | Calls |
writeError?from StorageRecordBaseOptions | 'keep' | 'throw' | What a write the storage refuses (full, blocked) does. |
StorageRecordEnvelopeOptionsInterface#
A record stored in the versioned envelope { "v": version, "data": … }.
interface StorageRecordEnvelopeOptions<T> extends StorageRecordBaseOptions<T>Type parameters
| Name | Description |
|---|---|
| T |
Properties
| Name | Type | Description |
|---|---|---|
| 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 StorageRecordBaseOptions | T | What the record reads while nothing valid is stored. |
keyfrom StorageRecordBaseOptions | string | The storage key. |
legacy?from StorageRecordBaseOptions | readonly 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; |
| parse | (data: unknown) => T | undefined | Validates the stored data of the current version; |
storage?from StorageRecordBaseOptions | RecordStorage | (() => RecordStorage | null | undefined) | null | Where the value lives: a storage, or a function read on each access. |
| version | number | The version written in the envelope. Raise it when the stored shape changes, and |
watch?from StorageRecordBaseOptions | ((key: string, onChange: () => void) => () => void) | null | Calls |
writeError?from StorageRecordBaseOptions | 'keep' | 'throw' | What a write the storage refuses (full, blocked) does. |
StorageRecordLegacyInterface#
Where a record read its value before it lived under its key in the envelope.
interface StorageRecordLegacy<T>Type parameters
| Name | Description |
|---|---|
| T |
Properties
| Name | Type | Description |
|---|---|---|
| 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; |
WritableInterface#
A Readable you can also write to.
interface Writable<T> extends Readable<T>Type parameters
| Name | Description |
|---|---|
| T |
Properties
| Name | Type | Description |
|---|---|---|
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 |
subscribefrom Readable | (onChange: () => void) => () => void | Registers a listener called after the value changes. Returns a function that removes the listener. |
Types#
RecordStorageType#
The Storage calls a record makes (localStorage, sessionStorage or a double).
type RecordStorage = Pick<Storage, 'getItem' | 'setItem'> & Partial<Pick<Storage, 'removeItem'>>StorageRecordOptionsType#
Options of createStorageRecord: the envelope (version, parse) or a format of its own (codec).
type StorageRecordOptions<T> =
| StorageRecordEnvelopeOptions<T>
| StorageRecordCodecOptions<T>Type parameters
| Name | Description |
|---|---|
| T |