Skip to content

Storage Instances ​

Exported from usewagen/storage. An instance wraps an adapter, which holds the strings, and adds the prefix, the subscriptions and the error handling the states rely on.

createLocalStorage ​

ts
function createLocalStorage(config?: StorageConfig): StorageInstance

An instance over window.localStorage, with crossTab on.

createSessionStorage ​

ts
function createSessionStorage(config?: StorageConfig): StorageInstance

An instance over window.sessionStorage.

createMemoryStorage ​

ts
function createMemoryStorage(config?: StorageConfig): StorageInstance

An instance over a Map.

Where the browser refuses to hand over a web storage, the two web instances warn and fall back to memory. Where there is no window, as on the server, they fall back to a storage that holds nothing: reads give the parser default and writes are dropped.

createStorage ​

ts
function createStorage(name: string, options: StorageAdapter & StorageConfig): StorageInstance

Builds an instance over an adapter of your own. See A storage of your own.

StorageConfig ​

OptionTypeDefaultDescription
prefixstring''Added to every key before the adapter sees it.
crossTabbooleanfalseWhether watch is subscribed to.
onError(error: unknown) => voidnoneCalled with whatever the adapter throws.

StorageAdapter ​

MemberTypeDescription
getItem(key: string) => string | nullThe raw string, or null.
setItem(key: string, value: string) => voidWrites the raw string.
removeItem(key: string) => voidRemoves the entry.
keys() => string[]Every key the store holds, prefixes included.
watch(onChange: (key: string | null) => void) => UnsubscribeOptional. Reports changes made outside the adapter.

The keys an adapter is given carry the prefix, which is why keys returns them that way. watch has to return the function that stops listening, and a null key means everything changed at once.

StorageInstance ​

MemberTypeDescription
namestring'local', 'session', 'memory' or the name you gave it.
prefixstringThe prefix every key goes through.
getItem(key: string) => string | nullThe raw string under the prefix, without a parser.
setItem(key: string, value: string) => voidWrites a raw string and notifies the listeners for that key.
removeItem(key: string) => voidRemoves the entry and notifies the same listeners.
has(key: string) => booleanWhether the entry is there.
keys() => string[]The keys of this instance, prefix removed.
clear(options?: { except?: string[] }) => voidRemoves every key of this instance, keeping the ones in except.
subscribe(key: string, listener: () => void) => UnsubscribeRuns the listener when that key changes.
subscribe(listener: (key: string | null) => void) => UnsubscribeRuns the listener on every change, with the key that moved.
destroy() => voidStops the cross tab listener and drops every subscription.

Keys outside the prefix belong to something else on the origin, so keys does not list them and clear does not touch them. A write that changes nothing notifies nobody.

Released under the MIT License.