Surface / Drawing only what a Message can change

The header shows an old title

A part of the page that shows an old value kept a drawing it should not have. The usual cause is a wrong declaration: a Message changed a field its Transition does not list. (The other cause is a renderer that reads something besides its Model; see the rules above.) Nothing reports a wrong declaration in production, so in development check every Message:

ts
import { WritesCheck } from 'foldkit-surface/writes'

const checkedUpdate = writes.track(
  import.meta.env?.DEV === true
    ? WritesCheck.verify(writes, appUpdate, { surfaces: [Header, Tally] })
    : appUpdate,
)

WritesCheck.verify compares the Model before and after each Message with the writes its Transition declares. For a Counted that also set the title it throws:

[foldkit-surface] "Counted" changed title, and its transition declares it writes count. A view kept on the declared writes would show its last drawing: declare the write, or stop making it.

A struct rebuilt with the fields it had is not a change, and a change is named by the deepest path that differs (account.owner, not account). Messages with no Transition have nothing to be compared with.

surfaces adds a second check. A Surface declared closed: true promises that only the Messages in its messages list change what it shows; verify reports any other Message that does.

Options:

  • report decides what happens to a finding. Left out, it is WritesCheck.fail, which throws, so a test that sends the Message fails. Pass WritesCheck.warn to log a warning and carry on, which suits a running development server. Pass your own function to send findings elsewhere.
  • surfaces is the list of Surfaces to hold to their closed promise. Left out, only the declared writes are checked.

The check walks the Model on every Message, so keep it behind import.meta.env?.DEV === true as above. A production build then leaves the check's code out of the page.

Next