Learn / Quickstart
Declare what one feature reads and sends
A plain selector like m => m.todos works for rendering, but a server fetcher, offline sync, or AI agent cannot inspect an arbitrary function to learn which fields it reads or which events it triggers. To make those dependencies explicit, register your Model and Message schemas with Surface.application, then call App.surface(...) to declare one feature boundary.
In an ESM project (Node 22+ with "type": "module" in package.json), save this as quickstart.ts and run npx tsx quickstart.ts:
ts
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { modifyFields } from 'foldkit/struct'
import { Surface } from 'foldkit-plus'
const Todo = Schema.Struct({ id: Schema.String, title: Schema.String, done: Schema.Boolean })
const Model = Schema.Struct({ todos: Schema.Array(Todo), filter: Schema.String })
const Message = defineMessageUnion({
Added: { id: Schema.String, title: Schema.String },
Filtered: { filter: Schema.String },
})
const update = (model: typeof Model.Type, message: typeof Message.Type) =>
Message.match(message, {
Added: ({ id, title }) => ({
model: modifyFields(model, { todos: list => [...list, { id, title, done: false }] }),
}),
Filtered: ({ filter }) => ({ model: modifyFields(model, { filter: () => filter }) }),
})
const App = Surface.application({ Model, Message })
const TodoList = App.surface('TodoList', {
model: ({ model }) => ({ todos: model.todos }),
messages: [Message.Added],
})
const next = update({ todos: [], filter: 'all' }, Message.Added({ id: '1', title: 'Try Foldkit' }))
console.log(Surface.read(TodoList, next.model))
console.log(TodoList.projection().dependencies)Running npx tsx quickstart.ts prints:
{ todos: [ { id: '1', title: 'Try Foldkit', done: false } ] }
[ [ 'todos' ] ]What happened:
Schema.Structdefines the data shapes and their TypeScript types via.Type.defineMessageUnioncreates tagged event constructors (Message.Added(...)) and an exhaustiveMessage.matchhelper.updatereturns{ model }with the updatedtodosarray (viamodifyFields), leavingfilteras'all'.Surface.read(TodoList, next.model)extracts{ todos: [...] }withoutfilter, becauseTodoListonly readsmodel.todos.TodoList.projection().dependenciesreturns[['todos']]— the list ofModelfield paths read bymodel.todos— without mounting a UI. Server fetching, offline sync, and agent adapters inspect that list to load only what the feature uses, whilemessages: [Message.Added]restricts which events this feature can send.