The fold

fold(events) turns a slice of the log into the current state of every task it mentions. It is incremental — applyEvent takes one event at a time — so a client that holds a cursor replays only what it hasn't seen.

Task numbers are dense over created events in log order, so #38 is the 38th task the log ever saw and never changes. Numbering is derived, not stored: that is what lets a task created offline receive its number when the remote log commits it.

import { fold, MemoryLog, VirtaStore, counterIds, stepClock } from 'tosijs-virta'

const store = new VirtaStore({ log: new MemoryLog(), identity: 'demo', ids: counterIds(), clock: stepClock() })
await store.load()
const a = await store.create({ title: 'first', tags: ['bug'] })
await store.create({ title: 'second' })
await store.tag(a.id, 'status:doing')
await store.comment(a.id, 'on it')

preview.textContent = store.tasks.map((t) => `#${t.num} ${t.title} [${t.tags.join(' ')}] comments=${t.comments}`).join('\n')
test('two tasks, numbered in creation order, with the fold applied', () => {
  expect(preview.textContent).toBe('#1 first [bug status:doing] comments=1\n#2 second [] comments=0')
})