import { Option, Schema } from 'effect'; import { Update, type Runtime } from 'foldkit'; import type { Document, HtmlBuilder } from 'foldkit/html'; import { defineMessageUnion } from 'foldkit/message'; import { createJsx } from '@causeeffect/foldkit-jsx'; import * as Counter from './counter.gtsx'; export const CounterId = Schema.Literals(['left', 'right']); export type CounterId = typeof CounterId.Type; export const Model = Schema.Struct({ left: Counter.Model, right: Counter.Model, }); export type Model = typeof Model.Type; export const Message = defineMessageUnion({ GotCounterMessage: { counterId: CounterId, message: Counter.Message }, ClickedResetAll: {}, }); export type Message = typeof Message.Type; const wrapCounterMessage = (counterId: CounterId) => (message: Counter.Message) => Message.GotCounterMessage({ counterId, message }); // The selected instance is read and written through Foldkit's normal child fold. const foldCounter = (counterId: CounterId) => Update.foldChild({ update: Counter.update, read: (model: Model) => Option.some(model[counterId]), write: (model, counter) => ({ ...model, [counterId]: counter }), toParentMessage: wrapCounterMessage(counterId), }); const resetCounter = (counterId: CounterId) => Update.foldChildStep({ update: Counter.reset, read: (model: Model) => Option.some(model[counterId]), write: (model, counter) => ({ ...model, [counterId]: counter }), toParentMessage: wrapCounterMessage(counterId), }); export const update = (model: Model, message: Message) => Message.match>(message, { GotCounterMessage: ({ counterId, message }) => foldCounter(counterId)(model, message), ClickedResetAll: () => Update.combine(model, [resetCounter('left'), resetCounter('right')]), }); export const init: Runtime.ApplicationInit = () => ({ model: { left: Counter.init(), right: Counter.init() }, }); export const view = (model: Model, h: HtmlBuilder): Document => { const jsx = createJsx(h); const total = model.left.count + model.right.count; return { title: `Foldkit + gtsx · Total ${total}`, body: (

Foldkit × gtsx

One view. Two independent counters.

Each counter owns its model, messages, and update. JSX describes the view; Foldkit routes the interactions.

Combined total {total}

), }; };