@ilha/model 0.1.1 → 0.2.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 +27 -0
- package/README.md +91 -35
- package/dist/component-BlwL7V1h.d.ts +221 -0
- package/dist/index.d.ts +31 -6
- package/dist/index.js +252 -152
- package/dist/internal-DsyiAF5H.js +58 -0
- package/dist/rolldown-runtime-CNh9ExCG.js +17 -0
- package/dist/test.d.ts +29 -53
- package/dist/test.js +52 -51
- package/package.json +6 -6
- package/dist/app.d.ts +0 -125
- package/dist/internal-kl_qCyzu.js +0 -45
- package/dist/internal.d.ts +0 -36
- package/dist/log.d.ts +0 -33
- package/dist/submodel.d.ts +0 -28
package/LICENSE
CHANGED
|
@@ -19,3 +19,30 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
19
19
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
20
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
21
|
THE SOFTWARE.
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
Portions of this software are adapted from Effect UI
|
|
26
|
+
(https://github.com/Effect-TS/ui), distributed under the following license:
|
|
27
|
+
|
|
28
|
+
MIT License
|
|
29
|
+
|
|
30
|
+
Copyright (c) 2023 Effectful Technologies Inc
|
|
31
|
+
|
|
32
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
33
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
34
|
+
in the Software without restriction, including without limitation the rights
|
|
35
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
36
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
37
|
+
furnished to do so, subject to the following conditions:
|
|
38
|
+
|
|
39
|
+
The above copyright notice and this permission notice shall be included in all
|
|
40
|
+
copies or substantial portions of the Software.
|
|
41
|
+
|
|
42
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
43
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
44
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
45
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
46
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
47
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
48
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
# `@ilha/model`
|
|
2
2
|
|
|
3
|
-
Elm-style
|
|
4
|
-
|
|
5
|
-
---
|
|
3
|
+
Elm-style model components for [Ilha](https://github.com/ilhajs/ilha) islands. Each component instance owns one model atom; every change flows through a single `update` function. Side effects run as Commands, ongoing work as Subscriptions.
|
|
6
4
|
|
|
7
5
|
## Installation
|
|
8
6
|
|
|
@@ -10,50 +8,108 @@ Elm-style Model, `update`, commands, and subscriptions for [Ilha](https://github
|
|
|
10
8
|
bun add @ilha/model
|
|
11
9
|
```
|
|
12
10
|
|
|
13
|
-
|
|
11
|
+
`ilha` and `effect` are peer dependencies.
|
|
14
12
|
|
|
15
13
|
## Usage
|
|
16
14
|
|
|
17
15
|
```tsx
|
|
18
|
-
import {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
16
|
+
import {
|
|
17
|
+
Command,
|
|
18
|
+
Subscription,
|
|
19
|
+
defineComponent,
|
|
20
|
+
attachModelRecorder,
|
|
21
|
+
} from "@ilha/model";
|
|
22
|
+
import type {
|
|
23
|
+
DispatchRecord,
|
|
24
|
+
ModelRecorder,
|
|
25
|
+
ModelComponent,
|
|
26
|
+
Step,
|
|
27
|
+
Issued,
|
|
28
|
+
} from "@ilha/model";
|
|
29
|
+
import { Story } from "@ilha/model/test";
|
|
30
|
+
import { Effect, Layer, Schema, Stream } from "effect";
|
|
31
|
+
import { mount } from "ilha";
|
|
32
|
+
|
|
33
|
+
const Msg = Schema.TaggedUnion({
|
|
34
|
+
ClickedAdd: { amount: Schema.Number },
|
|
35
|
+
ClickedReset: {},
|
|
36
|
+
CompletedReset: {},
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const DelayReset = Command.make("DelayReset", { mode: "switch" }, () =>
|
|
40
|
+
Effect.sleep("1 second").pipe(Effect.as(Msg.cases.CompletedReset.make({})))
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
export const Counter = defineComponent({
|
|
44
|
+
name: "Counter",
|
|
45
|
+
props: Schema.Struct({ label: Schema.String }), // optional
|
|
46
|
+
model: Schema.Struct({ count: Schema.Number }), // also the SSR snapshot codec
|
|
47
|
+
message: Msg,
|
|
48
|
+
events: Schema.TaggedUnion({ Reached: { count: Schema.Number } }), // optional
|
|
49
|
+
}).make({
|
|
50
|
+
init: ($, props) => ({ count: 0 }), // Model | Step
|
|
51
|
+
update: {
|
|
52
|
+
// exhaustive record, one handler per tag
|
|
53
|
+
ClickedAdd: ($, { amount }) => $.evolve({ count: (c) => c + amount }),
|
|
54
|
+
ClickedReset: ($) => $.run(DelayReset()),
|
|
55
|
+
CompletedReset: ($) =>
|
|
56
|
+
$.set({ count: 0 }).emit({ _tag: "Reached", count: 0 }),
|
|
57
|
+
},
|
|
58
|
+
view: (h, model) => (
|
|
40
59
|
<div>
|
|
41
|
-
<
|
|
42
|
-
<
|
|
43
|
-
|
|
60
|
+
<span>{h.props.label}</span>
|
|
61
|
+
<output>{model.count}</output>
|
|
62
|
+
<button onclick={h.send(Msg.cases.ClickedAdd.make({ amount: 1 }))}>
|
|
63
|
+
+1
|
|
44
64
|
</button>
|
|
45
|
-
<button
|
|
46
|
-
|
|
65
|
+
<button
|
|
66
|
+
disabled={h.isRunning(DelayReset)}
|
|
67
|
+
onclick={h.send(Msg.cases.ClickedReset.make({}))}
|
|
68
|
+
>
|
|
69
|
+
Reset
|
|
47
70
|
</button>
|
|
48
71
|
</div>
|
|
49
72
|
),
|
|
73
|
+
subscriptions: (model, props) => [], // optional
|
|
74
|
+
layer: Layer.empty, // optional
|
|
50
75
|
});
|
|
51
76
|
|
|
52
|
-
|
|
77
|
+
// parent view: <Counter key="likes" props={{ label: "Likes" }} onEmit={h.send((e) => PageMsg.cases.Reached.make(e))} />
|
|
78
|
+
// root: mount(host, () => <Counter props={{ label: "Likes" }} />) — no root helper
|
|
53
79
|
```
|
|
54
80
|
|
|
55
|
-
|
|
81
|
+
Notes:
|
|
82
|
+
|
|
83
|
+
- `$` in `init`/`update` has `props`, `set(model)`, `evolve(patch)` (each field a value or `(prev) => next`), `run(...issued)`, `emit(event)`; `update`'s `$` also has `model`. Handlers return a bare `Model` or a `Step`.
|
|
84
|
+
- `h` in `view` is Ilha's `h` plus `props`, `send(message | (arg) => message | undefined)`, and reactive `isRunning(command)`.
|
|
85
|
+
- `Command.make(name, body)` issues concurrently; `{ mode: "switch" }` interrupts the instance's running issue of the same command. Nothing runs during SSR; init commands run on the client after mount/hydration.
|
|
86
|
+
- `Subscription.make(name, stream)` or `make(name, deps, (deps) => stream)`; deps compared with `Equal.equals`, restarted on change, stopped when removed.
|
|
87
|
+
- `layer` is built once per component per mounted root; instances of that component share its services.
|
|
88
|
+
- Throws in `init`/`view` go through Ilha's normal error path (`ErrorBoundary` / mount `onError`); `update` throws and Command/Subscription defects are logged and the instance keeps its last model.
|
|
89
|
+
|
|
90
|
+
## Testing with Story
|
|
91
|
+
|
|
92
|
+
`Story` runs the loop synchronously with no Effect and no DOM: `init` runs (commands are recorded, never executed), `send` feeds messages through `update`, `emitted` collects events, `commands` lists pending `{ name, args }` issues, and `resolve(command, message?)` drops the oldest pending issue and optionally feeds a message back.
|
|
93
|
+
|
|
94
|
+
```tsx
|
|
95
|
+
const story = Story.make(Counter, { label: "Likes" });
|
|
96
|
+
story.send(Msg.cases.ClickedAdd.make({ amount: 2 }));
|
|
97
|
+
expect(story.model).toEqual({ count: 2 });
|
|
98
|
+
expect(story.commands).toEqual([{ name: "DelayReset", args: undefined }]);
|
|
99
|
+
story.resolve(DelayReset, Msg.cases.CompletedReset.make({}));
|
|
100
|
+
expect(story.model).toEqual({ count: 0 });
|
|
101
|
+
```
|
|
56
102
|
|
|
57
|
-
|
|
103
|
+
## Migration from 0.1
|
|
58
104
|
|
|
59
|
-
|
|
105
|
+
| 0.1 | 0.2.0 |
|
|
106
|
+
| --- | --- |
|
|
107
|
+
| `defineApp({ init, update, view })` + `app.component` | `defineComponent({ name, model, message, props?, events? }).make({ init, update, view })` |
|
|
108
|
+
| `update(model, message)` switch | `update` record, one handler per tag: `ClickedAdd: ($, { amount }) => ...` |
|
|
109
|
+
| `{ model, cmd }` returns | `$` steps: `$.set(...)`, `$.evolve(...)`, `$.run(...)`, `$.emit(...)`; or return a bare model |
|
|
110
|
+
| `keyed(key, cmd)` | `Command.make(name, { mode: "switch" }, body)` (`"concurrent"` is the default) |
|
|
111
|
+
| `subscription({ deps, stream })` | `Subscription.make(name, deps, (deps) => stream)` |
|
|
112
|
+
| `runCommands` / `simulate` in `@ilha/model/test` | `Story.make(Component, props?)` with `send` / `resolve` |
|
|
113
|
+
| `foldChild` / `combine` submodels | child components with `props` + `onEmit` |
|
|
114
|
+
| `Component.root`, typed `View<R>` | gone: mount a closure, `mount(host, () => <Counter props={...} />)` |
|
|
115
|
+
| Query, Validation, MessageLog, `./devtools`, shares/reads, slots, keepAlive, lazy, Feature, provide, clientOnly, headless parts, Mount/Dom*/Keyboard/DomFiles/DomImages, TaggedEnum, standalone `evolve`, Scene/Play/TestHost/locators/ssr harness | gone |
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import "effect/Cause";
|
|
2
|
+
import * as Effect from "effect/Effect";
|
|
3
|
+
import * as Layer from "effect/Layer";
|
|
4
|
+
import * as Schema from "effect/Schema";
|
|
5
|
+
import * as Stream from "effect/Stream";
|
|
6
|
+
import { View, h } from "ilha";
|
|
7
|
+
import { AtomRegistry } from "effect/reactivity/AtomRegistry";
|
|
8
|
+
declare namespace command_d_exports {
|
|
9
|
+
export { Command, CommandBody, CommandMode, CommandOptions, Issued, MakeCommand, MaybeMessage, make$1 as make };
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* One-shot work a component issues from `init` or `update`, started with
|
|
13
|
+
* `$.run(...)`. Resolves with a follow-up message or with nothing. The
|
|
14
|
+
* `never` error channel forces every expected failure into a message;
|
|
15
|
+
* defects are logged and the instance keeps running.
|
|
16
|
+
*/
|
|
17
|
+
type MaybeMessage<Msg> = Msg | void;
|
|
18
|
+
/** How the instance treats a new issue while one is still running. */
|
|
19
|
+
type CommandMode = "concurrent" | "switch";
|
|
20
|
+
interface CommandOptions {
|
|
21
|
+
readonly mode?: CommandMode;
|
|
22
|
+
}
|
|
23
|
+
/** A pending issue: the command plus the args it was issued with. */
|
|
24
|
+
interface Issued<Msg = never, R = never> {
|
|
25
|
+
/** The command's name, for `isRunning`, `Story.commands`, and `Story.resolve`. */
|
|
26
|
+
readonly name: string;
|
|
27
|
+
/** The args the command was issued with (`undefined` for a zero-arg command). */
|
|
28
|
+
readonly args: unknown;
|
|
29
|
+
/** Runs the body with the issued args; the runtime delivers the message. */
|
|
30
|
+
readonly body: () => Effect.Effect<MaybeMessage<Msg>, never, R>;
|
|
31
|
+
/** How a re-issue treats the running one. */
|
|
32
|
+
readonly mode: CommandMode;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* A command definition. Call it with its args to issue it from `$.run`
|
|
36
|
+
* (zero-arg when its body takes none). Carries `.name` for `isRunning`.
|
|
37
|
+
*/
|
|
38
|
+
interface Command<Msg = never, R = never, Args = never> {
|
|
39
|
+
readonly name: string;
|
|
40
|
+
(...args: [Args] extends [never] ? [] : [args: Args]): Issued<Msg, R>;
|
|
41
|
+
}
|
|
42
|
+
/** A command body: an effect resolving to a message or nothing. */
|
|
43
|
+
type CommandBody<Msg, R, Args> = (args: Args) => Effect.Effect<MaybeMessage<Msg>, never, R>;
|
|
44
|
+
/** Overloads for {@link make}: with or without command options. */
|
|
45
|
+
interface MakeCommand {
|
|
46
|
+
<Msg, R = never, Args = never>(name: string, body: CommandBody<Msg, R, Args>): Command<Msg, R, Args>;
|
|
47
|
+
<Msg, R = never, Args = never>(name: string, options: CommandOptions, body: CommandBody<Msg, R, Args>): Command<Msg, R, Args>;
|
|
48
|
+
}
|
|
49
|
+
/** Build a command from a body; issues run concurrently unless switched. */
|
|
50
|
+
declare const make$1: MakeCommand;
|
|
51
|
+
declare namespace subscription_d_exports {
|
|
52
|
+
export { MakeSubscription, Subscription, make };
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Ongoing work whose lifetime follows slices of the model and props.
|
|
56
|
+
* Declarative: the runtime restarts the stream when `depsOf` changes
|
|
57
|
+
* (compared with `Equal.equals`), stops it when the entry leaves the
|
|
58
|
+
* list, and starts everything on the client only.
|
|
59
|
+
*/
|
|
60
|
+
interface Subscription<Model, Props, Msg, R, Deps> {
|
|
61
|
+
/** Subscription name; duplicates in one list throw at render. */
|
|
62
|
+
readonly name: string;
|
|
63
|
+
/** Values controlling the lifetime; the stream restarts when they change. */
|
|
64
|
+
readonly depsOf: (model: Model, props: Props) => Deps;
|
|
65
|
+
/** Stream of messages for the current deps. */
|
|
66
|
+
readonly run: (deps: Deps) => Stream.Stream<Msg, never, R>;
|
|
67
|
+
}
|
|
68
|
+
/** Overloads for {@link make}: constant stream or deps-driven stream. */
|
|
69
|
+
interface MakeSubscription {
|
|
70
|
+
<Msg, R = never>(name: string, stream: Stream.Stream<Msg, never, R>): Subscription<unknown, unknown, Msg, R, void>;
|
|
71
|
+
<Model, Props, Msg, R, Deps>(name: string, depsOf: (model: Model, props: Props) => Deps, run: (deps: Deps) => Stream.Stream<Msg, never, R>): Subscription<Model, Props, Msg, R, Deps>;
|
|
72
|
+
}
|
|
73
|
+
/** A subscription with constant deps: same stream for the instance's life. */
|
|
74
|
+
declare const make: MakeSubscription;
|
|
75
|
+
/**
|
|
76
|
+
* What `init` and `update` accumulate: the next model, commands to issue,
|
|
77
|
+
* and events to emit to the author's `onEmit`. Each builder returns a new
|
|
78
|
+
* step, so chains like `$.set(model).run(cmd).emit(event)` keep extending.
|
|
79
|
+
*/
|
|
80
|
+
interface Step<Model, Msg, R = never, Events = never> {
|
|
81
|
+
/** The model the step carries. */
|
|
82
|
+
readonly model: Model;
|
|
83
|
+
/**
|
|
84
|
+
* Pending issues, in order. The runner executes them; `Story` exposes
|
|
85
|
+
* them as `{ name, args }`.
|
|
86
|
+
*/
|
|
87
|
+
readonly commands: readonly Issued<Msg, R>[];
|
|
88
|
+
/** Emitted events, in order. */
|
|
89
|
+
readonly events: readonly Events[];
|
|
90
|
+
/** Replace the model, no merge. */
|
|
91
|
+
readonly set: (model: Model) => Step<Model, Msg, R, Events>;
|
|
92
|
+
/** Change some fields; each patch value replaces, or maps the previous. */
|
|
93
|
+
readonly evolve: (patch: EvolvePatch<Model>) => Step<Model, Msg, R, Events>;
|
|
94
|
+
/** Issue commands, owned by the instance. Results come back as messages. */
|
|
95
|
+
readonly run: <R2>(...issued: readonly Issued<Msg, R2>[]) => Step<Model, Msg, R | R2, Events>;
|
|
96
|
+
/** Report events to the author's `onEmit`, after the model commits. */
|
|
97
|
+
readonly emit: (...events: readonly Events[]) => Step<Model, Msg, R, Events>;
|
|
98
|
+
}
|
|
99
|
+
/** Shallow patch: each field is a value, or an updater from the previous. */
|
|
100
|
+
type EvolvePatch<Model> = { readonly [K in keyof Model]?: Model[K] | ((previous: Model[K]) => Model[K]) | undefined; };
|
|
101
|
+
/** `$` in `init`: the current props, and `set` to build the first model. */
|
|
102
|
+
interface InitDollar<Model, Msg, Props, Events> {
|
|
103
|
+
readonly props: Props;
|
|
104
|
+
readonly set: (model: Model) => Step<Model, Msg, never, Events>;
|
|
105
|
+
}
|
|
106
|
+
/** `$` in `update`: a step seeded with the current model, plus props. */
|
|
107
|
+
interface UpdateDollar<Model, Msg, Props, Events> extends Step<Model, Msg, never, Events> {
|
|
108
|
+
readonly props: Props;
|
|
109
|
+
}
|
|
110
|
+
/** Exhaustive record: one handler per message tag. */
|
|
111
|
+
type UpdateHandlers<Model, Msg extends {
|
|
112
|
+
readonly _tag: string;
|
|
113
|
+
}, Props, Events> = { readonly [Tag in Msg["_tag"]]: (dollar: UpdateDollar<Model, Msg, Props, Events>, message: Extract<Msg, {
|
|
114
|
+
readonly _tag: Tag;
|
|
115
|
+
}>) => Model | Step<Model, Msg, any, Events>; };
|
|
116
|
+
/** `h` in `view`: Ilha's `h`, plus the component's props and helpers. */
|
|
117
|
+
type Helper<Props, Msg> = typeof h & {
|
|
118
|
+
readonly props: Props;
|
|
119
|
+
readonly send: {
|
|
120
|
+
/** Send a fixed message; the argument is ignored. */
|
|
121
|
+
(message: Msg): () => void;
|
|
122
|
+
/** Map an argument to a message; `undefined` sends nothing. */
|
|
123
|
+
<A>(fromArg: (arg: A) => Msg | undefined): (arg?: A) => void;
|
|
124
|
+
};
|
|
125
|
+
/** Whether the command has a running issue in this instance. Reactive. */
|
|
126
|
+
readonly isRunning: (command: {
|
|
127
|
+
readonly name: string;
|
|
128
|
+
}) => boolean;
|
|
129
|
+
};
|
|
130
|
+
interface CallOptions<Props, Events> {
|
|
131
|
+
readonly props: Props;
|
|
132
|
+
readonly onEmit?: ((event: Events) => void) | undefined;
|
|
133
|
+
readonly key?: string | number | undefined;
|
|
134
|
+
}
|
|
135
|
+
type RenderOptions<Props, Events> = Record<never, never> extends Props ? {
|
|
136
|
+
readonly props?: Props | undefined;
|
|
137
|
+
readonly onEmit?: ((event: Events) => void) | undefined;
|
|
138
|
+
readonly key?: string | number | undefined;
|
|
139
|
+
} : CallOptions<Props, Events>;
|
|
140
|
+
/**
|
|
141
|
+
* A component built by `defineComponent(...).make(...)`: an ordinary Ilha
|
|
142
|
+
* typed-props component taking `{ props, onEmit? }` (`props` required iff
|
|
143
|
+
* a props schema was given; `key` is Ilha's). Each mounted instance owns
|
|
144
|
+
* its own model atom; a child re-renders without re-running its parent.
|
|
145
|
+
* The definition and impl travel on the component for `Story`.
|
|
146
|
+
*/
|
|
147
|
+
interface ModelComponent<Props = Record<never, never>, Events = never, Model = unknown, Msg = unknown> {
|
|
148
|
+
(options: RenderOptions<Props, Events>): View;
|
|
149
|
+
/** Definition and impl travel on the component for `Story`. */
|
|
150
|
+
readonly definition: Definition<Props, Model, Msg, Events>;
|
|
151
|
+
readonly impl: unknown;
|
|
152
|
+
}
|
|
153
|
+
interface Definition<Props, Model, Msg, Events> {
|
|
154
|
+
readonly name: string;
|
|
155
|
+
readonly props: Schema.ConstraintDecoder<Props>;
|
|
156
|
+
readonly model: Schema.ConstraintCodec<Model, unknown>;
|
|
157
|
+
readonly message: Schema.Schema<Msg>;
|
|
158
|
+
readonly events: Schema.Schema<Events>;
|
|
159
|
+
}
|
|
160
|
+
interface MakeImpl<Model, Msg extends {
|
|
161
|
+
readonly _tag: string;
|
|
162
|
+
}, Props, Events> {
|
|
163
|
+
readonly init: (dollar: InitDollar<Model, Msg, Props, Events>, props: Props) => Model | Step<Model, Msg, any, Events>;
|
|
164
|
+
readonly update: UpdateHandlers<Model, Msg, Props, Events>;
|
|
165
|
+
readonly view: (h: Helper<Props, Msg>, model: Model) => View;
|
|
166
|
+
readonly subscriptions?: ((model: Model, props: Props) => readonly Subscription<Model, Props, Msg, any, any>[]) | undefined;
|
|
167
|
+
}
|
|
168
|
+
/** Services an `init` or `update` return still requires. */
|
|
169
|
+
type StepServices<S> = S extends {
|
|
170
|
+
readonly commands: readonly Issued<unknown, infer R>[];
|
|
171
|
+
} ? R : never;
|
|
172
|
+
type UpdateServices<U> = U extends Record<string, (dollar: never, message: never) => infer Ret> ? StepServices<Ret> : never;
|
|
173
|
+
type InitServices<F> = F extends ((dollar: never, props: never) => infer Ret) ? StepServices<Ret> : never;
|
|
174
|
+
type SubServices<S> = S extends ((model: never, props: never) => readonly (infer E)[]) ? E extends {
|
|
175
|
+
readonly run: (deps: never) => Stream.Stream<unknown, never, infer R>;
|
|
176
|
+
} ? R : never : never;
|
|
177
|
+
/** Every service the component's commands and subscriptions still require. */
|
|
178
|
+
type ImplNeeds<I> = I extends {
|
|
179
|
+
readonly update: infer U;
|
|
180
|
+
readonly init: infer F;
|
|
181
|
+
} ? UpdateServices<U> | InitServices<F> | (I extends {
|
|
182
|
+
readonly subscriptions: infer S;
|
|
183
|
+
} ? SubServices<Exclude<S, undefined>> : never) : never;
|
|
184
|
+
/**
|
|
185
|
+
* Reject commands and subscriptions that need services outside the layer.
|
|
186
|
+
* The runtime always provides `AtomRegistry`; anything else must come
|
|
187
|
+
* from `layer`.
|
|
188
|
+
*/
|
|
189
|
+
type CheckServices<Needs, L extends Layer.Any> = [Needs] extends [Layer.Success<L> | AtomRegistry] ? unknown : {
|
|
190
|
+
"services missing from layer": Exclude<Needs, Layer.Success<L> | AtomRegistry>;
|
|
191
|
+
};
|
|
192
|
+
/**
|
|
193
|
+
* What `defineComponent` returns: `make(impl)` builds the component.
|
|
194
|
+
* `layer` defaults to empty; `CheckServices` proves the layer covers
|
|
195
|
+
* every service `init`, Commands, and Subscriptions need.
|
|
196
|
+
*/
|
|
197
|
+
interface ComponentMaker<Model, Msg extends {
|
|
198
|
+
readonly _tag: string;
|
|
199
|
+
}, Props, Events> {
|
|
200
|
+
make: <L extends Layer.Any = Layer.Layer<never, never>, I extends MakeImpl<Model, Msg, Props, Events> = MakeImpl<Model, Msg, Props, Events>>(impl: MakeImpl<Model, Msg, Props, Events> & I & {
|
|
201
|
+
readonly layer?: L | undefined;
|
|
202
|
+
} & CheckServices<ImplNeeds<I>, L>) => ModelComponent<Props, Events, Model, Msg>;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Define a component. `model` is also the SSR snapshot codec; `message`
|
|
206
|
+
* is usually a `Schema.TaggedUnion`. Returns `{ make(impl) }`; `make`
|
|
207
|
+
* returns a `ModelComponent` usable as an Ilha component or JSX tag.
|
|
208
|
+
*/
|
|
209
|
+
declare const defineComponent: <const Name extends string, Props, Model, Msg extends {
|
|
210
|
+
readonly _tag: string;
|
|
211
|
+
}, Events = never>(options: {
|
|
212
|
+
readonly name: Name;
|
|
213
|
+
readonly model: Schema.ConstraintCodec<Model, unknown>;
|
|
214
|
+
readonly message: Schema.Schema<Msg>;
|
|
215
|
+
readonly events?: Schema.Schema<Events> | undefined;
|
|
216
|
+
} & (Record<never, never> extends Props ? {
|
|
217
|
+
readonly props?: Schema.ConstraintDecoder<Props> | undefined;
|
|
218
|
+
} : {
|
|
219
|
+
readonly props: Schema.ConstraintDecoder<Props>;
|
|
220
|
+
})) => ComponentMaker<Model, Msg, Props, Events>;
|
|
221
|
+
export { Issued as a, subscription_d_exports as i, defineComponent as n, command_d_exports as o, Step as r, ModelComponent as t };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { a as Issued, i as subscription_d_exports, n as defineComponent, o as command_d_exports, r as Step, t as ModelComponent } from "./component-BlwL7V1h.js";
|
|
2
|
+
/**
|
|
3
|
+
* One dispatch as an attached recorder sees it: raw values, nothing copied
|
|
4
|
+
* or summarized. Summarizing, diffing, and buffering belong to the
|
|
5
|
+
* recorder, so apps never ship that code.
|
|
6
|
+
*/
|
|
7
|
+
interface DispatchRecord {
|
|
8
|
+
/**
|
|
9
|
+
* The island instance's model atom. Join it against `atomsOf(id)` from
|
|
10
|
+
* `ilha/debug` to find which island dispatched.
|
|
11
|
+
*/
|
|
12
|
+
readonly instance: object;
|
|
13
|
+
/** The dispatched message. */
|
|
14
|
+
readonly message: unknown;
|
|
15
|
+
/** The model `update` received. */
|
|
16
|
+
readonly before: unknown;
|
|
17
|
+
/** The model `update` returned. */
|
|
18
|
+
readonly after: unknown;
|
|
19
|
+
/** How many commands the message started. */
|
|
20
|
+
readonly commands: number;
|
|
21
|
+
}
|
|
22
|
+
/** Receives every dispatch while attached. */
|
|
23
|
+
type ModelRecorder = (record: DispatchRecord) => void;
|
|
24
|
+
/**
|
|
25
|
+
* Hand every `dispatch` of every model island to `recorder` until the
|
|
26
|
+
* returned detach runs. The devtools page script attaches one for you.
|
|
27
|
+
* Detached, `dispatch` pays one size check: no record, no allocation. A
|
|
28
|
+
* recorder that throws is logged and never breaks `dispatch`.
|
|
29
|
+
*/
|
|
30
|
+
export declare const attachModelRecorder: (recorder: ModelRecorder) => (() => void);
|
|
31
|
+
export { command_d_exports as Command, type DispatchRecord, type Issued, type ModelComponent, type ModelRecorder, type Step, subscription_d_exports as Subscription, defineComponent };
|