/** * @title Organizing an Effect UI app * * Folder layout, `#domain` and `#queries` imports, and where Commands and * Layers live. */ import { Effect, Layer } from "effect" import { FetchHttpClient } from "effect/http" import { Router } from "effect/ui" import { BrowserHost, BrowserRouter } from "effect/ui/html" import { App } from "../fixtures/App.ts" import { AppRoute } from "../fixtures/routes.ts" // The fixtures are laid out as `src/` would be in a real app: // // App.ts root component; imports the pages // routes.ts AppRoute, apart from App.ts so pages can link // domain/Note.ts Schemas, errors, pure functions, Query.Tags // queries/GetNote.ts one Query per file (ListNotes, SearchNotes…) // kit/Listbox.ts headless or slotted parts no feature owns // components/ app components shared by several features // notes/ one folder per feature // services/Notes.ts a service and its Layers // components/NoteEditor.ts a component and the Commands it runs // pages/ one file per route, folders mirror the URL // NotesIndex.ts / // notes/NotePage.ts /notes/:id (a param segment gets no folder) // search/SearchPage.ts /search // server/NotesApi.ts the JSON API the Notes service calls // // Dependency direction: `domain` imports only packages and other `domain` // files. `queries` import `domain` and services. Features import `domain`, // `queries` and `kit`. Pages import features, shared components and // queries, and nothing imports a page except `App.ts`. A second feature is a // sibling of `notes/`, with its types in `domain/`. `queries/` starts flat and // can grow feature subfolders. // // Outside `domain` and `queries`, import them through subpath imports // declared once in the app's package.json (as `ai-docs/package.json` does): // // "imports": { // "#components/*": "./src/components/*.ts", // "#domain/*": "./src/domain/*.ts", // "#queries/*": "./src/queries/*.ts" // } // // Node, TypeScript, Vite and Vitest all read `imports` with no other config, // so a server entry still runs under plain `node`. The mapping adds the // `.ts`, so write `import { Note } from "#domain/Note"` with no extension. // Every other import is relative, with its `.ts` extension. // // Commands live in the file of the component that runs them, never in a // shared `commands/` folder. When two components need the same write, they // share the service method and each keeps its own Command. Queries get their // own folder because pages, components and the server all read them. // // A component provides the domain Layers it needs in its own file with // `Component.provide`: its Commands, its queries and the services they // capture. The plain name (`NotePage`) is the provided component, which // parents and entries use; `NotePageNoDeps` is the bare one, for tests. A // component that needs no domain Layers has only the plain name. A stateful // service, such as a session or a sync engine, is provided by the component // that owns its lifetime, usually the root. // An entry module sits beside these folders (`main.ts`, `server.ts`, the // tests). It defines no components and provides platform Layers only: the // Router, the HTTP client, logging. The server and a whole-app test swap the // HTTP client, never the domain services. const Main = Layer.suspend(() => BrowserHost.mount(App, Router.props(AppRoute), document.getElementById("app")!)).pipe( Layer.provide([BrowserRouter.layer({ routes: AppRoute }), FetchHttpClient.layer]) ) Layer.launch(Main).pipe(Effect.runFork)