Surface / foldkit-surface

Surfaces

App.surface(name, { params?, model, messages? }) pairs a projection (returning a Projection or a plain object of Projections and ModelRefs) with the Messages a feature may send:

ts
const TodoDetail = App.surface('TodoDetail', {
  model: ({ model }) => ({ todos: model.todos, selectedTodoId: model.selectedTodoId }),
  messages: [Message.ToggledTodo],
})

// A parameterized Surface may read `params`; dynamic lookups are OptionalRefs.
const ById = App.surface('ById', {
  params: { id: Schema.String },
  model: ({ model, params }) => ({ todo: ModelRef.focus(model.todosById).at(params.id) }),
})

Surface.read(TodoDetail, root)                     // project purely
Surface.read(ById, root, { id: 't1' })
const render = Surface.view(TodoDetail, (todos, h) => h.div([], [`${todos.todos.length} todos`]))
Surface.rootView(TodoDetail, undefined, render)    // bound to the app root

The Surface's Model type is the projected record ({ todos, selectedTodoId }), and its Message type is the union of listed constructors (constructors from other applications fail to compile). Parameterized Surfaces require params whenever read.

  • Surface.at(surface, params) tracks an active Surface instance where params is either static or (model) => Option<Params>, exposing projectionOf(model), owner, and messages for consumers such as foldkit-remote's Data.subscriptions.
  • Surface.each(child, { from, instances }) expands one Surface definition into zero-to-many keyed instances while its parent from Surface is active, throwing on duplicate keys or __proto__. Both at and each share the SurfaceSource interface read via Surface.instances(source, model).

Draw a Surface only when a Message writes what it reads

Foldkit runs every view after every Message. writes.rootView(surface, params, render) is Surface.rootView that keeps the Surface's last drawing until a Message is declared to write a field the Surface reads: render does not run and Foldkit does not compare the drawing with the page. writes comes from Writes.of in foldkit-surface/writes, which takes one declared transition per Message, and WritesCheck.verify checks those declarations against what update changes while you develop. Draw only the parts of a page a Message can change shows both.

Surface.make, the explicit form

Surface.make(app, name, { Params?, model, messages? }) is the underlying function App.surface calls, taking a Params schema and a model callback returning a Projection.