The ecosystem · local
Data that stays on the device
Local.signal!("rack.games", []) is a signal that keeps itself: a reload, a closed tab or a closed app finds it where it was. In a browser, and in an app on Android and iOS, with no server.
Use it for what belongs to the person and the device: settings, a draft, the state of a game. Rack, a score keeper with no server, keeps its players, the game in progress and the games played with it. What every visitor shares belongs on the server.
Local is a module both halves of an app compile. LocalStore is a plugin that serves a small script every page loads, which keeps the values in the browser. An app on a phone needs no script: the phone keeps them.
Installing it#
Name the package in the app's header, and add the plugin to the app's plugins:
app [program] { …, local: "../../packages/local/main.roc", pf: platform "../../server/platform/main.roc" }import local.LocalStore
…
program = App.program({
…
plugins: [LocalStore.plugin({})],
})Then import local.Local in the components that keep something.
Signals that keep themselves#
## A signal kept on the device under `key`, as `create_signal!`'s: `(value!, set!)`. It holds
## `default` until what the device keeps under `key` is read (`Local.loaded!`), and keeps holding
## it when the device keeps nothing there, or something that isn't this type's JSON (an older
## shape: it is replaced at the next `set!`). Each `set!` keeps the value at once.
signal! : Str, a => ((() => a), (a => {})) where [a.is_eq : a, a -> Bool, a.Json.Encodable([]), a.Json.Parseable([InvalidJson(Str)])]
signal! = |key, default| {
(value!, set_value!) = create_signal!(default)
on_settled!(
|| match Local.read!(key) {
Ok(kept) => set_value!(kept)
Err(_) => {}
},
)
set! = |next| {
set_value!(next)
_ = Local.write!(key, next)
{}
}
(value!, set!)
}
## Whether the page's kept signals hold what the device keeps: false while the server renders and
## a browser hydrates, true once the page is on the screen. A page shows a placeholder until then,
## not its defaults, so nothing flashes by on the way. Ask for it after the page's
## `Local.signal!`s, whose reads it follows.
##
## ready! = Local.loaded!({})
## {if ready!() <Scores /> else <p class="placeholder"></p>}
loaded! : {} => (() => Bool)
loaded! = |{}| {
(ready!, set_ready!) = create_signal!(Bool.False)
on_settled!(|| set_ready!(Bool.True))
ready!
}(games!, set_games!) = Local.signal!("rack.games", [])works ascreate_signal!does, and eachset_games!keeps the value at once, as its JSON, under the key. Give the keys a prefix of the app's: a browser shares its storage with every page of the site.- The value's type is what is kept. Something kept that isn't that type's JSON any more (an older shape) is passed over: the signal holds its default, and the next
set!replaces it. Local.read!andLocal.write!read and keep one value now, without a signal.
It is Solid Primitives' makePersisted the Roc way: a key and a default instead of a signal and options, and the device's own storage.
When it reads#
A page's HTML is rendered where nothing is kept: by the server, or once by rocx build --static. So the browser's first render has the defaults too, and matches it as the page hydrates. What the device keeps is read right after, in an on_settled!. On a phone, which renders nothing in advance, that is at once.
Local.loaded! says when the kept values are in. A page shows a placeholder until then, not its defaults, so a game in progress doesn't flash a new-game form on the way. Rack's game page:
import local.Local
…
view! = |_| {
…
(known!, set_known!) = Local.signal!("rack.players", Rack.nobody)
(table!, set_table!) = Local.signal!("rack.table", Rack.idle)
(games!, set_games!) = Local.signal!("rack.games", Rack.no_games)
ready! = Local.loaded!({})
…
screen! = || if (ready!()) Game.screen(table!()) else Reading
<main class="game">
…
{match screen!() {
Reading => <p class="reading" aria-busy="true"></p>In a browser#
Local asks the page with Browser.ask!, a call that answers at once (D-159). The plugin's script, served at /rocx/plugins/local/local.js and loaded before start.js, answers its two questions from localStorage:
// The local plugin's script (packages/local): every page loads it before start.js, so what it
// answers is there before the page wakes. `Local` asks it at once (`Browser.ask!`, D-159):
//
// - `local.get` `{ key }`: the text kept under `key` in `localStorage`, or "" when there is none.
// - `local.set` `{ key, value }`: `value` (a text: the value's JSON) kept under `key`; whether it
// was. A browser that keeps nothing for the page (storage turned off, or full) answers false, and
// the value lives as long as the page does.
function get({ key }) {
try {
return localStorage.getItem(key) ?? "";
} catch {
return "";
}
}
function set({ key, value }) {
try {
localStorage.setItem(key, value);
return true;
} catch {
return false;
}
}
window.rocxAsks = { ...window.rocxAsks, "local.get": get, "local.set": set };A browser that keeps nothing for the page (storage turned off, or full) answers that it didn't keep a value. The signal still changes, and the value lives as long as the page. Two tabs of one app don't see each other's changes until a reload.
On a phone#
On Android and iOS, Browser.ask! goes to the phone's shell, which answers the same two names with no script. Android keeps the values in the app's SharedPreferences, iOS in its NSUserDefaults, under the same keys. They outlive the app being closed, and go when it is uninstalled.