Bundle / foldkit-bundle

Args, OutMessages, and helpers

update receives the args as its third parameter, so behaviour can depend on configuration without storing it in the Model. This counter emits an OutMessage when it reaches its limit. CounterModel, CounterMessage, and the LimitReached Schema are ordinary Schemas:

ts
const Counter = Bundle.make('Counter', {
  Model: CounterModel,
  Message: CounterMessage,
  args: Schema.Struct({ limit: Schema.Number }),
  init: () => ({ model: { count: 0 } }),
  update: (model, _message, { limit }) => {
    const count = model.count + 1
    return count === limit
      ? { model: { count }, outMessage: LimitReached.make({ count }) }
      : { model: { count } }
  },
  helpers: {
    reset: (model: CounterModel, to: number) => ({ model: { ...model, count: to } }),
  },
})

A bundle with an OutMessage must be placed with onOut, which folds it into the parent as a Step. The Step sees the parent with the child already written back, and its model is typed from the scope:

ts
const Clicks = Bundle.declare(Counter, 'clicks')
const CounterPage = Bundle.parent({
  Model: Schema.Struct({ ...Clicks.fields, reached: Schema.Number }),
  Message: defineMessageUnion({ ...Clicks.cases }),
})

const ClicksPlaced = CounterPage.at(Clicks, {
  args: { limit: 10 },
  onOut: outMessage => model => ({ model: { ...model, reached: outMessage.count } }),
})

ClicksPlaced.helpers.reset(0) // an Update.Step of the parent

A helper that needs the args, as update does, is given in a function of them. Each placement calls that function with the args it holds, so two placements of one bundle fill to their own limits:

ts
const Filling = Bundle.make('Filling', {
  Model: CounterModel,
  Message: CounterMessage,
  args: Schema.Struct({ limit: Schema.Number }),
  init: () => ({ model: { count: 0 } }),
  update: model => ({ model }),
  helpers: ({ limit }) => ({
    fill: (model: CounterModel) => ({ model: { ...model, count: limit } }),
  }),
})

// A parent folding the child itself, with no placement, reads either form:
Bundle.helpersOf(Filling, { limit: 3 }).fill({ count: 0 }) // { model: { count: 3 } }

A placement reads them through the args it retains, so its derived args must be resolved first, as for update. A preset (with) binds them.

A parent's decision is its own Message and a helper

A parent's view sends the parent's own Message, saying what happened in its terms; its update applies the decision through the child's helper. It does not build the child's Message (wrapper.make(Child.Message.X(...))) in the view: the fact is the parent's, and a helper such as a tree's toggle reads what it needs from the child's own state, which the view would have to guess from a drawing. @foldkit/oxlint-plugin's no-child-message-construction-in-root reports the other shape.

ts
const ResetMessage = defineMessageUnion({ ...Clicks.cases, ClickedReset: {} })
const ResetPage = Bundle.parent({
  Model: Schema.Struct({ ...Clicks.fields }),
  Message: ResetMessage,
})
const ResetClicks = ResetPage.at(Clicks, { args: { limit: 10 }, onOut: Bundle.ignore })
const resetting = ResetPage.assemble(ResetClicks)

// The view sends ResetMessage.ClickedReset(); its only own Message lands here.
const updateResetting = resetting.update((model, _clickedReset) => ResetClicks.helpers.reset(0)(model))

The child's own Messages still reach it through the wrapper, from the view the child draws and the Behaviors it attaches; a test or a demo that stands in for a person may send them too. follow, below, is the exception: the Messages it replays are the owner's answer to its own ask, built at the one boundary that knows both.

  • args is required when the bundle takes them, and checked against the args Schema when placed (or when a preset is made with with); a value that bypassed the types throws naming the placement.
  • onOut is required when the bundle has an OutMessage, so one cannot be dropped by omission. Write onOut: Bundle.ignore to drop it on purpose.
  • Without an args Schema, Args is inferred from init's parameter annotation instead.
  • The Model and Message Schemas declare those types, and init, update, subscriptions, view and the helpers are checked against them: init may write heights: {} or status: 'idle' and the Model stays the Schema's.
  • The bundle's services are what its Commands need, gathered from init, update and each helper separately, so a helper whose Command yields a service needs no annotation, and the placement carries it to the runtime's resources. make takes no explicit generics.

Derived args: args from the parent seed

args is a static value or a factory from the parent seed: the fields assembly.initial(rest) was given, before any placement initialised. A child whose initial state depends on what only the parent knows at startup — the route, the authenticated user, the workspace — derives it instead of taking a fixed value:

ts
const people = Page.at(PeopleDeclared, {
  args: parent => ({
    searchText: searchFromRoute(parent.route),
  }),
})

assembly.initial({ route: urlToAppRoute(url) })

The seed is rest plus empty collections, with no placement's fields in it. The factory's parameter omits its own field, so reading it is a type error; sibling fields are consistently absent at runtime, so read only seed fields and placement order never matters. The result is checked against the bundle's args Schema, naming the placement, then retained for update, helpers, Subscriptions, and resources. Subscriptions and resources that ignore args are readable before initial() runs; ones built from a factory need it (or runtime()) first, and name the placement when read too early. A factory runs once per seed it has seen: repeat initializations over equal seeds reuse the retained args, so one runtime flow derives once for its records and once is enough for its init. It never re-runs against live state, so keep it pure of its seed — return the same args for the same seed, and generate ids outside it. A factory on an optional child is skipped when rest starts the child as None. On a Model no initialization produced, update derives per use without retaining, so hand-built Models in tests fold with their own seed.