effect-ui-tsx 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +295 -2
- package/dist/component/component.d.ts +56 -0
- package/dist/component/component.js +86 -0
- package/dist/component/component.js.map +1 -0
- package/dist/controller/controller.d.ts +157 -0
- package/dist/controller/controller.js +55 -0
- package/dist/controller/controller.js.map +1 -0
- package/dist/controller/operation.d.ts +61 -0
- package/dist/controller/operation.js +47 -0
- package/dist/controller/operation.js.map +1 -0
- package/dist/controller/queries.d.ts +48 -0
- package/dist/controller/queries.js +2 -0
- package/dist/controller/queries.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/model/model.d.ts +22 -0
- package/dist/model/model.js +21 -0
- package/dist/model/model.js.map +1 -0
- package/dist/runtime/jsx-dev-runtime.d.ts +1 -0
- package/dist/runtime/jsx-dev-runtime.js +2 -0
- package/dist/runtime/jsx-dev-runtime.js.map +1 -0
- package/dist/runtime/jsx-runtime.d.ts +25 -0
- package/dist/runtime/jsx-runtime.js +178 -0
- package/dist/runtime/jsx-runtime.js.map +1 -0
- package/dist/runtime/jsx.d.ts +148 -0
- package/dist/runtime/jsx.js +2 -0
- package/dist/runtime/jsx.js.map +1 -0
- package/package.json +42 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 priemskiyyy
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,296 @@
|
|
|
1
|
-
#
|
|
1
|
+
# effect-ui-tsx
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://github.com/priemskiyyy/effect-ui-tsx/actions/workflows/check.yml)
|
|
4
|
+
|
|
5
|
+
Write [Effect UI](https://github.com/Effect-TS/ui) components as a model, a controller and a plain TSX view.
|
|
6
|
+
|
|
7
|
+
```tsx
|
|
8
|
+
import { component, controller, model, type View } from "effect-ui-tsx"
|
|
9
|
+
|
|
10
|
+
const counterModel = model({ count: 0 })
|
|
11
|
+
|
|
12
|
+
const counterController = controller(counterModel).actions(({ on, reset }) => ({
|
|
13
|
+
increment: on({}, (state) => ({ count: state.count + 1 })),
|
|
14
|
+
restart: reset("count"),
|
|
15
|
+
}))
|
|
16
|
+
|
|
17
|
+
const CounterView: View<typeof counterController> = ({ state, action }) => (
|
|
18
|
+
<div>
|
|
19
|
+
<button type="button" onClick={action.increment}>
|
|
20
|
+
Clicked {state.count} times
|
|
21
|
+
</button>
|
|
22
|
+
<button type="button" disabled={state.count === 0} onClick={action.restart}>
|
|
23
|
+
Restart
|
|
24
|
+
</button>
|
|
25
|
+
</div>
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
export const Counter = component({
|
|
29
|
+
name: "Counter",
|
|
30
|
+
controller: counterController,
|
|
31
|
+
view: CounterView,
|
|
32
|
+
})
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
`Counter` is an ordinary Effect UI component with a model schema, tagged messages and an exhaustive update table. Mount it with the Effect UI browser host:
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import { BrowserHost } from "@effect/ui/html"
|
|
39
|
+
import { Effect, Layer } from "effect"
|
|
40
|
+
|
|
41
|
+
Effect.runFork(Layer.launch(BrowserHost.mount(Counter, {}, document.getElementById("app")!)))
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
- **Works with no tooling.** The package is a JSX runtime and three functions. Any bundler that handles TSX works as it is.
|
|
45
|
+
- **An optional compiler removes the wiring.** Declare `module({ name: "FilterField" })` and it finds the model, controller and view by convention.
|
|
46
|
+
- **Typed from the model outwards.** `state`, every `action`, and every event payload are inferred. A wrong field, payload or event is reported where you wrote it.
|
|
47
|
+
|
|
48
|
+
> [!NOTE]
|
|
49
|
+
> Effect UI is experimental and has no npm release yet. This package is built against a pinned snapshot of it, and both can change.
|
|
50
|
+
|
|
51
|
+
## Features
|
|
52
|
+
|
|
53
|
+
A controller is a chain. Each step declares one thing the feature has, and the view receives it typed.
|
|
54
|
+
|
|
55
|
+
| Feature | You write | Guide |
|
|
56
|
+
| ------------------ | ------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------- |
|
|
57
|
+
| State and actions | `model({ count: 0 })`, then `on`, `set` and `reset` in `.actions()` | [State and messages](docs/guide.md#model-state-and-messages) |
|
|
58
|
+
| Props and events | `.props()`, `.events()`, and `.emit()` on an action | [Props and events](docs/guide.md#props-and-events) |
|
|
59
|
+
| Slots | `.slots("title", "children")`, placed in the view as `slots.title` | [Slots](docs/guide.md#slots) |
|
|
60
|
+
| Commands | `.run()` on an action: requests that replace, cancel and report back | [Commands](docs/guide.md#commands) |
|
|
61
|
+
| Queries | `.queries()` and `.keyedQueries()`: cached data beside the state, with paged lists and `.loadMore()` | [Queries](docs/guide.md#queries) |
|
|
62
|
+
| Document head | `.head()`: the page title from state, props and query results | [The document head](docs/guide.md#the-document-head) |
|
|
63
|
+
| Shared state | `.reads()`: an atom several features read and write | [Shared state](docs/guide.md#shared-state) |
|
|
64
|
+
| Subscriptions | `.subscriptions()`: streams and document events while the feature is mounted | [Subscriptions](docs/guide.md#subscriptions) |
|
|
65
|
+
| Components as tags | `<FilterField placeholder="…" onChanged={…} />`, with `fallback`, `onFailure`, `slots` and `keepAlive` | [Compose with Effect UI](docs/views.md#compose-with-effect-ui) |
|
|
66
|
+
| Typed JSX | Every HTML and SVG tag and custom elements, with the event payloads Effect UI defines per element | [Write views](docs/views.md#write-views) |
|
|
67
|
+
| Element work | `ref`, `onMount`, `draggable`, `dropTarget`, and `apply` for the parts of a headless component | [Write views](docs/views.md#write-views) |
|
|
68
|
+
| Routing | Effect UI's router, with a feature as the shell | [Routing](docs/tooling.md#routing) |
|
|
69
|
+
| Server rendering | Render to HTML, then hydrate the same features in the browser | [Test and render](docs/tooling.md#test-and-render-on-the-server) |
|
|
70
|
+
| Devtools | Effect UI's own devtools, hooked up with one Layer | [Devtools](docs/tooling.md#devtools) |
|
|
71
|
+
| Lint rules | Five rules, shipped for both ESLint and Oxlint | [Lint](docs/tooling.md#lint) |
|
|
72
|
+
|
|
73
|
+
The JSX is checked against Effect UI, not React. A misspelt `onClik` and React's `onKeyDown` are type errors, and `className` and `strokeWidth` are errors that name the spelling to use.
|
|
74
|
+
|
|
75
|
+
A feature is used as a tag. Its props go in, and each event it reports comes back as an `on…` handler:
|
|
76
|
+
|
|
77
|
+
```tsx
|
|
78
|
+
export const FilteredListView: View = ({ state, action }) => (
|
|
79
|
+
<div>
|
|
80
|
+
<FilterField
|
|
81
|
+
key="filter"
|
|
82
|
+
placeholder="Type a fruit"
|
|
83
|
+
onChanged={({ text }) => action.filter(text)}
|
|
84
|
+
/>
|
|
85
|
+
<ul>
|
|
86
|
+
{fruit
|
|
87
|
+
.filter((name) => name.toLowerCase().includes(state.filter.toLowerCase()))
|
|
88
|
+
.map((name) => (
|
|
89
|
+
<li key={name}>{name}</li>
|
|
90
|
+
))}
|
|
91
|
+
</ul>
|
|
92
|
+
</div>
|
|
93
|
+
)
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## Install
|
|
97
|
+
|
|
98
|
+
Install the package together with Effect and the Effect UI snapshot it is built against:
|
|
99
|
+
|
|
100
|
+
```sh
|
|
101
|
+
pnpm add effect-ui-tsx effect \
|
|
102
|
+
"@effect/ui@https://pkg.pr.new/Effect-TS/ui/@effect/ui@df04560"
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Then point TypeScript's JSX at the package:
|
|
106
|
+
|
|
107
|
+
```json
|
|
108
|
+
{
|
|
109
|
+
"compilerOptions": {
|
|
110
|
+
"jsx": "react-jsx",
|
|
111
|
+
"jsxImportSource": "effect-ui-tsx",
|
|
112
|
+
"strict": true,
|
|
113
|
+
"exactOptionalPropertyTypes": true
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
- Effect UI requires `strict` and `exactOptionalPropertyTypes`.
|
|
119
|
+
- On TypeScript 6 and earlier, also set `skipLibCheck`. The Effect UI snapshot's own declarations exceed the checker's instantiation depth there. TypeScript 7 checks them in full.
|
|
120
|
+
- In an application that also uses another JSX runtime, put `/** @jsxImportSource effect-ui-tsx */` at the top of each Effect view instead of setting `jsxImportSource` globally.
|
|
121
|
+
|
|
122
|
+
## Run the examples
|
|
123
|
+
|
|
124
|
+
Use Node 24 or later and pnpm 10.
|
|
125
|
+
|
|
126
|
+
```sh
|
|
127
|
+
pnpm install
|
|
128
|
+
pnpm dev
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
`pnpm dev` compiles the examples, recompiles them as you edit, and serves the [examples page](examples/index.html) at the address it prints. The same page is [published from `main`](https://priemskiyyy.github.io/effect-ui-tsx/).
|
|
132
|
+
|
|
133
|
+
| Example | Shows |
|
|
134
|
+
| ---------------------------------------- | ----------------------------------------------------------------------------------------------- |
|
|
135
|
+
| [Counter](examples/counter.tsx) | A whole feature in one file, written without the compiler |
|
|
136
|
+
| [Filter field](examples/filter-field/) | A compiled feature, `set` and `reset`, a prop and an event |
|
|
137
|
+
| [Filtered list](examples/filtered-list/) | A parent that passes props to a child and handles its event |
|
|
138
|
+
| [Preferences](examples/preferences/) | A form's named fields and a checkbox |
|
|
139
|
+
| [Todos](examples/todos/) | A schema for an array, keyed rows, a function view |
|
|
140
|
+
| [Search](examples/search/) | A command that replaces and cancels requests, a tagged union |
|
|
141
|
+
| [Clock](examples/clock/) | Subscriptions: a stream that runs while the clock does, and a document key |
|
|
142
|
+
| [Facts](examples/facts/) | A query whose args follow the state, and a page title from the head |
|
|
143
|
+
| [Panel](examples/panel/) | A feature with slots: it owns being open, its parent owns the content |
|
|
144
|
+
| [Log](examples/log/) | Effect UI's message log, read by a feature and shown as a list |
|
|
145
|
+
| [Dashboard](examples/dashboard/) | Every example as a tag inside a panel, a fallback, provided Layers |
|
|
146
|
+
| [Site](examples/site/) | Routing: the shell of the examples page, with links, a catch-all route and a navigation command |
|
|
147
|
+
|
|
148
|
+
Two applications show the package in an ordinary stack. [apps/react-host](apps/react-host/) is a React app with React Router and Tailwind that embeds features as islands, which is how an existing React codebase would adopt this (`pnpm dev:react`). [apps/tasks](apps/tasks/) is written with features throughout: a task board built with Vite, Tailwind, routing, queries, commands that refresh them, and Effect UI's devtools. Run it with `pnpm dev:app`.
|
|
149
|
+
|
|
150
|
+
## Author a feature
|
|
151
|
+
|
|
152
|
+
With the compiler, a feature is a module folder. You write a one-line declaration and three files. The compiler finds them by convention, reads what each one exports, and writes the wiring next to them:
|
|
153
|
+
|
|
154
|
+
```text
|
|
155
|
+
filter-field/
|
|
156
|
+
module.ts you write: module({ name: "FilterField" })
|
|
157
|
+
model/
|
|
158
|
+
model.ts you write: the state and its schemas
|
|
159
|
+
controller/
|
|
160
|
+
filterController.ts you write: props, events and actions
|
|
161
|
+
filterController.effect-ui.ts generated: the controller, bound to the model
|
|
162
|
+
view/
|
|
163
|
+
FilterFieldView.tsx you write: the markup
|
|
164
|
+
bindings.effect-ui.ts generated: controller() for this model
|
|
165
|
+
module.effect-ui.ts generated: FilterField, View and ViewProps
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
| From | The compiler takes |
|
|
169
|
+
| --------------------------- | ---------------------------------------------------------- |
|
|
170
|
+
| `module.ts` | The component's name, and which controller and view to use |
|
|
171
|
+
| `model/model.ts` | The exported model, so `controller()` needs no argument |
|
|
172
|
+
| `controller/*Controller.ts` | The exported controller, so `View` needs no type argument |
|
|
173
|
+
| `view/FilterFieldView.tsx` | The exported view, matched to the declaration's name |
|
|
174
|
+
|
|
175
|
+
The generated module is the wiring you would otherwise write in every feature:
|
|
176
|
+
|
|
177
|
+
```ts
|
|
178
|
+
// module.effect-ui.ts, generated
|
|
179
|
+
import { component, type View as ViewOf, type ViewProps as ViewPropsOf } from "effect-ui-tsx"
|
|
180
|
+
import { filterController as featureController } from "./controller/filterController.effect-ui"
|
|
181
|
+
import { FilterFieldView as featureView } from "./view/FilterFieldView"
|
|
182
|
+
|
|
183
|
+
export { controller } from "./bindings.effect-ui"
|
|
184
|
+
|
|
185
|
+
export type View = ViewOf<typeof featureController>
|
|
186
|
+
export type ViewProps = ViewPropsOf<typeof featureController>
|
|
187
|
+
|
|
188
|
+
export const FilterField = component({
|
|
189
|
+
name: "FilterField",
|
|
190
|
+
controller: featureController,
|
|
191
|
+
view: featureView,
|
|
192
|
+
})
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
Git ignores the generated files, and your code never names them: `./module` resolves to `module.effect-ui.ts`. The four files you write:
|
|
196
|
+
|
|
197
|
+
```ts
|
|
198
|
+
// module.ts
|
|
199
|
+
import { module } from "effect-ui-tsx"
|
|
200
|
+
|
|
201
|
+
module({ name: "FilterField" })
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
```ts
|
|
205
|
+
// model/model.ts
|
|
206
|
+
import { model } from "effect-ui-tsx"
|
|
207
|
+
|
|
208
|
+
export const filterModel = model({ text: "" })
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
```ts
|
|
212
|
+
// controller/filterController.ts
|
|
213
|
+
import { Schema } from "effect"
|
|
214
|
+
import { controller } from "../module"
|
|
215
|
+
|
|
216
|
+
export const filterController = controller()
|
|
217
|
+
.props({ placeholder: Schema.String })
|
|
218
|
+
.events({ changed: { text: Schema.String } })
|
|
219
|
+
.actions(({ set, reset }) => ({
|
|
220
|
+
changeText: set("text").emit("changed", ({ text }) => ({ text })),
|
|
221
|
+
clear: reset("text").emit("changed", ({ text }) => ({ text })),
|
|
222
|
+
}))
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
```tsx
|
|
226
|
+
// view/FilterFieldView.tsx
|
|
227
|
+
import type { View } from "../module"
|
|
228
|
+
|
|
229
|
+
export const FilterFieldView: View = ({ state, action, props }) => (
|
|
230
|
+
<label>
|
|
231
|
+
Filter items
|
|
232
|
+
<input
|
|
233
|
+
placeholder={props.placeholder}
|
|
234
|
+
value={state.text}
|
|
235
|
+
onInput={({ value }) => action.changeText(value)}
|
|
236
|
+
/>
|
|
237
|
+
</label>
|
|
238
|
+
)
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
Consumers import the finished component from the feature's module: `import { FilterField } from "./filter-field/module"`.
|
|
242
|
+
|
|
243
|
+
The controller and the view import from `../module` too. There, `controller()` is already bound to the feature's model, and `View` is already the view type of this feature.
|
|
244
|
+
|
|
245
|
+
## The compiler
|
|
246
|
+
|
|
247
|
+
The compiler reads `module.ts` and writes a `module.effect-ui.ts` next to it, which assembles the feature. TypeScript and the bundler resolve `./module` to that generated file. Generated files are ignored by Git and rebuilt on demand.
|
|
248
|
+
|
|
249
|
+
```sh
|
|
250
|
+
pnpm add -D @priemskiyyy/effect-ui-tsx-compiler
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
Tell TypeScript about the generated files in `tsconfig.json`:
|
|
254
|
+
|
|
255
|
+
```json
|
|
256
|
+
{ "compilerOptions": { "moduleSuffixes": [".effect-ui", ""] } }
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
With Vite, add the plugin. It finds every feature in the project, compiles them before a build and again as you edit, and needs no other setup:
|
|
260
|
+
|
|
261
|
+
```ts
|
|
262
|
+
// vite.config.ts
|
|
263
|
+
import { effectUiTsx } from "@priemskiyyy/effect-ui-tsx-compiler/vite"
|
|
264
|
+
import { defineConfig } from "vite"
|
|
265
|
+
|
|
266
|
+
export default defineConfig({ plugins: [effectUiTsx()] })
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
The [compiler's README](packages/compiler/README.md) covers the esbuild plugin, the command line, how a declaration can name its controller and view, and the diagnostic codes.
|
|
270
|
+
|
|
271
|
+
The compiler is optional. Everything it writes can be written by hand, as the counter at the top of this page is: `controller(model)` instead of `controller()`, `View<typeof controller>` instead of `View`, and `component({ name, controller, view })` instead of `module({ name })`. The rest of this page writes `controller()`.
|
|
272
|
+
|
|
273
|
+
A controller also runs without a view or a DOM, which keeps its transitions easy to test:
|
|
274
|
+
|
|
275
|
+
```ts
|
|
276
|
+
const { changeText } = filterController.operations
|
|
277
|
+
changeText.update({ text: "" }, { value: "needle" }, { placeholder: "" }) // { text: "needle" }
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
## Documentation
|
|
281
|
+
|
|
282
|
+
| Page | Covers |
|
|
283
|
+
| --------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- |
|
|
284
|
+
| [Feature guide](docs/guide.md) | State and messages, props and events, slots, commands, queries, the document head, shared state, subscriptions |
|
|
285
|
+
| [Views and composition](docs/views.md) | The JSX mapping, control flow, components as tags, headless and lazy components |
|
|
286
|
+
| [Routing, devtools, testing and linting](docs/tooling.md) | The router, Effect UI's devtools, stories and scenes, server rendering and hydration, the lint rules |
|
|
287
|
+
| [Reference](docs/reference.md) | What is not covered, the API at a glance, troubleshooting |
|
|
288
|
+
| [The compiler](packages/compiler/README.md) | The Vite and esbuild plugins, the command line, diagnostics |
|
|
289
|
+
|
|
290
|
+
## Develop
|
|
291
|
+
|
|
292
|
+
`pnpm check` runs everything. [CONTRIBUTING.md](CONTRIBUTING.md) describes the layout of the repository, the conventions of its code and how it is tested.
|
|
293
|
+
|
|
294
|
+
## License
|
|
295
|
+
|
|
296
|
+
[MIT](LICENSE)
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { type Command, type Component, type View as ViewModule } from "@effect/ui";
|
|
2
|
+
import { Html } from "@effect/ui/html";
|
|
3
|
+
import type { CommandOf, Controller, Message, EmbeddedAll } from "../controller/controller.js";
|
|
4
|
+
import type { Cases, EventOf } from "../controller/operation.js";
|
|
5
|
+
import type { QueryResults } from "../controller/queries.js";
|
|
6
|
+
import type { Fields, State } from "../model/model.js";
|
|
7
|
+
import { type ActionMessage, type JSX } from "../runtime/jsx-runtime.js";
|
|
8
|
+
type Props<F extends Fields, PR extends Fields, O, Slot extends string, Q, Parts, Rd> = {
|
|
9
|
+
state: State<F>;
|
|
10
|
+
props: State<PR>;
|
|
11
|
+
/** The shared state the feature reads, by the name it declared each under. */
|
|
12
|
+
reads: Component.SharedValues<Rd>;
|
|
13
|
+
/** The components the feature declared, ready to use as tags. */
|
|
14
|
+
components: EmbeddedAll<Parts>;
|
|
15
|
+
/** The latest result of each query the feature declares. */
|
|
16
|
+
queries: QueryResults<Q>;
|
|
17
|
+
/** Where the parent's content for each declared slot goes. Place each one once. */
|
|
18
|
+
slots: {
|
|
19
|
+
readonly [Name in Slot]: ViewModule.Outlet;
|
|
20
|
+
};
|
|
21
|
+
action: {
|
|
22
|
+
[K in keyof O]: O[K] extends {
|
|
23
|
+
args: (...args: infer Args) => unknown;
|
|
24
|
+
} ? (...args: Args) => ActionMessage : never;
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* What a controller's view receives: the current `state`, the `props` from the parent, one
|
|
29
|
+
* `action` per message and the `slots` it declared. Use `ViewProps<typeof myController>["action"]` to pass the actions on to a
|
|
30
|
+
* function view.
|
|
31
|
+
*/
|
|
32
|
+
export type ViewProps<C> = C extends Controller<infer F, infer PR, any, infer O, infer Slot, any, infer Q, infer Parts, infer Rd> ? Props<F, PR, O, Slot, Q, Parts, Rd> : never;
|
|
33
|
+
/**
|
|
34
|
+
* Any feature or Effect UI component that needs no Layer, by its props and the events it reports.
|
|
35
|
+
* It types code that mounts components it does not know, such as an island in another framework.
|
|
36
|
+
*/
|
|
37
|
+
export type Mountable<Props = {}, Events = never> = Component.Members<string, Props, any, any, Events, never, Html.Elements, any>;
|
|
38
|
+
/** The view of a controller: `const MyView: View<typeof myController> = ({ state, action }) => …` */
|
|
39
|
+
export type View<C> = (props: ViewProps<C>) => JSX.Element;
|
|
40
|
+
/** Assembles a controller and its view into an ordinary Effect UI component. */
|
|
41
|
+
export declare function component<const Name extends string, F extends Fields, PR extends Fields, E extends Cases, O, Slot extends string = never, SR = never, Q = {}, Parts = {}, Rd = {}>(options: {
|
|
42
|
+
name: Name;
|
|
43
|
+
controller: Controller<F, PR, E, O, Slot, SR, Q, Parts, Rd>;
|
|
44
|
+
view: (props: Props<NoInfer<F>, NoInfer<PR>, NoInfer<O>, NoInfer<Slot>, NoInfer<Q>, NoInfer<Parts>, NoInfer<Rd>>) => JSX.Element;
|
|
45
|
+
}): Component.Component<Name, State<PR>, State<F>, Message<O>, EventOf<E>, Command.Requirements<CommandOf<O>> | SR | Component.QueryServices<Q>, Html.Elements, Q, Slot>;
|
|
46
|
+
/**
|
|
47
|
+
* Declares a feature for the effect-ui-tsx compiler, in the feature's `module.ts`. The compiler
|
|
48
|
+
* finds the model, controller and view by convention, or uses the ones named here, and generates
|
|
49
|
+
* the module that assembles them. Imports of `./module` resolve to that generated module.
|
|
50
|
+
*/
|
|
51
|
+
export declare function module<const Options extends {
|
|
52
|
+
name: string;
|
|
53
|
+
controller?: unknown;
|
|
54
|
+
view?: unknown;
|
|
55
|
+
}>(options: Options): Options;
|
|
56
|
+
export {};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { UiSchema } from "@effect/ui";
|
|
2
|
+
import { Html } from "@effect/ui/html";
|
|
3
|
+
import { Schema } from "effect";
|
|
4
|
+
import { render } from "../runtime/jsx-runtime.js";
|
|
5
|
+
/** Assembles a controller and its view into an ordinary Effect UI component. */
|
|
6
|
+
export function component(options) {
|
|
7
|
+
const { model, operations, props, events, init, slots, subscribed, head } = options.controller;
|
|
8
|
+
const shown = ($) => ({ state: $.model, props: $.props, queries: $.queries });
|
|
9
|
+
const table = (entry) => Object.fromEntries(Object.entries(operations).map(([key, value]) => [
|
|
10
|
+
key,
|
|
11
|
+
entry(value, key),
|
|
12
|
+
]));
|
|
13
|
+
// The tables below are assembled from the controller's keys at runtime, so Effect UI cannot infer
|
|
14
|
+
// them. The signature above states the same types.
|
|
15
|
+
const message = UiSchema.TaggedEnum(table((operation) => operation.payload));
|
|
16
|
+
const event = UiSchema.TaggedEnum(events);
|
|
17
|
+
const action = table((operation, key) => (...args) => message[key](operation.args(...args)));
|
|
18
|
+
// Effect UI names the model where a feature says state. A query with a declared reaction gets
|
|
19
|
+
// it as its onChange.
|
|
20
|
+
const reactions = options.controller.changes?.({ action: action }) ?? {};
|
|
21
|
+
const queries = Object.entries(options.controller.queries).map(([name, entry]) => [
|
|
22
|
+
name,
|
|
23
|
+
{
|
|
24
|
+
...entry,
|
|
25
|
+
args: ($) => entry.args(shown($)),
|
|
26
|
+
...(reactions[name] && { onChange: reactions[name] }),
|
|
27
|
+
},
|
|
28
|
+
]);
|
|
29
|
+
const definition = Html.component({
|
|
30
|
+
name: options.name,
|
|
31
|
+
props: Schema.Struct(props),
|
|
32
|
+
model: Schema.Struct(model.fields),
|
|
33
|
+
message,
|
|
34
|
+
events: event,
|
|
35
|
+
// Effect UI gives the view its outlets only when the definition names slots.
|
|
36
|
+
...(slots.length > 0 && { slots }),
|
|
37
|
+
...(queries.length > 0 && { queries: Object.fromEntries(queries) }),
|
|
38
|
+
...(options.controller.restore !== undefined && { restore: options.controller.restore }),
|
|
39
|
+
...(Object.keys(options.controller.reads).length > 0 && {
|
|
40
|
+
reads: options.controller.reads,
|
|
41
|
+
}),
|
|
42
|
+
});
|
|
43
|
+
return definition.make({
|
|
44
|
+
init: ($) => (init ? init($.props) : model.initial),
|
|
45
|
+
update: table((operation) => (step, received) => {
|
|
46
|
+
const { props, queries } = step;
|
|
47
|
+
const next = operation.update(step.model, received, props, queries);
|
|
48
|
+
if (operation.whenChanged && next === step.model) {
|
|
49
|
+
return next;
|
|
50
|
+
}
|
|
51
|
+
const commands = operation.commands.map((make) => make(next, received, props, queries));
|
|
52
|
+
const emitted = operation.emits.flatMap(({ tag, payload }) => {
|
|
53
|
+
const reported = payload(next, received, props, queries);
|
|
54
|
+
return reported === undefined ? [] : [event[tag](reported)];
|
|
55
|
+
});
|
|
56
|
+
const settled = step
|
|
57
|
+
.set(next)
|
|
58
|
+
.run(...commands)
|
|
59
|
+
.emit(...emitted);
|
|
60
|
+
// An infinite query is asked for its next page, or to start over, by name.
|
|
61
|
+
return operation.lists.reduce((asked, { issue, query }) => asked[issue](query), settled);
|
|
62
|
+
}),
|
|
63
|
+
// The subscriptions are declared again from each new state. Effect UI keeps the ones whose
|
|
64
|
+
// key is unchanged running.
|
|
65
|
+
subscriptions: subscribed && (($) => subscribed({ ...shown($), action: action })),
|
|
66
|
+
head: head && (($) => head(shown($))),
|
|
67
|
+
view: definition.view((host, state) => render(options.view({
|
|
68
|
+
state,
|
|
69
|
+
action,
|
|
70
|
+
props: host.props,
|
|
71
|
+
slots: host.slots,
|
|
72
|
+
queries: host.queries,
|
|
73
|
+
components: options.controller.components,
|
|
74
|
+
reads: host.reads,
|
|
75
|
+
}), host)),
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Declares a feature for the effect-ui-tsx compiler, in the feature's `module.ts`. The compiler
|
|
80
|
+
* finds the model, controller and view by convention, or uses the ones named here, and generates
|
|
81
|
+
* the module that assembles them. Imports of `./module` resolve to that generated module.
|
|
82
|
+
*/
|
|
83
|
+
export function module(options) {
|
|
84
|
+
return options;
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=component.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"component.js","sourceRoot":"","sources":["../../src/component/component.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAyD,MAAM,YAAY,CAAA;AAC5F,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAA;AACtC,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAK/B,OAAO,EAAE,MAAM,EAAgC,MAAM,yBAAyB,CAAA;AA2D9E,gFAAgF;AAChF,MAAM,UAAU,SAAS,CAWvB,OAcD;IAWC,MAAM,EAAE,KAAK,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,UAAU,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC,UAAU,CAAA;IAE9F,MAAM,KAAK,GAAG,CAAC,CAAQ,EAAE,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC,KAAK,EAAE,OAAO,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC,CAAA;IACpF,MAAM,KAAK,GAAG,CAAI,KAAkD,EAAE,EAAE,CACtE,MAAM,CAAC,WAAW,CAChB,MAAM,CAAC,OAAO,CAAC,UAA0C,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC;QAC/E,GAAG;QACH,KAAK,CAAC,KAAK,EAAE,GAAG,CAAC;KAClB,CAAC,CACH,CA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AAE,IAAI,CAAC,KAAK;YACjB,KAAK,EAAE,IAAI,CAAC,KAAK;YACjB,OAAO,EAAE,IAAI,CAAC,OAAO;YACrB,UAAU,EAAE,OAAO,CAAC,UAAU,CAAC,UAAU;YACzC,KAAK,EAAE,IAAI,CAAC,KAAK;SACgC,CAAC,EACpD,IAAI,CACL,CACJ;KACF,CAAC,CAAA;AACJ,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,MAAM,CAEpB,OAAgB;IAChB,OAAO,OAAO,CAAA;AAChB,CAAC","sourcesContent":["import { UiSchema, type Command, type Component, type View as ViewModule } from \"@effect/ui\"\nimport { Html } from \"@effect/ui/html\"\nimport { Schema } from \"effect\"\nimport type { CommandOf, Controller, Message, EmbeddedAll } from \"src/controller/controller\"\nimport type { AnyOperation, Cases, EventOf } from \"src/controller/operation\"\nimport type { QueryResults } from \"src/controller/queries\"\nimport type { Fields, State } from \"src/model/model\"\nimport { render, type ActionMessage, type JSX } from \"src/runtime/jsx-runtime\"\n\n// A query entry as a controller holds it, before its args are read from the current state.\ntype AnyQuery = { args: (shown: object) => unknown }\n\ntype Props<F extends Fields, PR extends Fields, O, Slot extends string, Q, Parts, Rd> = {\n state: State<F>\n props: State<PR>\n /** The shared state the feature reads, by the name it declared each under. */\n reads: Component.SharedValues<Rd>\n /** The components the feature declared, ready to use as tags. */\n components: EmbeddedAll<Parts>\n /** The latest result of each query the feature declares. */\n queries: QueryResults<Q>\n /** Where the parent's content for each declared slot goes. Place each one once. */\n slots: { readonly [Name in Slot]: ViewModule.Outlet }\n action: {\n [K in keyof O]: O[K] extends { args: (...args: infer Args) => unknown }\n ? (...args: Args) => ActionMessage\n : never\n }\n}\n/**\n * What a controller's view receives: the current `state`, the `props` from the parent, one\n * `action` per message and the `slots` it declared. Use `ViewProps<typeof myController>[\"action\"]` to pass the actions on to a\n * function view.\n */\nexport type ViewProps<C> =\n C extends Controller<\n infer F,\n infer PR,\n any,\n infer O,\n infer Slot,\n any,\n infer Q,\n infer Parts,\n infer Rd\n >\n ? Props<F, PR, O, Slot, Q, Parts, Rd>\n : never\n/**\n * Any feature or Effect UI component that needs no Layer, by its props and the events it reports.\n * It types code that mounts components it does not know, such as an island in another framework.\n */\nexport type Mountable<Props = {}, Events = never> = Component.Members<\n string,\n Props,\n any,\n any,\n Events,\n never,\n Html.Elements,\n any\n>\n\n/** The view of a controller: `const MyView: View<typeof myController> = ({ state, action }) => …` */\nexport type View<C> = (props: ViewProps<C>) => JSX.Element\n\n/** Assembles a controller and its view into an ordinary Effect UI component. */\nexport function component<\n const Name extends string,\n F extends Fields,\n PR extends Fields,\n E extends Cases,\n O,\n Slot extends string = never,\n SR = never,\n Q = {},\n Parts = {},\n Rd = {},\n>(options: {\n name: Name\n controller: Controller<F, PR, E, O, Slot, SR, Q, Parts, Rd>\n view: (\n props: Props<\n NoInfer<F>,\n NoInfer<PR>,\n NoInfer<O>,\n NoInfer<Slot>,\n NoInfer<Q>,\n NoInfer<Parts>,\n NoInfer<Rd>\n >,\n ) => JSX.Element\n}): Component.Component<\n Name,\n State<PR>,\n State<F>,\n Message<O>,\n EventOf<E>,\n Command.Requirements<CommandOf<O>> | SR | Component.QueryServices<Q>,\n Html.Elements,\n Q,\n Slot\n> {\n const { model, operations, props, events, init, slots, subscribed, head } = options.controller\n type Shown = { model: State<F>; props: State<PR>; queries: QueryResults<Q> }\n const shown = ($: Shown) => ({ state: $.model, props: $.props, queries: $.queries })\n const table = <A>(entry: (operation: AnyOperation, key: string) => A) =>\n Object.fromEntries(\n Object.entries(operations as Record<string, AnyOperation>).map(([key, value]) => [\n key,\n entry(value, key),\n ]),\n )\n // The tables below are assembled from the controller's keys at runtime, so Effect UI cannot infer\n // them. The signature above states the same types.\n const message: any = UiSchema.TaggedEnum(table((operation) => operation.payload) as never)\n const event: any = UiSchema.TaggedEnum(events as never)\n const action = table(\n (operation, key) =>\n (...args: unknown[]) =>\n message[key](operation.args(...args)),\n )\n // Effect UI names the model where a feature says state. A query with a declared reaction gets\n // it as its onChange.\n const reactions = options.controller.changes?.({ action: action as never }) ?? {}\n const queries = Object.entries(options.controller.queries as Record<string, AnyQuery>).map(\n ([name, entry]) => [\n name,\n {\n ...entry,\n args: ($: Shown) => entry.args(shown($)),\n ...(reactions[name] && { onChange: reactions[name] }),\n },\n ],\n )\n const definition: any = Html.component({\n name: options.name,\n props: Schema.Struct(props) as never,\n model: Schema.Struct(model.fields) as never,\n message,\n events: event,\n // Effect UI gives the view its outlets only when the definition names slots.\n ...(slots.length > 0 && { slots }),\n ...(queries.length > 0 && { queries: Object.fromEntries(queries) }),\n ...(options.controller.restore !== undefined && { restore: options.controller.restore }),\n ...(Object.keys(options.controller.reads as object).length > 0 && {\n reads: options.controller.reads as never,\n }),\n })\n type Step = Component.Step<unknown, unknown, never> & { props: unknown; queries: never }\n return definition.make({\n init: ($: { props: State<PR> }) => (init ? init($.props) : model.initial),\n update: table((operation) => (step: Step, received: never) => {\n const { props, queries } = step\n const next = operation.update(step.model, received, props, queries)\n if (operation.whenChanged && next === step.model) {\n return next\n }\n const commands = operation.commands.map((make) => make(next, received, props, queries))\n const emitted = operation.emits.flatMap(({ tag, payload }) => {\n const reported = payload(next, received, props, queries)\n return reported === undefined ? [] : [event[tag](reported)]\n })\n const settled = step\n .set(next)\n .run(...(commands as never[]))\n .emit(...(emitted as never[]))\n // An infinite query is asked for its next page, or to start over, by name.\n return operation.lists.reduce((asked: any, { issue, query }) => asked[issue](query), settled)\n }),\n // The subscriptions are declared again from each new state. Effect UI keeps the ones whose\n // key is unchanged running.\n subscriptions:\n subscribed && (($: Shown) => subscribed({ ...shown($), action: action as never })),\n head: head && (($: Shown) => head(shown($))),\n view: definition.view(\n (\n host: Html.H<unknown> & { props: State<PR>; slots: object; queries: object; reads: object },\n state: State<F>,\n ) =>\n render(\n options.view({\n state,\n action,\n props: host.props,\n slots: host.slots,\n queries: host.queries,\n components: options.controller.components,\n reads: host.reads,\n } as unknown as Props<F, PR, O, Slot, Q, Parts, Rd>),\n host,\n ),\n ),\n })\n}\n\n/**\n * Declares a feature for the effect-ui-tsx compiler, in the feature's `module.ts`. The compiler\n * finds the model, controller and view by convention, or uses the ones named here, and generates\n * the module that assembles them. Imports of `./module` resolve to that generated module.\n */\nexport function module<\n const Options extends { name: string; controller?: unknown; view?: unknown },\n>(options: Options): Options {\n return options\n}\n"]}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import type { Command, Component, Subscription, UiSchema, View } from "@effect/ui";
|
|
2
|
+
import type { Html } from "@effect/ui/html";
|
|
3
|
+
import { type AnyOperation, type Cases, type Helpers } from "./operation.js";
|
|
4
|
+
import type { Keyed, KeyedDeclarations, Queries, QueryDeclarations, QueryResults, Single } from "./queries.js";
|
|
5
|
+
import type { Fields, Model, State } from "../model/model.js";
|
|
6
|
+
/** The tagged union of a controller's messages. */
|
|
7
|
+
export type Message<O> = {
|
|
8
|
+
[K in keyof O & string]: O[K] extends {
|
|
9
|
+
payload: infer P extends Fields;
|
|
10
|
+
} ? {
|
|
11
|
+
readonly _tag: K;
|
|
12
|
+
} & State<P> : never;
|
|
13
|
+
}[keyof O & string];
|
|
14
|
+
/** The union of the commands a controller's operations run. */
|
|
15
|
+
export type CommandOf<O> = {
|
|
16
|
+
[K in keyof O]: O[K] extends {
|
|
17
|
+
commands: ReadonlyArray<(...args: never[]) => infer C>;
|
|
18
|
+
} ? C : never;
|
|
19
|
+
}[keyof O];
|
|
20
|
+
/** The message each action builds, by action name. Subscriptions send these. */
|
|
21
|
+
export type Messages<O> = {
|
|
22
|
+
[K in keyof O & string]: O[K] extends {
|
|
23
|
+
args: (...args: infer Args) => unknown;
|
|
24
|
+
} ? (...args: Args) => Extract<Message<O>, {
|
|
25
|
+
readonly _tag: K;
|
|
26
|
+
}> : never;
|
|
27
|
+
};
|
|
28
|
+
/** The components a feature embeds without providing their Layers itself, by name. */
|
|
29
|
+
export type Components = Record<string, Component.Any>;
|
|
30
|
+
type Embedded<C> = C extends Component.Component<infer Name, infer Props, infer Model, infer Message, infer Events, infer _Requirements, infer Elements, infer QueryList, infer Slot> ? Component.Component<Name, Props, Model, Message, Events, never, Elements, QueryList, Slot> : never;
|
|
31
|
+
export type EmbeddedAll<Parts> = {
|
|
32
|
+
readonly [Name in keyof Parts]: Embedded<Parts[Name]>;
|
|
33
|
+
};
|
|
34
|
+
type RequiredBy<Parts> = {
|
|
35
|
+
[Name in keyof Parts]: Component.Requirements<Parts[Name]>;
|
|
36
|
+
}[keyof Parts];
|
|
37
|
+
/** The owners of shared state a feature reads, by the name it reads each under. */
|
|
38
|
+
export type Reads = Readonly<Record<string, Component.Shared.Owner>>;
|
|
39
|
+
type Shown<F extends Fields, PR extends Fields, Q> = {
|
|
40
|
+
state: State<F>;
|
|
41
|
+
props: State<PR>;
|
|
42
|
+
queries: QueryResults<Q>;
|
|
43
|
+
};
|
|
44
|
+
type Current<F extends Fields, PR extends Fields, O, Q> = Shown<F, PR, Q> & {
|
|
45
|
+
action: Messages<O>;
|
|
46
|
+
};
|
|
47
|
+
type Declared<Sub> = ReadonlyArray<Sub | false | null | undefined>;
|
|
48
|
+
type Known<R> = unknown extends R ? never : R;
|
|
49
|
+
type HeadEntries = ReadonlyArray<View.HeadEntry<Html.Elements>>;
|
|
50
|
+
type Checked<O> = {
|
|
51
|
+
[K in keyof O]: K extends UiSchema.TaggedEnum.ReservedTag ? "This action name is reserved by Effect UI" : O[K] extends AnyOperation ? [CommandOf<Pick<O, K>>] extends [Command.Command<Message<O> | Component.Noop, unknown>] ? O[K] : "A command must resolve to one of this controller's messages" : "Build each operation with on, set or reset";
|
|
52
|
+
};
|
|
53
|
+
type PropNames<P> = {
|
|
54
|
+
[K in keyof P]: K extends "key" | "children" | "fallback" | "slots" | "view" | "keepAlive" ? "This prop name is taken by JSX" : K extends `on${infer Rest}` ? Rest extends "" | Uncapitalize<Rest> ? P[K] : Rest extends Capitalize<Rest> ? "A prop cannot be named like an event handler" : P[K] : P[K];
|
|
55
|
+
};
|
|
56
|
+
type Unreserved<E> = {
|
|
57
|
+
[K in keyof E]: K extends "failure" | "Failure" ? "A tag already uses onFailure for a failed child" : K extends UiSchema.TaggedEnum.ReservedTag ? "This event name is reserved by Effect UI" : E[K];
|
|
58
|
+
};
|
|
59
|
+
/** A model together with the messages that change it, and what the feature takes and reports. */
|
|
60
|
+
export interface Controller<F extends Fields, PR extends Fields, E extends Cases, O, Slot extends string = never, SR = never, Q = {}, Parts = {}, Rd = {}> {
|
|
61
|
+
model: Model<F>;
|
|
62
|
+
props: PR;
|
|
63
|
+
events: E;
|
|
64
|
+
init: ((props: State<PR>) => State<F>) | undefined;
|
|
65
|
+
restore: false | ((state: State<F>, props: State<PR>) => State<F>) | undefined;
|
|
66
|
+
slots: ReadonlyArray<Slot>;
|
|
67
|
+
components: Parts;
|
|
68
|
+
reads: Rd;
|
|
69
|
+
queries: Readonly<Record<string, object>>;
|
|
70
|
+
head: ((shown: Shown<F, PR, Q>) => HeadEntries) | undefined;
|
|
71
|
+
operations: O;
|
|
72
|
+
/** The action to send when a query's result changes, by query name, if any is declared. */
|
|
73
|
+
changes: ((current: {
|
|
74
|
+
action: Messages<O>;
|
|
75
|
+
}) => Readonly<Record<string, (result: never) => unknown>>) | undefined;
|
|
76
|
+
/** The subscriptions the feature wants for its current state and props, if it declares any. */
|
|
77
|
+
subscribed: ((current: Current<F, PR, O, Q>) => Declared<Subscription.Subscription<any, SR>>) | undefined;
|
|
78
|
+
}
|
|
79
|
+
/** A controller whose actions are named. It may still declare subscriptions that send them. */
|
|
80
|
+
export interface ActionsController<F extends Fields, PR extends Fields, E extends Cases, O, Slot extends string, Q, Parts, Rd> extends Controller<F, PR, E, O, Slot, RequiredBy<Parts> | Component.SharedRequirements<Rd>, Q, Parts, Rd> {
|
|
81
|
+
/**
|
|
82
|
+
* Declares what the feature listens to while it is mounted, from its state and props: a stream
|
|
83
|
+
* made with `Subscription.make`, or a document or window event. An entry that is `false` is off.
|
|
84
|
+
* Each subscription sends the feature's own actions.
|
|
85
|
+
*/
|
|
86
|
+
/**
|
|
87
|
+
* Sends an action when a query's result changes, for a transition that has to react to it, such
|
|
88
|
+
* as moving focus once a load fails. Showing a result needs none of this: the view reads it.
|
|
89
|
+
*/
|
|
90
|
+
queryChanges(react: (current: {
|
|
91
|
+
action: Messages<O>;
|
|
92
|
+
}) => {
|
|
93
|
+
readonly [Name in keyof Q]?: (result: QueryResults<Q>[Name]) => Message<O>;
|
|
94
|
+
}): ActionsController<F, PR, E, O, Slot, Q, Parts, Rd>;
|
|
95
|
+
subscriptions<Sub extends Subscription.Subscription<Message<O>, any> = never>(declare: (current: Current<F, PR, O, Q>) => Declared<Sub>): Controller<F, PR, E, O, Slot, RequiredBy<Parts> | Component.SharedRequirements<Rd> | Known<Subscription.Requirements<Sub>>, Q, Parts, Rd>;
|
|
96
|
+
}
|
|
97
|
+
export interface ControllerBuilder<F extends Fields, PR extends Fields, E extends Cases, Slot extends string = never, Q = {}, Parts = {}, Rd = {}> {
|
|
98
|
+
/** Declares what the parent passes in: an Effect schema for each prop. */
|
|
99
|
+
props<const P extends Fields>(props: P & PropNames<P>): ControllerBuilder<F, P, E, Slot, Q, Parts, Rd>;
|
|
100
|
+
/** Declares what the feature reports to its parent: a payload for each event. */
|
|
101
|
+
events<const Ev extends Cases>(events: Ev & Unreserved<Ev>): ControllerBuilder<F, PR, Ev, Slot, Q, Parts, Rd>;
|
|
102
|
+
/** Computes the initial state from the props the feature mounts with. */
|
|
103
|
+
init(init: (props: State<PR>) => State<F>): ControllerBuilder<F, PR, E, Slot, Q, Parts, Rd>;
|
|
104
|
+
/**
|
|
105
|
+
* Says what the router saves of this feature's state when the user leaves a page, to bring back
|
|
106
|
+
* on Back and Forward. By default it saves all of it. Return the state without what must not
|
|
107
|
+
* come back, such as a password or a flag for a request that is no longer running, or pass
|
|
108
|
+
* `false` to start over each time.
|
|
109
|
+
*/
|
|
110
|
+
restore(save: false | ((state: State<F>, props: State<PR>) => State<F>)): ControllerBuilder<F, PR, E, Slot, Q, Parts, Rd>;
|
|
111
|
+
/**
|
|
112
|
+
* Names the slots a parent may fill with its own markup. The view places each one where it
|
|
113
|
+
* belongs. A slot named `children` is filled by the children of the feature's tag.
|
|
114
|
+
*/
|
|
115
|
+
slots<const Name extends string>(...names: ReadonlyArray<Name>): ControllerBuilder<F, PR, E, Name, Q, Parts, Rd>;
|
|
116
|
+
/**
|
|
117
|
+
* Declares the data the feature shows, by name. Effect UI fetches each query with the args its
|
|
118
|
+
* entry returns, keeps the latest result beside the model, and renders again when it changes.
|
|
119
|
+
* The view reads the results as `queries`.
|
|
120
|
+
*/
|
|
121
|
+
queries<Next extends Queries>(queries: QueryDeclarations<F, PR, Next, Q>): ControllerBuilder<F, PR, E, Slot, Q & Single<Next>, Parts, Rd>;
|
|
122
|
+
/**
|
|
123
|
+
* Declares queries read once for each of several args, such as one user per id in a list. The
|
|
124
|
+
* view reads each as a list of entries, one per args, each with its own result.
|
|
125
|
+
*/
|
|
126
|
+
keyedQueries<Next extends Queries>(queries: KeyedDeclarations<F, PR, Next, Q>): ControllerBuilder<F, PR, E, Slot, Q & Keyed<Next>, Parts, Rd>;
|
|
127
|
+
/**
|
|
128
|
+
* Declares components the feature embeds while leaving their Layers to whoever mounts it. What
|
|
129
|
+
* they require becomes a requirement of this feature, and the view receives them as
|
|
130
|
+
* `components`, ready to use as tags. Use it to provide a Layer once, where the feature is
|
|
131
|
+
* mounted, instead of on each child.
|
|
132
|
+
*/
|
|
133
|
+
components<const Next extends Components>(components: Next): ControllerBuilder<F, PR, E, Slot, Q, Next, Rd>;
|
|
134
|
+
/**
|
|
135
|
+
* Declares the shared state the feature reads, by name. An owner is a component that shares its
|
|
136
|
+
* state with everything inside it, or an atom shared with `UiAtom.share`. The view reads each
|
|
137
|
+
* value as `reads.name`, and renders again when it changes.
|
|
138
|
+
*/
|
|
139
|
+
reads<const Next extends Reads>(reads: Next): ControllerBuilder<F, PR, E, Slot, Q, Parts, Next>;
|
|
140
|
+
/**
|
|
141
|
+
* Declares what the feature puts in the document head, such as the title, from its state, props
|
|
142
|
+
* and query results.
|
|
143
|
+
*/
|
|
144
|
+
head(head: (shown: Shown<F, PR, Q>) => HeadEntries): ControllerBuilder<F, PR, E, Slot, Q, Parts, Rd>;
|
|
145
|
+
/**
|
|
146
|
+
* Names each message and says how it changes the model, which finishes the controller. Every key
|
|
147
|
+
* of the returned object becomes a message tag and an action of the same name in the view.
|
|
148
|
+
*/
|
|
149
|
+
actions<O>(configure: (helpers: Helpers<F, PR, E, Q>) => O & Checked<NoInfer<O>>): ActionsController<F, PR, E, O, Slot, Q, Parts, Rd>;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Starts a feature's controller from its model. Declare `.props()`, `.events()`, `.slots()`,
|
|
153
|
+
* `.queries()`, `.head()` and `.init()` as needed, then finish with `.actions()`.
|
|
154
|
+
* `.subscriptions()` may follow it.
|
|
155
|
+
*/
|
|
156
|
+
export declare function controller<F extends Fields>(model: Model<F>): ControllerBuilder<F, {}, {}>;
|
|
157
|
+
export {};
|