Integrate with Effect UI
Effect UI continues the work of Foldkit 0.x. A component changes its Model through Messages, runs side effects as Commands, loads and follows data through queries, and receives feeds with Subscriptions. The @alchemy.run/domain/EffectUI adapter connects each of these to a Domain Api. It follows the Foldkit adapter: the failures and read rules are the same.
Install @effect/ui in the web package. Domain’s root entry does not import it. The examples below use the bank example’s CustomerApi.
Derive the services
Section titled “Derive the services”import * as DomainEffectUI from "@alchemy.run/domain/EffectUI";import { CustomerApi } from "@bank/domain/CustomerApi.api";
export const Bank = DomainEffectUI.make(CustomerApi);Bank has one Command.Service per Api command, one query per Api query, and one Query.StreamService per View. Each View also has the watch and events Subscriptions, and each Stream has them alone. Bank.layer implements every Command and query from the Api client. The name layer is reserved: make does not compile for an Api with an entry named layer.
Send a command
Section titled “Send a command”Each Command takes { input, commandId?, headers? }. It succeeds with { commandId, reply, receipt, commits }, and fails with Rejected, Denied, or Unknown, each with its commandId. Map both channels where the component issues the Command:
import { AccountId } from "@bank/domain/Account/Account.id";import { Command, UiSchema } from "@effect/ui";import { Html } from "@effect/ui/html";import * as Schema from "effect/Schema";import { Bank } from "./Bank.ts";
export const DepositMessage = UiSchema.TaggedEnum({ ClickedDeposit: {}, ClickedRetry: { commandId: Schema.String }, SucceededDeposit: { balance: Schema.Number }, FailedDeposit: { failure: Bank.DepositMoney.error },});
// Deposit 10.00. Pass the `commandId` of an `Unknown` failure to retry that write.const deposit = (accountId: AccountId, commandId?: string) => Bank.DepositMoney.make({ input: { accountId, amount: 1_000 }, commandId }).pipe( Command.match({ onSuccess: ({ reply }) => DepositMessage.SucceededDeposit({ balance: reply.balance }), onFailure: (failure) => DepositMessage.FailedDeposit({ failure }), }), );
export const Deposit = Html.component({ name: "Deposit", props: Schema.Struct({ accountId: AccountId }), model: Schema.Struct({ balance: Schema.NullOr(Schema.Number), failure: Schema.NullOr(Bank.DepositMoney.error), }), message: DepositMessage,}).make({ init: () => ({ balance: null, failure: null }), update: { ClickedDeposit: ($) => $.run(deposit($.props.accountId)), ClickedRetry: ($, { commandId }) => $.run(deposit($.props.accountId, commandId)), SucceededDeposit: ($, { balance }) => $.evolve({ balance, failure: null }), FailedDeposit: ($, { failure }) => $.evolve({ failure }), }, view: (h, model) => h.div( h.button.on("click", DepositMessage.ClickedDeposit()).children("Deposit 10.00"), model.balance !== null && h.p.attr("aria-label", "Balance").children(String(model.balance)), model.failure?._tag === "Unknown" ? h.button .on("click", DepositMessage.ClickedRetry({ commandId: model.failure.commandId })) .children("Retry") : model.failure !== null && h.p.attr("role", "alert").children(model.failure._tag), ),});Leave commandId out. The Command makes a new id each time it runs, so two instances that hydrate one server page, or an instance that history restore brings back, never send the same id twice. A Command defect produces Unknown. Retry Unknown with the id it returns, because the write can have committed: the Retry button above sends it back.
Bank.DepositMoney.error is the Schema of the failure, so a Message and the Model can hold it. Interruption ends the Command without a failure.
Wait for the views
Section titled “Wait for the views”A command answers when its events are committed. To answer once every view its events reach has applied them, set consistency per command:
import * as DomainEffectUI from "@alchemy.run/domain/EffectUI";import { CustomerApi } from "@bank/domain/CustomerApi.api";
export const Bank = DomainEffectUI.make(CustomerApi, { commands: { DepositMoney: { consistency: "strong" } },});A strong command can also fail with TimedOut { commandId, receipt }: the command committed at receipt, and a view did not apply it in time. TimedOut is in the type only for a command with consistency: "strong". Do not retry it as a new write. Without the option, the client’s Domain.DefaultConsistency decides, and a view timeout is Unknown.
Update the query cache
Section titled “Update the query cache”By default, a command invalidates each query whose waitsFor reads its events, and no other query. A View’s stream entry is not invalidated: its open stream sends the change. Declare what a command does to the cache with commands. Each callback receives the Command args and the Api’s queries and Views:
import * as DomainEffectUI from "@alchemy.run/domain/EffectUI";import { CustomerApi } from "@bank/domain/CustomerApi.api";import * as Option from "effect/Option";
export const Bank = DomainEffectUI.make(CustomerApi, { queries: { AccountLedger: { take: 20, key: (row) => row.id } }, commands: { DepositMoney: { // The ledger gains a row: fetch it again. invalidates: ({ input }, queries) => [ queries.AccountLedger.entry({ input: { account: input.accountId } }), ], // Show the deposit in the summary while it runs. optimistic: ({ input }, queries) => [ queries.AccountSummary.patch({ key: input.accountId }, (snapshot) => ({ ...snapshot, state: Option.map(snapshot.state, (summary) => ({ ...summary, balance: summary.balance + input.amount, })), })), ], }, },});invalidates replaces the default, and optimistic patches an entry while the run is queued or running. writes stores entries from a success without a refetch. Add Command.afterRefetch("2 seconds") to a Command to hold its success Message until the invalidated queries have refetched.
The adapter keeps the receipts of this client’s commands. A query with waitsFor, and each View stream when it opens, sends them as atLeast, so the result includes this client’s writes. The receipts are not part of the args, so they do not split the cache.
Ask a query
Section titled “Ask a query”A query is a Query.Service, so the Effect UI cache shares and refreshes its result. Its args are { input }. Declare it in a component’s queries:
import { CustomerId } from "@bank/domain/Customer/Customer.id";import { UiSchema } from "@effect/ui";import { Html } from "@effect/ui/html";import * as AsyncResult from "effect/reactivity/AsyncResult";import * as Schema from "effect/Schema";import { Bank } from "./Bank.ts";
export const Address = Html.component({ name: "Address", props: Schema.Struct({ customerId: CustomerId }), model: Schema.Struct({}), message: UiSchema.TaggedEnum({}), queries: { address: { query: Bank.CustomerAddress, args: ({ props }) => ({ input: { customerId: props.customerId } }), }, },}).make({ init: () => ({}), update: {}, view: (h) => AsyncResult.match(h.queries.address, { onInitial: () => h.p("Loading"), onFailure: ({ cause }) => h.p.attr("role", "alert").children(String(cause)), onSuccess: ({ value }) => h.p(value.address ?? "No address on file"), }),});A query fails with Rejected, Denied, TimedOut (only for a query with waitsFor), or Unknown. make(api, { staleTime }) sets how long a result stays fresh. The default is 0: the cache shows a stored result at once and fetches again.
Page through a query
Section titled “Page through a query”A paged query is a Query.InfiniteService. take sets the page size and key identifies an entry, in the queries option of make (see Update the query cache). Its value holds the entries in order, and $.loadMore loads the next page:
import { AccountId } from "@bank/domain/Account/Account.id";import { UiSchema } from "@effect/ui";import { Html } from "@effect/ui/html";import * as AsyncResult from "effect/reactivity/AsyncResult";import * as Schema from "effect/Schema";import { Bank } from "./Bank.ts";
export const LedgerMessage = UiSchema.TaggedEnum({ ClickedMore: {} });
export const Ledger = Html.component({ name: "Ledger", props: Schema.Struct({ accountId: AccountId }), model: Schema.Struct({}), message: LedgerMessage, queries: { rows: { query: Bank.AccountLedger, args: ({ props }) => ({ input: { account: props.accountId } }), }, },}).make({ init: () => ({}), update: { ClickedMore: ($) => $.loadMore("rows") }, view: (h) => AsyncResult.match(h.queries.rows, { onInitial: () => h.p("Loading"), onFailure: () => h.p.attr("role", "alert").children("Ledger unavailable"), onSuccess: ({ value: { items, more } }) => h.div( h.ul(...items.map((row) => h.li.key(row.id).children(`${row.kind} ${row.amount}`))), more._tag === "Available" && h.button.on("click", LedgerMessage.ClickedMore()).children("More"), ), }),});With key, an entry that a later page repeats is left out. Without it, every entry is kept.
Show a View
Section titled “Show a View”A View is a stream query keyed by its read key. Its args are { key }, and its value is the { state, cursor } snapshot, with absent state as Option.none():
import { AccountId } from "@bank/domain/Account/Account.id";import { UiSchema } from "@effect/ui";import { Html } from "@effect/ui/html";import * as Option from "effect/Option";import * as AsyncResult from "effect/reactivity/AsyncResult";import * as Schema from "effect/Schema";import { Bank } from "./Bank.ts";
export const Summary = Html.component({ name: "Summary", props: Schema.Struct({ accountId: AccountId }), model: Schema.Struct({}), message: UiSchema.TaggedEnum({}), queries: { summary: { query: Bank.AccountSummary, args: ({ props }) => ({ key: props.accountId }) }, },}).make({ init: () => ({}), update: {}, view: (h) => Option.match(AsyncResult.value(h.queries.summary), { onNone: () => h.p(h.queries.summary._tag === "Failure" ? "Not available" : "Loading"), onSome: ({ state }) => h.p.attr("aria-label", "Balance").children( Option.match(state, { onNone: () => "No account", onSome: (s) => String(s.balance) }), ), }),});AsyncResult.value gives the last Success, also when a later failure keeps it as its previousSuccess. h.queries.summary.waiting is true while the stream opens again.
Each stream starts with snapshot(key), with this client’s receipts as atLeast, so its first element includes this client’s writes. Then it follows watch(key). The adapter does not fold events: a View that declares events can change its state without an event. Every component that reads the same key shares one entry and one open stream. The stream closes when the entry is collected: by default 5 minutes after the last component that reads it unmounts. Set a shorter time per View with views: { AccountSummary: { idleTimeToLive: "30 seconds" } } in the options of make.
A View read fails with Rejected (the View’s validate), Denied (its middleware), TimedOut (the View did not apply this client’s writes in time), or Unknown (transport or a defect). The entry keeps the last value as previousSuccess. A failure closes the stream. It opens again when the entry is asked for again (a get, a new component, a refetch of a stale entry) or after invalidate.
Patch a View before the command answers
Section titled “Patch a View before the command answers”The optimistic patch in Update the query cache shows a deposit in AccountSummary at once. Effect UI applies the patch over each element while the run is queued or running. When the run succeeds, it closes the stream and opens it again, and the first element of the new stream drops the patch. A Domain View applies a command’s events a short time after the command answers, so a plain watch can send the old state first, and the value goes back for a moment. The adapter prevents this: the new stream starts with a snapshot that waits for the run’s receipt.
Each optimistic run still opens the stream again, which costs one more subscription and one more read of the key’s state. And while the run is still running, the open stream can send an element that already holds the write: the patch applies to it again, so a patch that adds shows the deposit twice until the run ends. Where you can, write patches that set a value, not ones that add to it (our comment on Effect-TS/ui#454).
Follow a View with Subscriptions
Section titled “Follow a View with Subscriptions”watch is the lower-level form of the stream query. It copies each snapshot into the Model through a Message, opens one stream per component, and has no data in a server render. Use it when update must handle each snapshot as a Message. events follows an event feed after the cursor after:
import { AccountId } from "@bank/domain/Account/Account.id";import { StatementLine } from "@bank/domain/Account/Statement.view";import { UiSchema } from "@effect/ui";import { Html } from "@effect/ui/html";import * as Option from "effect/Option";import * as Schema from "effect/Schema";import { Bank } from "./Bank.ts";
export const StatementMessage = UiSchema.TaggedEnum({ ReceivedBalance: { balance: Schema.Option(Schema.Number) }, ReceivedLine: { line: StatementLine, cursor: Schema.Number }, CaughtUp: { cursor: Schema.Number }, FailedRead: { reason: Schema.String },});type StatementMessage = typeof StatementMessage.Type;
export const Statement = Html.component({ name: "Statement", props: Schema.Struct({ accountId: AccountId }), model: Schema.Struct({ balance: Schema.Option(Schema.Number), lines: Schema.Array(StatementLine), cursor: Schema.Option(Schema.Number), }), message: StatementMessage,}).make({ init: () => ({ balance: Option.none(), lines: [], cursor: Option.none() }), update: { ReceivedBalance: ($, { balance }) => $.evolve({ balance }), ReceivedLine: ($, { line, cursor }) => $.evolve({ lines: (lines) => [...lines, line], cursor: Option.some(cursor) }), CaughtUp: ($, { cursor }) => $.evolve({ cursor: Option.some(cursor) }), FailedRead: ($) => $.model, }, subscriptions: ({ props, model }) => [ // Each snapshot of the account's summary, as a Message. Bank.AccountSummary.watch<StatementMessage>(props.accountId, { onSnapshot: ({ state }) => StatementMessage.ReceivedBalance({ balance: Option.map(state, (s) => s.balance) }), onFailure: (error) => StatementMessage.FailedRead({ reason: error._tag }), onDefect: () => StatementMessage.FailedRead({ reason: "Read defect" }), }), // Each statement line after the last one applied. Bank.Statement.events<StatementMessage>(props.accountId, { after: model.cursor, onEntry: ({ event, envelope }) => StatementMessage.ReceivedLine({ line: event, cursor: envelope.seq }), onCaughtUp: (cursor) => StatementMessage.CaughtUp({ cursor }), onFailure: (error) => StatementMessage.FailedRead({ reason: error._tag }), onDefect: () => StatementMessage.FailedRead({ reason: "Read defect" }), }), ], view: (h, model) => h.div( h.p(Option.match(model.balance, { onNone: () => "No account", onSome: String })), h.ul(...model.lines.map((line) => h.li(`${line.kind} ${line.amount}`))), ),});A new key or restartKey restarts a Subscription. Read defects call onDefect, typed read failures call onFailure, and interruption ends the Subscription without a Message.
The feed reads after when it starts, so a new cursor does not restart it. Change restartKey to restart it, for example after a failure. The feed drops repeated entries and allows gaps. A Stream has only these two Subscriptions.
Restart a read after the session ends
Section titled “Restart a read after the session ends”When the session ends, the server ends each live read with the middleware error (#3458). A View entry fails with Denied and its stream closes. Sign in again, then invalidate the View: the target covers all its entries, so each entry a component shows opens its stream again:
import { AccountId } from "@bank/domain/Account/Account.id";import { CustomerId } from "@bank/domain/Customer/Customer.id";import { Command, UiSchema } from "@effect/ui";import { Html } from "@effect/ui/html";import * as Effect from "effect/Effect";import * as Option from "effect/Option";import * as AsyncResult from "effect/reactivity/AsyncResult";import * as Schema from "effect/Schema";import { Bank } from "./Bank.ts";
export const SessionMessage = UiSchema.TaggedEnum({ ClickedSignIn: {}, SignedIn: {},});
// Sign in again, then reopen every AccountSummary entry the page shows.export const SignIn = Command.make("bank/SignIn", { args: { customerId: CustomerId } }, ({ customerId }) => Effect.promise(() => fetch("/api/session", { method: "POST", body: JSON.stringify({ customerId }) }), ).pipe(Effect.andThen(Bank.AccountSummary.invalidate()), Effect.as(SessionMessage.SignedIn())),);
export const Session = Html.component({ name: "Session", props: Schema.Struct({ customerId: CustomerId, accountId: AccountId }), model: Schema.Struct({}), message: SessionMessage, queries: { summary: { query: Bank.AccountSummary, args: ({ props }) => ({ key: props.accountId }) }, },}).make({ init: () => ({}), update: { ClickedSignIn: ($) => $.run(SignIn.make({ customerId: $.props.customerId })), SignedIn: ($) => $.model, }, view: (h) => h.queries.summary._tag === "Failure" && Option.isNone(AsyncResult.value(h.queries.summary)) ? h.button.on("click", SessionMessage.ClickedSignIn()).children("Sign in again") : h.p("Signed in"),});A Subscription calls onFailure and stops. Change its restartKey after the new sign-in to start it again. A query that failed with Denied keeps its failure until it fetches again: invalidate it too, or name it in the invalidates of a command that signs in.
Seed the first paint
Section titled “Seed the first paint”A server render stores the first element of each View entry and closes its stream. The page ships the entry, and the browser shows it without a read. The browser opens the stream once hydration’s fetch-free window closes, and then the View follows live changes. Nothing goes in the Model.
A server render does not run Subscriptions. A component that follows a View with watch can keep a snapshot in its Model instead. Bank.AccountSummary.SnapshotSchema is the Schema of { state, cursor }, and decodeSnapshot decodes a root prop: a stale state Schema gives None and a fresh watch, and boot continues. To follow the feed from a snapshot, pass its cursor to events as after.
Test a component with the model
Section titled “Test a component with the model”Mount the component with HtmlPlay, and build the adapter and the Api client inside the component tree:
import * as DomainEffectUI from "@alchemy.run/domain/EffectUI";import { AccountId } from "@bank/domain/Account/Account.id";import { Component } from "@effect/ui";import { HtmlLocator, HtmlPlay } from "@effect/ui/html/testing";import { TestHost } from "@effect/ui/testing";import { it } from "@effect/vitest";import * as Effect from "effect/Effect";import * as Layer from "effect/Layer";import { Client } from "./Client.ts";import { Bank } from "../src/Bank.ts";import { Deposit } from "../src/Deposit.ts";
// Every command id this test sends is "test:0", so a retry is easy to assert.const PinnedIds = Layer.succeed(DomainEffectUI.NewCommandId, Effect.succeed("test:0"));
it.effect("a deposit to an account that isn't open is refused", () => Effect.gen(function* () { const play = yield* HtmlPlay.make( Deposit.pipe( // The adapter and the in-memory client are built inside the tree, so the test // host waits for their work. Component.provide(Bank.layer.pipe(Layer.provide(PinnedIds), Layer.provideMerge(Client))), ), { accountId: AccountId.make("c-1-main") }, ); yield* play.click(HtmlLocator.role("button", { name: "Deposit 10.00" })); yield* play.expect(HtmlLocator.role("alert")).toHaveText("Rejected"); }).pipe(Effect.scoped, Effect.provide(TestHost.layer())),);Client is the Api’s in-process client over Domain.InMemory. TestHost.settle waits only for the fibers of the component tree, so a client provided beside TestHost runs outside it, and under the TestClock the View can still show the old value after a click (Effect-TS/ui#411). A command that the model schedules for later also needs the in-memory platform on the test clock: until #3690 lands, build it with InMemory.make({ now }) from @alchemy.run/domain/InMemory. NewCommandId pins the ids the Commands make.
Provide the client and headers
Section titled “Provide the client and headers”Provide Bank.layer and the Api client where the app mounts. Provide DomainEffectUI.CommandHeaders under Bank.layer:
import * as Domain from "@alchemy.run/domain";import * as DomainEffectUI from "@alchemy.run/domain/EffectUI";import { AccountId } from "@bank/domain/Account/Account.id";import { CustomerApi } from "@bank/domain/CustomerApi.api";import { Component } from "@effect/ui";import { BrowserHost } from "@effect/ui/html";import * as Effect from "effect/Effect";import * as FetchHttpClient from "effect/http/FetchHttpClient";import * as Layer from "effect/Layer";import * as Socket from "effect/socket/Socket";import { Bank } from "./Bank.ts";import { Summary } from "./Summary.ts";
// Read on every call and every query fetch: pass headers the app owns.const Headers = Layer.succeed(DomainEffectUI.CommandHeaders, { get: () => Effect.succeed({ "x-app-version": "1.0.0" }),});
// The browser sends the session cookie on every call and every WebSocket.const Connection = CustomerApi.layerClient.pipe( Layer.provide(Domain.protocolWebSocket(`${location.origin}/api`)), Layer.provide(Layer.merge(FetchHttpClient.layer, Socket.layerWebSocketConstructorGlobal)),);
const App = Summary.pipe(Component.provide(Bank.layer.pipe(Layer.provide(Headers))));
const Main = BrowserHost.mount(App, { accountId: AccountId.make("c-1-main") }, document.getElementById("app")!).pipe( Layer.provide(Connection),);
Layer.launch(Main).pipe(Effect.runFork);CommandHeaders.get receives the command tag or query name, and runs on each call. A Command also finds headers provided beside Bank.layer, but a query fetch can run outside the component that asked for it, so only headers under Bank.layer reach every query. Never put credentials in Command input or in the Model. The rules for cookies and protocols are in Integrate with Foldkit.
Send a secret with one command
Section titled “Send a secret with one command”A secret that the user types, such as an MFA code, goes in the Command’s headers arg. It is Redacted, so the Effect UI message log does not show it. Its headers replace the CommandHeaders headers with the same name:
import { AccountId } from "@bank/domain/Account/Account.id";import { Command, UiSchema } from "@effect/ui";import { Html } from "@effect/ui/html";import * as Redacted from "effect/Redacted";import * as Schema from "effect/Schema";import { Bank } from "./Bank.ts";
export const WithdrawMessage = UiSchema.TaggedEnum({ // Redacted, so the message log never shows the code. SubmittedWithdrawal: { code: Schema.Redacted(Schema.String) }, SucceededWithdrawal: {}, FailedWithdrawal: { reason: Schema.String },});
export const Withdraw = Html.component({ name: "Withdraw", props: Schema.Struct({ accountId: AccountId }), model: Schema.Struct({ error: Schema.NullOr(Schema.String) }), message: WithdrawMessage,}).make({ init: () => ({ error: null }), update: { SubmittedWithdrawal: ($, { code }) => $.run( Bank.WithdrawMoney.make({ input: { accountId: $.props.accountId, amount: 500 }, headers: Redacted.make({ "x-mfa-code": Redacted.value(code) }), }).pipe( Command.match({ onSuccess: () => WithdrawMessage.SucceededWithdrawal(), onFailure: (failure) => WithdrawMessage.FailedWithdrawal({ reason: failure._tag }), }), ), ), SucceededWithdrawal: ($) => $.evolve({ error: null }), FailedWithdrawal: ($, { reason }) => $.evolve({ error: reason }), }, view: (h, model) => h.form .on("submit", ({ entries }) => WithdrawMessage.SubmittedWithdrawal({ code: Redacted.make(entries.find(([name]) => name === "code")?.[1] ?? ""), }), ) .children( h.input.attrs({ name: "code", "aria-label": "MFA code", autocomplete: "one-time-code" }), h.button.attr("type", "submit").children("Withdraw 5.00"), model.error !== null && h.p.attr("role", "alert").children(model.error), ),});Render signed-in pages on the server
Section titled “Render signed-in pages on the server”HtmlServerHttp.layerHttp renders each request with its own query cache. A fetch runs in the request, so CommandHeaders.get can read the request’s cookie:
/// <reference types="@effect/ui/html/HtmlServerViteDocument" />import * as Domain from "@alchemy.run/domain";import * as DomainEffectUI from "@alchemy.run/domain/EffectUI";import { CustomerApi } from "@bank/domain/CustomerApi.api";import { Query } from "@effect/ui";import { layerHttp } from "@effect/ui/html/HtmlServerHttp";import * as Effect from "effect/Effect";import * as FetchHttpClient from "effect/http/FetchHttpClient";import * as HttpServerRequest from "effect/http/HttpServerRequest";import * as Layer from "effect/Layer";import * as Option from "effect/Option";import { document } from "virtual:@effect/ui/document";import { App } from "./App.ts";import { Bank } from "./Bank.ts";import { AppRoute } from "./routes.ts";
// A fetch runs in the request it renders, so each page's reads carry its visitor's cookie.const RequestHeaders = Layer.succeed(DomainEffectUI.CommandHeaders, { get: () => Effect.serviceOption(HttpServerRequest.HttpServerRequest).pipe( Effect.map( Option.match({ onNone: () => ({}), onSome: (request) => ({ cookie: request.headers.cookie ?? "" }), }), ), ),});
const Connection = CustomerApi.layerClient.pipe( Layer.provide(Domain.protocolHttp("https://bank.example.com/api")), Layer.provide(FetchHttpClient.layer),);
export default layerHttp(App, { routes: AppRoute, document }).pipe( Layer.provide(Layer.merge(Bank.layer.pipe(Layer.provide(RequestHeaders)), Query.layer())), Layer.provide(Connection),);Each request’s queries then run as that request’s user, and no custom handler is needed. In the browser, Effect.serviceOption finds no request and the browser sends the cookie.