Remote / foldkit-remote

Routing Remote's Messages

The first run folds Remote's Messages under one variant of the application's union, the shape Foldkit gives a Submodel. The fold's fetch, mutate, and subscriptions yield that variant too, with the lift recorded so a Story resolves a fetch or a mutation by Remote's own answer.

The shorter path, for an update that only reduces, spreads Remote's cases into the union and narrows by tag; the rest of update is then a partial match over tags the application never handles:

ts
const Message = defineMessageUnion({ ...Remote.messages })

function update(model: Model, message: Message): Update.Return<Model, Message, RemoteClient> {
  if (Remote.reduces(message)) return { model: Data.reduce(model, message) }
  return { model }
}

The snippets below call the domain itself (Data.subscriptions, Data.mutate, Data.retry, Data.preload), as the spread form does. Under the fold, call the fold's members of the same names (foldData.mutate, …), which yield the wrapper Message; the members that only read or write the Model (Data.get, Data.refresh, Data.forget, …) stay on the domain either way.

One value with foldkit-bundle: Data.wiring

The integration steps above — fold or reduce, derive the Subscriptions, provide the client — are one value when the application uses foldkit-bundle:

ts
import { Option } from 'effect'
import { Bundle } from 'foldkit-bundle'

const Page = Bundle.parent({ Model, Message })
const wiring = Page.assemble(
  Data.wiring({
    project: Surface.at(ProjectPage, model =>
      model.route._tag === 'project'
        ? Option.some({ projectId: model.route.projectId })
        : Option.none(),
    ),
  }),
)
const wiredUpdate = wiring.update(model => ({ model }))

Data.wiring routes Remote's Messages into Data.reduce, brings the active Surfaces' Subscriptions and the domain's contract, and requires RemoteClient from the runtime's resources — the same client layer from above. One assembly holds at most one Remote domain: every domain claims the same Message tags, and the assembly refuses a second claimant at startup, naming both.