std

module std::storagesource

Stores that persist: small sets of JSON values by key that a program keeps across its runs, such as its settings, a basket or recent searches. A store is read whole when opened and kept in memory, so reading never waits; it is meant for up to about a megabyte, and larger data goes into files of its own.

import std::json;
import std::option;

var store = Store::open(&"alloy-example", &"settings");
store.set(&"theme", Json::text(&"dark"));
expect(store.keys().length() == 1);
if (store.get(&"theme") is ::Some |&theme|) {
    expect_equal(&theme.to_json().view(), &"\"dark\"");
}
expect(store.flush());
store.dispose(); // flushes too

Stores are not for secrets: the file is one the user's other programs can read, and on the web any script of the origin can. Two processes of one app writing one store: the last flush wins; other tabs' changes on the web are not followed.

type Storesource

exp type Store : Disposable = struct

A store of the app, written back when flushed or disposed.

Functions

fn Store::opensource

exp fn Store::open(app: &[u8], name: &[u8]) -> Store

The app's store of the name: what was saved, or nothing. It never fails: one it cannot read or write is kept in memory, and saved says so.

fn getsource

exp fn get(self store: &Store, key: &[u8]) -> Option<&Json>

The value saved under the key.

fn setsource

exp fn set(self store: &var Store, key: &[u8], value: Json)

Sets the key's value, written at the next flush (natively) or at once (on the web).

fn removesource

exp fn remove(self store: &var Store, key: &[u8])

fn keyssource

exp fn keys(self store: &Store) -> Vector<String>

The keys in the order they were first set.

fn flushsource

exp fn flush(self store: &var Store) -> bool

Writes the store through now: false when it could not be.

fn savedsource

exp fn saved(self store: &Store) -> bool

Whether everything set has reached the disk or the browser.

fn pathsource

exp fn path(self store: &Store) -> Option<&[u8]>

The store's file, natively; None on the web and in memory.

fn disposesource

exp fn dispose(self store: &var Store)

Flushes the store.

type KeptStoresource

exp type KeptStore = struct

A store a holder keeps open for the whole program and writes back itself, as pkg::alloy::ui's Ui does for ui.store(): the same store as a Store without being disposable, for a holder that cannot be disposed. It may be written off the holder's thread: unsaved gives what to write, write writes it anywhere, and wrote says how it went.

Functions

fn KeptStore::opensource

exp fn KeptStore::open(app: &[u8], name: &[u8]) -> KeptStore

As Store::open.

fn getsource

exp fn get(self store: &KeptStore, key: &[u8]) -> Option<&Json>

fn setsource

exp fn set(self store: &var KeptStore, key: &[u8], value: Json)

fn removesource

exp fn remove(self store: &var KeptStore, key: &[u8])

fn keyssource

exp fn keys(self store: &KeptStore) -> Vector<String>

fn savedsource

exp fn saved(self store: &KeptStore) -> bool

fn pathsource

exp fn path(self store: &KeptStore) -> Option<&[u8]>

fn flushsource

exp fn flush(self store: &var KeptStore) -> bool

Writes the store through now, on the web the items the browser refused asked again: false when it could not be.

type StoreWritesource

exp type StoreWrite = struct

What a store's file is to be written with.

Functions

fn unsavedsource

exp fn unsaved(self store: &KeptStore) -> Option<StoreWrite>

What to write for the store's file to hold everything set, or None when it holds it already or the store has no file.

fn changessource

exp fn changes(self w: &StoreWrite) -> u64

How many sets and removes of the store the write holds.

fn writesource

exp fn write(self w: &StoreWrite) -> bool

Writes the file whole, atomically: to the path with .tmp after it, synced, then renamed over the old one, the directories made first.

fn wrotesource

exp fn wrote(self store: &var KeptStore, w: &StoreWrite, ok: bool)

Takes how a write unsaved gave went: what it held is saved when it was written.