@ilha/model 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 +56 -2
- package/dist/app.d.ts +126 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +177 -0
- package/dist/submodel.d.ts +28 -0
- package/package.json +55 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ryuz
|
|
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
|
|
13
|
+
all 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
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,57 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@ilha/model`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Elm-style Model, `update`, commands, and subscriptions for [Ilha](https://github.com/ilhajs/ilha) islands. One state tree per island, every change through a single `update` function.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
bun add @ilha/model
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```tsx
|
|
18
|
+
import { defineApp } from "@ilha/model";
|
|
19
|
+
import type { Dispatch } from "@ilha/model";
|
|
20
|
+
import { Data } from "effect";
|
|
21
|
+
|
|
22
|
+
type Message = Data.TaggedEnum<{
|
|
23
|
+
Decrement: {};
|
|
24
|
+
Increment: {};
|
|
25
|
+
}>;
|
|
26
|
+
const Message = Data.taggedEnum<Message>();
|
|
27
|
+
|
|
28
|
+
interface Model {
|
|
29
|
+
readonly count: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const app = defineApp({
|
|
33
|
+
init: () => ({ model: { count: 0 } }),
|
|
34
|
+
update: (model: Model, message: Message) =>
|
|
35
|
+
Message.$match(message, {
|
|
36
|
+
Decrement: () => ({ model: { count: model.count - 1 } }),
|
|
37
|
+
Increment: () => ({ model: { count: model.count + 1 } }),
|
|
38
|
+
}),
|
|
39
|
+
view: (model: Model, dispatch: Dispatch<Message>) => (
|
|
40
|
+
<div>
|
|
41
|
+
<p>{model.count}</p>
|
|
42
|
+
<button type="button" onclick={() => dispatch(Message.Decrement())}>
|
|
43
|
+
-
|
|
44
|
+
</button>
|
|
45
|
+
<button type="button" onclick={() => dispatch(Message.Increment())}>
|
|
46
|
+
+
|
|
47
|
+
</button>
|
|
48
|
+
</div>
|
|
49
|
+
),
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
export const Counter = app.component;
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Mount it like any Ilha component: `mount(host, Counter)` or `await renderToString(Counter)`.
|
|
56
|
+
|
|
57
|
+
See [Model](https://ilha.build/guide/model) for commands, subscriptions, submodels, and testing.
|
package/dist/app.d.ts
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import * as Effect from "effect/Effect";
|
|
2
|
+
import * as Layer from "effect/Layer";
|
|
3
|
+
import type { AtomRegistry } from "effect/reactivity/AtomRegistry";
|
|
4
|
+
import type * as Schema from "effect/Schema";
|
|
5
|
+
import * as Stream from "effect/Stream";
|
|
6
|
+
import type { Component, View } from "ilha";
|
|
7
|
+
/** Send a message into the app loop. */
|
|
8
|
+
export type Dispatch<Message> = (message: Message) => void;
|
|
9
|
+
/**
|
|
10
|
+
* One-shot work returned from `init` or `update`. Resolve with a follow-up
|
|
11
|
+
* message or with nothing. The `never` error channel forces every expected
|
|
12
|
+
* failure into a message; defects are logged and the island keeps running.
|
|
13
|
+
*/
|
|
14
|
+
export type Cmd<Message, R = never> = Effect.Effect<Message | void, never, R | AtomRegistry>;
|
|
15
|
+
/**
|
|
16
|
+
* What `init` and `update` return: the next model plus optional one-shot
|
|
17
|
+
* work. Several commands run concurrently with each other.
|
|
18
|
+
*/
|
|
19
|
+
export interface UpdateResult<Model, Message, R = never> {
|
|
20
|
+
readonly model: Model;
|
|
21
|
+
readonly cmd?: Cmd<Message, R> | readonly Cmd<Message, R>[] | undefined;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Ongoing work whose lifetime follows `deps(model)`. Build entries with
|
|
25
|
+
* {@link subscription} so each one keeps its own `Deps` type.
|
|
26
|
+
*/
|
|
27
|
+
export interface Subscription<Model, Message, Deps, R = never> {
|
|
28
|
+
/** Values controlling the lifetime; the stream restarts when they change. */
|
|
29
|
+
readonly deps: (model: Model) => Deps;
|
|
30
|
+
/** Stream of messages for the current deps. Restarted when deps change. */
|
|
31
|
+
readonly stream: (deps: Deps) => Stream.Stream<Message, never, R | AtomRegistry>;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Name one subscription. Each entry infers its own `Deps` from its `deps`
|
|
35
|
+
* function, so `stream` receives the exact type its deps return. Annotate
|
|
36
|
+
* the model parameter; everything else infers.
|
|
37
|
+
*/
|
|
38
|
+
export declare const subscription: <Model, Message, Deps, R = never>(sub: Subscription<Model, Message, Deps, R>) => Subscription<Model, Message, Deps, R>;
|
|
39
|
+
/** Options for {@link defineApp}. */
|
|
40
|
+
export interface AppOptions<Model, Message, R = never, E = never> {
|
|
41
|
+
/** Component name shown in `ilha/debug` trees. Default `"ModelApp"`. */
|
|
42
|
+
readonly name?: string | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* Build the first model, plus an optional start-up command. Must be pure.
|
|
45
|
+
* Runs once per instance; the command runs once per client mount, never
|
|
46
|
+
* during server rendering.
|
|
47
|
+
*/
|
|
48
|
+
readonly init: () => UpdateResult<Model, Message, R>;
|
|
49
|
+
/** The only place state changes. Must be pure. */
|
|
50
|
+
readonly update: (model: Model, message: Message) => UpdateResult<Model, Message, R>;
|
|
51
|
+
/** Render the model. Must be pure; report facts with `dispatch`. */
|
|
52
|
+
readonly view: (model: Model, dispatch: Dispatch<Message>) => View;
|
|
53
|
+
/** Ongoing work gated by slices of the model. */
|
|
54
|
+
readonly subscriptions?: readonly Subscription<Model, Message, any, R>[] | undefined;
|
|
55
|
+
/** Services available to commands and subscriptions. */
|
|
56
|
+
readonly layer?: Layer.Layer<R, E> | undefined;
|
|
57
|
+
/**
|
|
58
|
+
* Codec for the model snapshot. Without it the model must be JSON-safe;
|
|
59
|
+
* with it, values like `Date` survive server rendering into hydration.
|
|
60
|
+
*/
|
|
61
|
+
readonly schema?: Schema.ConstraintCodec<Model, unknown> | undefined;
|
|
62
|
+
}
|
|
63
|
+
/** An Elm-style app: mount `component` like any Ilha component. */
|
|
64
|
+
export interface ModelApp<Model, Message, R = never, E = never> {
|
|
65
|
+
readonly component: Component;
|
|
66
|
+
readonly options: AppOptions<Model, Message, R, E>;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Split one or many commands. Internal: shared with `submodel`, not part of
|
|
70
|
+
* the package surface. `Effect.isEffect` narrows the union where
|
|
71
|
+
* `Array.isArray` cannot, so no assertion is needed.
|
|
72
|
+
*/
|
|
73
|
+
export declare const eachCmd: <Message, R>(cmd: Cmd<Message, R> | readonly Cmd<Message, R>[]) => readonly Cmd<Message, R>[];
|
|
74
|
+
/**
|
|
75
|
+
* Latest-wins command: starting one interrupts the running command with the
|
|
76
|
+
* same key, so only the latest result dispatches. Different keys run
|
|
77
|
+
* independently. Keys are per app instance: two mounted instances never
|
|
78
|
+
* share them.
|
|
79
|
+
*/
|
|
80
|
+
export declare const keyed: <Message, R>(key: string, cmd: Cmd<Message, R>) => Cmd<Message, R>;
|
|
81
|
+
/** Services an effect still requires. */
|
|
82
|
+
type EffectNeeds<T> = T extends Effect.Effect<unknown, unknown, infer R> ? R : never;
|
|
83
|
+
/** Services one command, or one command list, still requires. */
|
|
84
|
+
type CmdNeeds<T> = T extends readonly (infer Each)[] ? EffectNeeds<Each> : EffectNeeds<T>;
|
|
85
|
+
/** Services an `init` or `update` result still requires. */
|
|
86
|
+
type UpdateNeeds<T> = T extends {
|
|
87
|
+
cmd?: infer C;
|
|
88
|
+
} ? CmdNeeds<Exclude<C, undefined>> : never;
|
|
89
|
+
/** Services one subscription entry still requires. */
|
|
90
|
+
type SubNeeds<T> = T extends {
|
|
91
|
+
stream: (...args: never[]) => Stream.Stream<unknown, unknown, infer R>;
|
|
92
|
+
} ? R : never;
|
|
93
|
+
/** Every service the app's commands and subscriptions still require. */
|
|
94
|
+
type OptionsNeeds<O> = (O extends {
|
|
95
|
+
init: (...args: never[]) => infer Ret;
|
|
96
|
+
} ? UpdateNeeds<Ret> : never) | (O extends {
|
|
97
|
+
update: (...args: never[]) => infer Ret;
|
|
98
|
+
} ? UpdateNeeds<Ret> : never) | (O extends {
|
|
99
|
+
subscriptions?: infer S;
|
|
100
|
+
} ? S extends readonly (infer Each)[] ? SubNeeds<Each> : never : never);
|
|
101
|
+
/**
|
|
102
|
+
* Reject commands and subscriptions that need services outside the layer.
|
|
103
|
+
* The runtime always provides `AtomRegistry`; anything else must come from
|
|
104
|
+
* `layer`. Evaluated after `O` and `L` are fixed, so inference never sees
|
|
105
|
+
* the layer's services through this check.
|
|
106
|
+
*/
|
|
107
|
+
type CheckServices<O, L extends Layer.Any> = OptionsNeeds<O> extends infer Needs ? [Needs] extends [Layer.Success<L> | AtomRegistry] ? unknown : {
|
|
108
|
+
"services missing from layer": Exclude<Needs, Layer.Success<L> | AtomRegistry>;
|
|
109
|
+
} : never;
|
|
110
|
+
/**
|
|
111
|
+
* What `defineApp` infers `Model` and `Message` from. Command and stream
|
|
112
|
+
* requirements stay permissive here so inference never forces the layer
|
|
113
|
+
* type early; `CheckServices` validates coverage after, and the layer
|
|
114
|
+
* itself is captured by `{ layer?: L }`.
|
|
115
|
+
*/
|
|
116
|
+
type LooseOptions<Model, Message> = Omit<AppOptions<Model, Message, any, any>, "layer">;
|
|
117
|
+
/**
|
|
118
|
+
* Define an Elm-style app. One atom holds the whole model; `dispatch` runs
|
|
119
|
+
* `update` and rerenders; commands run concurrently inside the app runtime;
|
|
120
|
+
* each subscription restarts only when its own deps change and stops on
|
|
121
|
+
* unmount.
|
|
122
|
+
*/
|
|
123
|
+
export declare const defineApp: <Model, Message, L extends Layer.Any = Layer.Layer<never, never>, O extends LooseOptions<Model, Message> = LooseOptions<Model, Message>>(options: LooseOptions<Model, Message> & O & {
|
|
124
|
+
layer?: L | undefined;
|
|
125
|
+
} & CheckServices<O, L>) => ModelApp<Model, Message, Layer.Success<L>, Layer.Error<L>>;
|
|
126
|
+
export {};
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import * as Cause from "effect/Cause";
|
|
2
|
+
import * as Context from "effect/Context";
|
|
3
|
+
import * as Effect from "effect/Effect";
|
|
4
|
+
import * as Equal from "effect/Equal";
|
|
5
|
+
import * as Exit from "effect/Exit";
|
|
6
|
+
import * as Fiber from "effect/Fiber";
|
|
7
|
+
import * as FiberMap from "effect/FiberMap";
|
|
8
|
+
import * as Layer from "effect/Layer";
|
|
9
|
+
import * as Queue from "effect/Queue";
|
|
10
|
+
import * as Atom from "effect/reactivity/Atom";
|
|
11
|
+
import * as Scope from "effect/Scope";
|
|
12
|
+
import * as Stream from "effect/Stream";
|
|
13
|
+
import { atom, untrack, watch } from "ilha";
|
|
14
|
+
|
|
15
|
+
// src/app.ts
|
|
16
|
+
/**
|
|
17
|
+
* Name one subscription. Each entry infers its own `Deps` from its `deps`
|
|
18
|
+
* function, so `stream` receives the exact type its deps return. Annotate
|
|
19
|
+
* the model parameter; everything else infers.
|
|
20
|
+
*/
|
|
21
|
+
const subscription = (sub) => sub;
|
|
22
|
+
/**
|
|
23
|
+
* Split one or many commands. Internal: shared with `submodel`, not part of
|
|
24
|
+
* the package surface. `Effect.isEffect` narrows the union where
|
|
25
|
+
* `Array.isArray` cannot, so no assertion is needed.
|
|
26
|
+
*/
|
|
27
|
+
const eachCmd = (cmd) => Effect.isEffect(cmd) ? [cmd] : cmd;
|
|
28
|
+
/**
|
|
29
|
+
* Per-instance keyed-command scope. The runner provides it around every
|
|
30
|
+
* command; `keyed` reads it back without adding a service requirement.
|
|
31
|
+
*/
|
|
32
|
+
const KeyedScope = Context.Service("ilha/model/KeyedScope");
|
|
33
|
+
/**
|
|
34
|
+
* Latest-wins command: starting one interrupts the running command with the
|
|
35
|
+
* same key, so only the latest result dispatches. Different keys run
|
|
36
|
+
* independently. Keys are per app instance: two mounted instances never
|
|
37
|
+
* share them.
|
|
38
|
+
*/
|
|
39
|
+
const keyed = (key, cmd) => Effect.flatMap(Effect.serviceOption(KeyedScope), (scope) => scope._tag === "None" ? cmd : Effect.flatMap(FiberMap.run(scope.value, key, cmd), (fiber) => Fiber.join(fiber)));
|
|
40
|
+
/**
|
|
41
|
+
* Report an unexpected cause through the Effect logger. Interruption
|
|
42
|
+
* (unmount, keyed supersede, subscription restart) is silent; anything else
|
|
43
|
+
* is a defect worth logging.
|
|
44
|
+
*/
|
|
45
|
+
const reportCause = (cause) => Cause.hasInterruptsOnly(cause) ? Effect.void : Effect.logError(Cause.pretty(cause));
|
|
46
|
+
const makeInstanceCells = () => {
|
|
47
|
+
const keyedScope = Effect.runSync(Scope.make());
|
|
48
|
+
return {
|
|
49
|
+
alive: true,
|
|
50
|
+
keyedMap: Effect.runSync(Effect.provideService(FiberMap.make(), Scope.Scope, keyedScope)),
|
|
51
|
+
keyedScope,
|
|
52
|
+
queue: Effect.runSync(Queue.unbounded()),
|
|
53
|
+
started: false
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Log a defect and end the stream quietly. Interruption passes through so
|
|
58
|
+
* restarts and unmount stay silent.
|
|
59
|
+
*/
|
|
60
|
+
const quietOnDefect = (cause) => Cause.hasInterruptsOnly(cause) ? Stream.failCause(cause) : Stream.unwrap(Effect.as(Effect.logError(Cause.pretty(cause)), Stream.empty));
|
|
61
|
+
/**
|
|
62
|
+
* One subscription's restartable stream: the model feeds `deps`, equal deps
|
|
63
|
+
* keep the stream alive, changed deps restart it. A defect in the stream for
|
|
64
|
+
* the current deps is logged and silences only that stream, so the next
|
|
65
|
+
* deps change starts a fresh one. A defect in `deps` itself is logged and
|
|
66
|
+
* ends the subscription.
|
|
67
|
+
*/
|
|
68
|
+
const watchOne = (stateAtom, sub) => Atom.toStream(stateAtom).pipe(Stream.map((model) => sub.deps(model)), Stream.changesWith(Equal.equals), Stream.switchMap((deps) => Stream.suspend(() => sub.stream(deps)).pipe(Stream.catchCause(quietOnDefect))), Stream.catchCause(quietOnDefect));
|
|
69
|
+
/**
|
|
70
|
+
* Merge every subscription into one stream, built once per app instance.
|
|
71
|
+
* Each entry restarts only on its own deps; entries never disturb each
|
|
72
|
+
* other.
|
|
73
|
+
*/
|
|
74
|
+
const mergeSubscriptions = (stateAtom, subscriptions) => Stream.mergeAll(subscriptions.map((sub) => watchOne(stateAtom, sub)), { concurrency: "unbounded" });
|
|
75
|
+
/**
|
|
76
|
+
* Define an Elm-style app. One atom holds the whole model; `dispatch` runs
|
|
77
|
+
* `update` and rerenders; commands run concurrently inside the app runtime;
|
|
78
|
+
* each subscription restarts only when its own deps change and stops on
|
|
79
|
+
* unmount.
|
|
80
|
+
*/
|
|
81
|
+
const defineApp = (options) => {
|
|
82
|
+
const { init, update, view } = options;
|
|
83
|
+
const subscriptions = options.subscriptions ?? [];
|
|
84
|
+
const provided = options.layer ?? Layer.empty;
|
|
85
|
+
const runtime = Atom.runtime(provided);
|
|
86
|
+
const component = () => {
|
|
87
|
+
const initResult = atom.lazy(() => init(), { snapshot: false });
|
|
88
|
+
const state = atom.lazy(() => untrack(() => initResult()).model, { schema: options.schema });
|
|
89
|
+
const cells = atom.lazy(() => makeInstanceCells(), { snapshot: false });
|
|
90
|
+
const merged = atom.lazy(() => ({ stream: mergeSubscriptions(state.atom, subscriptions) }), { snapshot: false });
|
|
91
|
+
const isAlive = () => {
|
|
92
|
+
try {
|
|
93
|
+
return untrack(() => cells()).alive;
|
|
94
|
+
} catch {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
const dispatch = (message) => {
|
|
99
|
+
if (!isAlive()) return;
|
|
100
|
+
const result = update(untrack(() => state()), message);
|
|
101
|
+
state.set(result.model);
|
|
102
|
+
if (result.cmd !== void 0) {
|
|
103
|
+
const { queue } = untrack(() => cells());
|
|
104
|
+
for (const cmd of eachCmd(result.cmd)) Queue.offerUnsafe(queue, cmd);
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
const { keyedMap } = untrack(() => cells());
|
|
108
|
+
const runOne = (cmd) => Effect.provideService(Effect.catchCause(cmd.pipe(Effect.flatMap((out) => out === void 0 ? Effect.void : Effect.sync(() => dispatch(out)))), reportCause), KeyedScope, keyedMap);
|
|
109
|
+
const runner = atom(Atom.fn((queue, get) => Effect.flatMap(get.result(runtime), (services) => Effect.provideContext(Effect.forever(Effect.flatMap(Queue.take(queue), (cmd) => Effect.forkChild(runOne(cmd)))), services))));
|
|
110
|
+
const runnerFailure = atom(runner.atom.pipe(Atom.map((result) => result._tag === "Failure" && !Cause.hasInterruptsOnly(result.cause) ? Cause.pretty(result.cause) : "")));
|
|
111
|
+
runner();
|
|
112
|
+
watch(runnerFailure, (failure) => {
|
|
113
|
+
if (failure !== "") Effect.runSync(Effect.logError(failure));
|
|
114
|
+
});
|
|
115
|
+
watch.once(({ ssr }) => {
|
|
116
|
+
const helpers = untrack(() => cells());
|
|
117
|
+
if (!ssr && !helpers.started) {
|
|
118
|
+
helpers.started = true;
|
|
119
|
+
const begin = untrack(() => initResult()).cmd;
|
|
120
|
+
if (begin !== void 0) for (const cmd of eachCmd(begin)) Queue.offerUnsafe(helpers.queue, cmd);
|
|
121
|
+
if (subscriptions.length > 0) Queue.offerUnsafe(helpers.queue, Stream.runForEach(untrack(() => merged()).stream, (message) => Effect.catchCause(Effect.sync(() => dispatch(message)), reportCause)));
|
|
122
|
+
runner.set(helpers.queue);
|
|
123
|
+
}
|
|
124
|
+
return () => {
|
|
125
|
+
helpers.alive = false;
|
|
126
|
+
Effect.runFork(Scope.close(helpers.keyedScope, Exit.void));
|
|
127
|
+
};
|
|
128
|
+
});
|
|
129
|
+
return view(state(), dispatch);
|
|
130
|
+
};
|
|
131
|
+
Object.defineProperty(component, "name", { value: options.name ?? "ModelApp" });
|
|
132
|
+
return {
|
|
133
|
+
component,
|
|
134
|
+
options
|
|
135
|
+
};
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
// src/submodel.ts
|
|
139
|
+
/**
|
|
140
|
+
* Run steps in order, threading each model into the next. The steps run in
|
|
141
|
+
* order, but the commands from all steps run concurrently, like any command
|
|
142
|
+
* list.
|
|
143
|
+
*/
|
|
144
|
+
const combine = (model, steps) => {
|
|
145
|
+
let current = model;
|
|
146
|
+
const cmds = [];
|
|
147
|
+
for (const step of steps) {
|
|
148
|
+
const result = step(current);
|
|
149
|
+
current = result.model;
|
|
150
|
+
if (result.cmd !== void 0) cmds.push(...eachCmd(result.cmd));
|
|
151
|
+
}
|
|
152
|
+
if (cmds.length === 0) return { model: current };
|
|
153
|
+
return {
|
|
154
|
+
cmd: cmds.length === 1 ? cmds[0] : cmds,
|
|
155
|
+
model: current
|
|
156
|
+
};
|
|
157
|
+
};
|
|
158
|
+
/**
|
|
159
|
+
* Embed a child update in a parent. Runs the child, writes its model back,
|
|
160
|
+
* and lifts its commands so results arrive wrapped for the parent.
|
|
161
|
+
*/
|
|
162
|
+
const foldChild = (options) => {
|
|
163
|
+
const { read, toParentMessage, update, write } = options;
|
|
164
|
+
return (model, message) => {
|
|
165
|
+
const child = read(model);
|
|
166
|
+
if (child === void 0) return { model };
|
|
167
|
+
const result = update(child, message);
|
|
168
|
+
const next = write(model, result.model);
|
|
169
|
+
if (result.cmd === void 0) return { model: next };
|
|
170
|
+
return {
|
|
171
|
+
cmd: eachCmd(result.cmd).map((cmd) => cmd.pipe(Effect.map((out) => out === void 0 ? void 0 : toParentMessage(out)))),
|
|
172
|
+
model: next
|
|
173
|
+
};
|
|
174
|
+
};
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
export { combine, defineApp, foldChild, keyed, subscription };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { UpdateResult } from "./app";
|
|
2
|
+
/**
|
|
3
|
+
* One transform over a model, used with {@link combine}. A child fold
|
|
4
|
+
* returns a step; inline lambdas receive the model the previous step wrote.
|
|
5
|
+
*/
|
|
6
|
+
export type Step<Model, Message, R = never> = (model: Model) => UpdateResult<Model, Message, R>;
|
|
7
|
+
/**
|
|
8
|
+
* Run steps in order, threading each model into the next. The steps run in
|
|
9
|
+
* order, but the commands from all steps run concurrently, like any command
|
|
10
|
+
* list.
|
|
11
|
+
*/
|
|
12
|
+
export declare const combine: <Model, Message, R = never>(model: Model, steps: readonly Step<Model, Message, R>[]) => UpdateResult<Model, Message, R>;
|
|
13
|
+
/** Options for {@link foldChild}. */
|
|
14
|
+
export interface FoldChildOptions<ParentModel, ParentMessage, ChildModel, ChildMessage, R = never> {
|
|
15
|
+
/** The child's own update. The parent never touches the child model. */
|
|
16
|
+
readonly update: (model: ChildModel, message: ChildMessage) => UpdateResult<ChildModel, ChildMessage, R>;
|
|
17
|
+
/** Read the child slice. `undefined` leaves the parent unchanged. */
|
|
18
|
+
readonly read: (model: ParentModel) => ChildModel | undefined;
|
|
19
|
+
/** Write the child slice back. */
|
|
20
|
+
readonly write: (model: ParentModel, child: ChildModel) => ParentModel;
|
|
21
|
+
/** Lift child messages, usually into a `Got*` wrapper. */
|
|
22
|
+
readonly toParentMessage: (message: ChildMessage) => ParentMessage;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Embed a child update in a parent. Runs the child, writes its model back,
|
|
26
|
+
* and lifts its commands so results arrive wrapped for the parent.
|
|
27
|
+
*/
|
|
28
|
+
export declare const foldChild: <ParentModel, ParentMessage, ChildModel, ChildMessage, R = never>(options: FoldChildOptions<ParentModel, ParentMessage, ChildModel, ChildMessage, R>) => ((model: ParentModel, message: ChildMessage) => UpdateResult<ParentModel, ParentMessage, R>);
|
package/package.json
CHANGED
|
@@ -1,6 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ilha/model",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Elm-style Model, update, commands, and subscriptions for Ilha islands",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"effect",
|
|
7
|
+
"elm-architecture",
|
|
8
|
+
"frontend",
|
|
9
|
+
"hydration",
|
|
10
|
+
"ilha",
|
|
11
|
+
"island-architecture",
|
|
12
|
+
"islands",
|
|
13
|
+
"ssr"
|
|
14
|
+
],
|
|
15
|
+
"homepage": "https://github.com/ilhajs/ilha/tree/main/packages/model#readme",
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/ilhajs/ilha/issues"
|
|
18
|
+
},
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"author": "Ryuz <ryuzer@proton.me>",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/ilhajs/ilha.git"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist"
|
|
27
|
+
],
|
|
28
|
+
"type": "module",
|
|
29
|
+
"sideEffects": false,
|
|
30
|
+
"main": "./dist/index.js",
|
|
31
|
+
"module": "./dist/index.js",
|
|
32
|
+
"types": "./dist/index.d.ts",
|
|
33
|
+
"exports": {
|
|
34
|
+
".": {
|
|
35
|
+
"types": "./dist/index.d.ts",
|
|
36
|
+
"import": "./dist/index.js"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "tsdown && tsc -p tsconfig.build.json",
|
|
44
|
+
"cleanup": "rimraf dist node_modules",
|
|
45
|
+
"test": "bun test",
|
|
46
|
+
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@types/bun": "^1.4.4",
|
|
50
|
+
"effect": "^4.1.0",
|
|
51
|
+
"ilha": "^0.16.2"
|
|
52
|
+
},
|
|
53
|
+
"peerDependencies": {
|
|
54
|
+
"effect": "^4.1.0",
|
|
55
|
+
"ilha": ">=0.16.2"
|
|
56
|
+
}
|
|
57
|
+
}
|