Live lists

A list of notes can update straight away when a note is added, edited or deleted, on any device. This example continues from Private notes, in the same REPL.

Run it

Listen to the notes, printing their titles newest first each time they change:

let latest = 0
const show = async notes => {
  const run = ++latest
  const newest = Object.values(notes ?? {})
    .filter(note => note?.title && note.created)
    .sort((a, b) => b.created - a.created)
  const titles = await Promise.all(
    newest.map(note => holster.SEA.decrypt(note.title, user.is)),
  )
  if (run === latest) console.log(titles)
}
user.get("notes").on(show, true, {nested: true})
// ["Groceries"]

Add a note, and the list prints again:

const second = crypto.randomUUID()
await user
  .get("notes")
  .next(second)
  .put({
    title: await holster.SEA.encrypt("Ideas", user.is),
    body: await holster.SEA.encrypt("Learn the cello", user.is),
    created: Date.now(),
  })
// ["Ideas", "Groceries"]

Delete it, and it's gone from the next list:

await user.get("notes").next(second).put(null)
// ["Groceries"]

To stop listening, pass the same callback to off():

user.get("notes").off(show)

How it works