Surface / Drawing only what a Message can change
Keep a Surface's drawing until a Message writes what it reads
This page has a title and a counter. The blocks below are one file (install foldkit, effect, foldkit-surface and @doeixd/metakit). The first is an application as you would already have it: a Model, two Messages, and a Surface with a view function (a renderer) for each part. App.surface's model picks the fields the part reads. A Renderer<Model, Message> is a view function from the fields the Surface reads and an HTML builder h to HTML; its second type parameter is the Messages the part can send, and never means none.
import { Result, Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Surface, type Renderer } from 'foldkit-surface'
const Model = Schema.Struct({ title: Schema.String, count: Schema.Number })
type Model = typeof Model.Type
const Message = defineMessageUnion({ Renamed: { title: Schema.String }, Counted: {} })
type Message = typeof Message.Type
const App = Surface.application({ Model, Message })
const Header = App.surface('Header', { model: ({ model }) => ({ title: model.title }) })
const Tally = App.surface('Tally', { model: ({ model }) => ({ count: model.count }) })
const drawHeader: Renderer<{ readonly title: string }, never> = (model, h) =>
h.h1([], [model.title])
const drawTally: Renderer<{ readonly count: number }, never> = (model, h) =>
h.p([], [String(model.count)])Now declare what each Message writes. A Transition, from the @doeixd/metakit package, is a Message's change written as data: Transition.set(state.count, …) says "this sets count", and the path is checked against the Model's Schema (state is the Model's paths, Num builds number expressions). A Machine is a record of Transitions, one under each Message's name.
import { Num, Transition } from '@doeixd/metakit'
import * as Machine from '@doeixd/metakit/machine'
import { Writes } from 'foldkit-surface/writes'
const Renamed = Transition.make({
state: Model,
input: Message.Renamed,
apply: ({ state, input }) => [Transition.set(state.title, input.title)],
})
const Counted = Transition.make({
state: Model,
input: Message.Counted,
apply: ({ state }) => [Transition.set(state.count, Num.add(state.count, 1))],
})
const writes = Writes.of(App, Machine.make({ state: Model, transitions: { Renamed, Counted } }))
const header = writes.rootView(Header, undefined, drawHeader)
const tally = writes.rootView(Tally, undefined, drawTally)
const run = <Input>(transition: Transition.Transition<Model, Input>, model: Model, input: Input) =>
Result.getOrElse(Transition.run(transition, model, input), () => model)
const appUpdate = (model: Model, message: Message) => ({
model: Message.match(message, {
Renamed: renamed => run(Renamed, model, renamed),
Counted: counted => run(Counted, model, counted),
}),
})
const update = writes.track(appUpdate)
writes.regions().map(region => [region.name, region.redrawnBy])
// [['Header', ['Renamed']], ['Tally', ['Counted']]]What happened:
Writes.of(App, machine)reads each Transition's writes. A Transition under a name that is not one of the application's Messages does not compile; from plain JavaScript,Writes.ofthrows.writes.rootView(Header, undefined, drawHeader)makes the header's view;undefinedis for a Surface that takes no params. It takes whatSurface.rootViewtakes and gives the same view.Headerreadstitle, and of the two Transitions onlyRenamedsetstitle. That match is made here, once.- Your application's view calls
header(model, h)andtally(model, h)with the Model and the builderhFoldkit hands it, as it would withoutwrites, and you giveupdateto Foldkit as usual. writes.track(appUpdate)is theupdateyou give Foldkit. It notes which Message made each Model. After aCounted,headerhands Foldkit the drawing it made last time: the header's fields are not read from the Model,drawHeaderdoes not run, and Foldkit skips comparing the header with the page. What that costs instead is a counter raised for each view a Message reaches, and two numbers compared per view.writes.regions()lists the result, so you can see which Messages draw each Surface again (redrawnByis that list of Message names).
In the counting test, the five Surfaces are a header, a counter, an account panel, an owner line and one that names no path. Fifty Counted Messages draw them 105 times with writes (the counter and the pathless one 51 times each, counting the first drawing, and the other three once) and 255 times without (5 Surfaces in 51 Models). The header, drawn once, is the same DOM element with no change recorded on it (writes.browser.test.ts).