Mirror / foldkit-mirror

Restore before the first render with bootstrap

Normally, Prefs.restore reads a store asynchronously and dispatches MirrorRestored after the initial render. When stored keys are already available at boot—embedded as server Flags, read synchronously, or passed in a test—apply them before the first render with Mirror.bootstrap:

ts
import type { Encoded } from 'foldkit-mirror'

const init = (flags: { prefs: Encoded }, url: Url): Return => ({
  model: Mirror.bootstrap(
    initial,
    Prefs.bootstrap(flags.prefs),
    model => Filters.reduce(model, url),
  ),
})

What each piece does:

  • Prefs.bootstrap(keys) applies pre-read keys (Encoded, Record<string, string>) purely to fields still at their initial values, running before the first render with no Command.
  • model => Filters.reduce(model, url) applies the starting URL over that result.
  • Mirror.bootstrap(model, ...steps) runs the steps in order so precedence stays URL > store > initial, returning the Model unchanged when nothing differs.

A bootstrapped value also wins over a later restore for any field it set, so bootstrapping from Flags alongside running restore stays safe. Wiring.init runs startup Commands after the Model exists, whereas bootstrap populates the initial Model itself.