Data
const text = tinylib.data.get(`entry/${day}`) ?? ''
tinylib.data.set(`entry/${day}`, 'Rain all day')
tinylib.data.remove(`entry/${day}`)
const days = tinylib.data.keys('entry/').reverse() // newest first
tinylib.data holds the persona's values in this app. They're on the device before the page's first line runs, and every call is synchronous. set saves on the device at once and syncs to the person's other devices once there's a connection, so the app works offline with no code of its own. A value is anything JSON holds; values that change together go in one value. Calls are on the reference page.
Changes from elsewhere
tinylib.data.onChange((keys, error) => {
if (error) toast(error.message)
else if (keys.includes(open) && document.activeElement !== editor) editor.value = tinylib.data.get(open) ?? ''
})
A change from another device or from the rules calls onChange with its keys. When a save this page made fails on the device, onChange gets the error too, and those keys go back to what the store holds. When two devices change one key, the last write wins.
When it's full
try {
tinylib.data.set(open, editor.value)
} catch (error) {
toast(error.message)
}
set throws a TinylibError with limit when a value would take the persona's data past its cap, and a TypeError for a value JSON can't hold. Writes two devices made offline are both kept, even past the cap; after that, writes that add data throw until it's back under. The caps are under Data.
Values for friends
tinylib.data.set('score', { day, seconds, streak }, { audience: 'friends' })
A value saved for friends is readable by the persona's friends who have the app. Friends reads them.
Keys only the rules write
{ "rulesKeys": ["wins", "games/"] }
// rules.js
ctx.data(winner).set('wins', (ctx.data(winner).get('wins') ?? 0) + 1, { audience: 'friends' })
// the page
const wins = tinylib.data.get('wins') ?? 0
A key tinylib.json declares in rulesKeys, or a key under a declared prefix ending in /, is written only by the rules, with ctx.data. The page reads it like any other, and set and remove throw a TypeError for it. Declared keys are permanent: a publish can't drop one.