Mirror / foldkit-mirror

Plug the URL into makeApplication with Mirror.routing

Mirror.routing wraps your init, update, and routing callbacks so the mirrors read the starting URL on boot and every UrlChanged on navigation, then returns the config to spread into Runtime.makeApplication:

ts
import { Runtime } from 'foldkit'
import type { HtmlBuilder } from 'foldkit/html'
import { UrlRequest } from 'foldkit/navigation'
import * as Subscription from 'foldkit/subscription'

type Model = typeof Model.Type
const RoutedMessage = defineMessageUnion({
  ClickedLink: { request: UrlRequest },
  UrlChanged: { url: Url },
})
type RoutedMessage = typeof RoutedMessage.Type

const routed = Mirror.routing({
  mirrors: [Filters],
  urlChanged: 'UrlChanged',
  init: (_url: Url) => ({ model: initial }),
  update: (model: Model, _message: RoutedMessage) => ({ model }),
  routing: {
    onUrlRequest: request => RoutedMessage.ClickedLink({ request }),
    onUrlChange: url => RoutedMessage.UrlChanged({ url }),
  },
})

const view = (model: Model, h: HtmlBuilder<RoutedMessage>) => ({
  title: 'Todos',
  body: h.p([], [model.filter]),
})
const subscriptions = Subscription.make<Model, RoutedMessage>()(() => ({
  ...Filters.subscriptions,
}))

Runtime.makeApplication({ Model, ...routed, view, subscriptions, container: document.body })

What each piece does:

  • init runs your init first and reduces the starting URL into the Model it returns. If your startup Commands depend on the mirrored slice, call Filters.reduce(initial, url) before building those Commands.
  • update reduces any UrlChanged message through Filters.urlChanged before passing the message to your update, so your router sees the updated slice.
  • routing keeps your own onUrlRequest and onUrlChange handlers as written.
  • Filters.subscriptions writes subsequent Model changes out to the browser URL.

urlChanged names the tag returned by onUrlChange, and that variant must carry a url field. examples/foldkit/query-sync shows this full application running.