Bundle / foldkit-bundle
bundle
In Foldkit, adding a reusable child state machine—a counter, a date picker, or a media query—means wiring it by hand in five places inside the parent: init (to seed its initial state and Commands), update (to unwrap GotChildMessage and fold the child's transition), subscriptions (to lift its streams), managedResources (to lift its resources), and view (to mount h.submodel). Placing the same child twice repeats all five by hand.
foldkit-bundle packages a child's Model, Message, init, update, subscriptions, resources, view, and helpers into one Bundle, and pairs it with a Link naming where its slice lives in the parent Model and Message. Placing the bundle compiles each part to Foldkit's standard lifts—keeping the parent Model as the single owner, so replay, DevTools, and Scene tests work the same as with hand-wired Submodels.
See reusable widgets placed and wired in the live widgets demo, or compare both styles side by side in Bundle versus Submodel.
Choose between a Bundle and a hand-wired Submodel
| Situation | Use |
|---|---|
| A child machine placed two or more times, or published for others | a Bundle |
| A child with Subscriptions or resources you keep forgetting to lift | a Bundle, then placements.complete |
A one-off child used in one place, such as a @foldkit/ui component | a hand-wired Submodel with a Link |
| Something without its own Model: a one-shot effect, a DOM attachment | a Command or a Mount, not a Bundle |
The parent Model owns each placement's state; a Bundle describes the transitions of that slice, and the Link names which slice. Placing a bundle twice creates two independent slices with separate Subscriptions. To check ownership alongside Sync and Remote, see foldkit-bundle-surface.
Install
pnpm add foldkit-bundle effect foldkiteffect and foldkit are peer dependencies.
A first Bundle: one counter
This defines a reusable Count bundle, places it under clicks in a Page parent, and builds the parent's runtime config:
import { Schema } from 'effect'
import type { HtmlBuilder } from 'foldkit/html'
import { defineMessageUnion } from 'foldkit/message'
import { Bundle } from 'foldkit-bundle'
const CountModel = Schema.Struct({ count: Schema.Number })
const CountMessage = defineMessageUnion({ Incremented: {} })
const Count = Bundle.make('Count', {
Model: CountModel,
Message: CountMessage,
init: () => ({ model: { count: 0 } }),
update: model => ({ model: { count: model.count + 1 } }),
})
const Page = Bundle.compose({ greeting: Schema.String }).pipe(Bundle.withChild('clicks', Count))
type Model = typeof Page.Model.Type
type Message = typeof Page.Message.Type
const { placements } = Page
const config = placements.complete({
init: () => placements.initial({ greeting: 'Hello' }),
update: placements.update(model => ({ model })),
subscriptions: placements.subscriptions(),
view: (model: Model, h: HtmlBuilder<Message>) =>
h.button(
[h.OnClick(Page.Message.GotClicksMessage({ message: CountMessage.Incremented() }))],
[`${model.greeting}: ${model.clicks.count}`],
),
})What happened:
Page.Modelbecomes{ greeting: string, clicks: { count: number } }, andPage.MessagegainsGotClicksMessage.- Hand
configtoRuntime.makeApplicationorRuntime.makeElement: initial render draws<button>Hello: 0</button>. - Clicking the button sends
Page.Message.GotClicksMessage({ message: CountMessage.Incremented() }), whichplacements.updateroutes toCount.updateand writes back tomodel.clicks, updating the button text toHello: 1.
See a complete running assembly in examples/bundle.