Mirror / foldkit-mirror
Plug the URL into makeApplication with Mirror.routing
Foldkit's makeApplication takes init(url), update and
routing: { onUrlRequest, onUrlChange }. Mirror.routing takes the
application's own three and gives back the same three, with the mirrors
reading every URL, so the result spreads straight into the runtime config.
The Message gains the link variant Foldkit's routing asks for:
import { Runtime } from 'foldkit'
import { UrlRequest } from 'foldkit/navigation'
const Message = defineMessageUnion({
ClickedLink: { request: UrlRequest },
UrlChanged: { url: Url },
})
const routed = Mirror.routing({
mirrors: [Filters],
urlChanged: 'UrlChanged',
init: (url: Url) => ({ model: initial }),
update: (model: Model, message: Message) => ({ model }),
routing: {
onUrlRequest: request => Message.ClickedLink({ request }),
onUrlChange: url => Message.UrlChanged({ url }),
},
})
Runtime.makeApplication({ Model, ...routed, view, subscriptions, container })What each piece does, and does not do:
initcalls the application'sinitand reads the starting URL into the Model it returns. The application's Commands are its own; they were made before the URL was read, so aninitwhose Commands depend on the slice callsFilters.reduceitself.updatereads aUrlChangedMessage's URL into the Model, then hands the Message to the application'supdate, which routes the path over slices the mirrors have already read. Every other Message goes straight through.routingis the application's own: its links, its URL Message. Nothing is intercepted or added.- Writing the URL is still
Filters.subscriptions, spread into the application's Subscriptions as above.
urlChanged names the tag the mirrors read; it must be the Message
onUrlChange makes, and that Message must carry a url, or the call does not
compile. A UrlChanged for the URL the Model already shows (the mirror's own
write, answered by Foldkit) returns the Model it was given, provided the
application's update also returns its Model for a route that did not change.
examples/foldkit/query-sync is the full application.