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
- **Natively** a store is one file,
<data directory>/<app>/<name>.json(std::fs'sdata_directory), one object of keys to values, a member a line.flushwrites it whole to<name>.json.tmp, syncs it to the disk and renames it over the old file, so a crash leaves the old file or the new, never half of one. A file that does not parse is moved aside to<name>.json.badand the store starts empty. - **On the web** each key is a
localStorageitem,<app>/<name>/<key>, holding the value's JSON text:setwrites through at once, and opening reads every item with the prefix, in the keys' byte order. A write the browser refuses (its quota, about 5 MB an origin, or private browsing) keeps the value in memory and makessavedfalse. The first store opened asks the browser to keep the site's data (navigator.storage.persist()), which Safari otherwise deletes after seven days unvisited. - **Nowhere to keep it:** a store whose directory is not known (WASI
without the variables or the directory preopened), whose file cannot
be read, or of the app "", is kept in memory, and
savedsays so.
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.
StoreStore::opengetsetremovekeysflushsavedpathdisposeKeptStoreKeptStore::opengetsetremovekeyssavedpathflushStoreWriteunsavedchangeswritewrote
type Storesource
exp type Store : Disposable = struct
A store of the app, written back when flushed or disposed.
Functions
exp fn Store::open(app: &[u8], name: &[u8]) -> Storeexp fn get(self store: &Store, key: &[u8]) -> Option<&Json>exp fn set(self store: &var Store, key: &[u8], value: Json)exp fn remove(self store: &var Store, key: &[u8])exp fn keys(self store: &Store) -> Vector<String>exp fn flush(self store: &var Store) -> boolexp fn saved(self store: &Store) -> boolexp fn path(self store: &Store) -> Option<&[u8]>exp fn dispose(self store: &var Store)
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
exp fn KeptStore::open(app: &[u8], name: &[u8]) -> KeptStoreexp fn get(self store: &KeptStore, key: &[u8]) -> Option<&Json>exp fn set(self store: &var KeptStore, key: &[u8], value: Json)exp fn remove(self store: &var KeptStore, key: &[u8])exp fn keys(self store: &KeptStore) -> Vector<String>exp fn saved(self store: &KeptStore) -> boolexp fn path(self store: &KeptStore) -> Option<&[u8]>exp fn flush(self store: &var KeptStore) -> boolexp fn unsaved(self store: &KeptStore) -> Option<StoreWrite>exp fn wrote(self store: &var KeptStore, w: &StoreWrite, ok: bool)
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.