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:
initruns yourinitfirst and reduces the starting URL into theModelit returns. If your startup Commands depend on the mirrored slice, callFilters.reduce(initial, url)before building those Commands.updatereduces anyUrlChangedmessage throughFilters.urlChangedbefore passing the message to yourupdate, so your router sees the updated slice.routingkeeps your ownonUrlRequestandonUrlChangehandlers as written.Filters.subscriptionswrites subsequentModelchanges 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.