@ilha/model 0.1.0 → 0.1.1
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/README.md +3 -1
- package/dist/app.d.ts +10 -11
- package/dist/index.d.ts +2 -0
- package/dist/index.js +43 -22
- package/dist/internal-kl_qCyzu.js +45 -0
- package/dist/internal.d.ts +36 -0
- package/dist/log.d.ts +33 -0
- package/dist/test.d.ts +56 -0
- package/dist/test.js +54 -0
- package/package.json +7 -3
package/README.md
CHANGED
|
@@ -54,4 +54,6 @@ export const Counter = app.component;
|
|
|
54
54
|
|
|
55
55
|
Mount it like any Ilha component: `mount(host, Counter)` or `await renderToString(Counter)`.
|
|
56
56
|
|
|
57
|
-
|
|
57
|
+
Test commands without rendering with `@ilha/model/test`: `runCommands` collects what commands resolve to, and `simulate` runs the loop on input messages.
|
|
58
|
+
|
|
59
|
+
See [Elm architecture](https://ilha.build/guide/ui/model) for commands, subscriptions, submodels, and testing.
|
package/dist/app.d.ts
CHANGED
|
@@ -36,7 +36,12 @@ export interface Subscription<Model, Message, Deps, R = never> {
|
|
|
36
36
|
* the model parameter; everything else infers.
|
|
37
37
|
*/
|
|
38
38
|
export declare const subscription: <Model, Message, Deps, R = never>(sub: Subscription<Model, Message, Deps, R>) => Subscription<Model, Message, Deps, R>;
|
|
39
|
-
/**
|
|
39
|
+
/**
|
|
40
|
+
* Options for {@link defineApp}. `Message` is inferred from `update`'s
|
|
41
|
+
* message parameter only; every other position checks against it, so a
|
|
42
|
+
* command that resolves to one variant never narrows the app's message
|
|
43
|
+
* type.
|
|
44
|
+
*/
|
|
40
45
|
export interface AppOptions<Model, Message, R = never, E = never> {
|
|
41
46
|
/** Component name shown in `ilha/debug` trees. Default `"ModelApp"`. */
|
|
42
47
|
readonly name?: string | undefined;
|
|
@@ -45,13 +50,13 @@ export interface AppOptions<Model, Message, R = never, E = never> {
|
|
|
45
50
|
* Runs once per instance; the command runs once per client mount, never
|
|
46
51
|
* during server rendering.
|
|
47
52
|
*/
|
|
48
|
-
readonly init: () => UpdateResult<Model, Message
|
|
53
|
+
readonly init: () => UpdateResult<Model, NoInfer<Message>, R>;
|
|
49
54
|
/** The only place state changes. Must be pure. */
|
|
50
|
-
readonly update: (model: Model, message: Message) => UpdateResult<Model, Message
|
|
55
|
+
readonly update: (model: Model, message: Message) => UpdateResult<Model, NoInfer<Message>, R>;
|
|
51
56
|
/** Render the model. Must be pure; report facts with `dispatch`. */
|
|
52
|
-
readonly view: (model: Model, dispatch: Dispatch<Message
|
|
57
|
+
readonly view: (model: Model, dispatch: Dispatch<NoInfer<Message>>) => View;
|
|
53
58
|
/** Ongoing work gated by slices of the model. */
|
|
54
|
-
readonly subscriptions?: readonly Subscription<Model, Message
|
|
59
|
+
readonly subscriptions?: readonly Subscription<Model, NoInfer<Message>, any, R>[] | undefined;
|
|
55
60
|
/** Services available to commands and subscriptions. */
|
|
56
61
|
readonly layer?: Layer.Layer<R, E> | undefined;
|
|
57
62
|
/**
|
|
@@ -65,12 +70,6 @@ export interface ModelApp<Model, Message, R = never, E = never> {
|
|
|
65
70
|
readonly component: Component;
|
|
66
71
|
readonly options: AppOptions<Model, Message, R, E>;
|
|
67
72
|
}
|
|
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
73
|
/**
|
|
75
74
|
* Latest-wins command: starting one interrupts the running command with the
|
|
76
75
|
* same key, so only the latest result dispatches. Different keys run
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
export type { AppOptions, Cmd, Dispatch, ModelApp, Subscription, UpdateResult, } from "./app";
|
|
2
2
|
export { defineApp, keyed, subscription } from "./app";
|
|
3
|
+
export type { DispatchRecord, ModelRecorder } from "./log";
|
|
4
|
+
export { attachModelRecorder } from "./log";
|
|
3
5
|
export type { FoldChildOptions, Step } from "./submodel";
|
|
4
6
|
export { combine, foldChild } from "./submodel";
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
import { a as runCommand, i as reportCause, r as eachCmd, t as KeyedScope } from "./internal-kl_qCyzu.js";
|
|
1
2
|
import * as Cause from "effect/Cause";
|
|
2
|
-
import * as Context from "effect/Context";
|
|
3
3
|
import * as Effect from "effect/Effect";
|
|
4
4
|
import * as Equal from "effect/Equal";
|
|
5
5
|
import * as Exit from "effect/Exit";
|
|
@@ -12,6 +12,33 @@ import * as Scope from "effect/Scope";
|
|
|
12
12
|
import * as Stream from "effect/Stream";
|
|
13
13
|
import { atom, untrack, watch } from "ilha";
|
|
14
14
|
|
|
15
|
+
// src/log.ts
|
|
16
|
+
/** Attached recorders; `dispatch` builds a record only while non-empty. */
|
|
17
|
+
const recorders = /* @__PURE__ */ new Set();
|
|
18
|
+
/**
|
|
19
|
+
* Hand every `dispatch` of every model island to `recorder` until the
|
|
20
|
+
* returned detach runs. The devtools page script attaches one for you.
|
|
21
|
+
* Detached, `dispatch` pays one size check: no record, no allocation. A
|
|
22
|
+
* recorder that throws is logged and never breaks `dispatch`.
|
|
23
|
+
*/
|
|
24
|
+
const attachModelRecorder = (recorder) => {
|
|
25
|
+
const entry = { recorder };
|
|
26
|
+
recorders.add(entry);
|
|
27
|
+
return () => {
|
|
28
|
+
recorders.delete(entry);
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
/** Whether any recorder is attached. Internal: `dispatch` checks it first. */
|
|
32
|
+
const isRecording = () => recorders.size > 0;
|
|
33
|
+
/** Deliver one dispatch to every recorder. Internal: `dispatch` calls it. */
|
|
34
|
+
const recordDispatch = (record) => {
|
|
35
|
+
for (const { recorder } of recorders) try {
|
|
36
|
+
recorder(record);
|
|
37
|
+
} catch (error) {
|
|
38
|
+
Effect.runSync(Effect.logError("@ilha/model recorder failed", error));
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
|
|
15
42
|
// src/app.ts
|
|
16
43
|
/**
|
|
17
44
|
* Name one subscription. Each entry infers its own `Deps` from its `deps`
|
|
@@ -20,29 +47,12 @@ import { atom, untrack, watch } from "ilha";
|
|
|
20
47
|
*/
|
|
21
48
|
const subscription = (sub) => sub;
|
|
22
49
|
/**
|
|
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
50
|
* Latest-wins command: starting one interrupts the running command with the
|
|
35
51
|
* same key, so only the latest result dispatches. Different keys run
|
|
36
52
|
* independently. Keys are per app instance: two mounted instances never
|
|
37
53
|
* share them.
|
|
38
54
|
*/
|
|
39
55
|
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
56
|
const makeInstanceCells = () => {
|
|
47
57
|
const keyedScope = Effect.runSync(Scope.make());
|
|
48
58
|
return {
|
|
@@ -97,15 +107,26 @@ const defineApp = (options) => {
|
|
|
97
107
|
};
|
|
98
108
|
const dispatch = (message) => {
|
|
99
109
|
if (!isAlive()) return;
|
|
100
|
-
const
|
|
110
|
+
const before = untrack(() => state());
|
|
111
|
+
const result = update(before, message);
|
|
101
112
|
state.set(result.model);
|
|
113
|
+
let started = 0;
|
|
102
114
|
if (result.cmd !== void 0) {
|
|
103
115
|
const { queue } = untrack(() => cells());
|
|
104
|
-
|
|
116
|
+
const cmds = eachCmd(result.cmd);
|
|
117
|
+
for (const cmd of cmds) Queue.offerUnsafe(queue, cmd);
|
|
118
|
+
started = cmds.length;
|
|
105
119
|
}
|
|
120
|
+
if (isRecording()) recordDispatch({
|
|
121
|
+
after: result.model,
|
|
122
|
+
before,
|
|
123
|
+
commands: started,
|
|
124
|
+
instance: state.atom,
|
|
125
|
+
message
|
|
126
|
+
});
|
|
106
127
|
};
|
|
107
128
|
const { keyedMap } = untrack(() => cells());
|
|
108
|
-
const runOne = (cmd) =>
|
|
129
|
+
const runOne = (cmd) => runCommand(cmd, (message) => Effect.sync(() => dispatch(message)), keyedMap);
|
|
109
130
|
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
131
|
const runnerFailure = atom(runner.atom.pipe(Atom.map((result) => result._tag === "Failure" && !Cause.hasInterruptsOnly(result.cause) ? Cause.pretty(result.cause) : "")));
|
|
111
132
|
runner();
|
|
@@ -174,4 +195,4 @@ const foldChild = (options) => {
|
|
|
174
195
|
};
|
|
175
196
|
};
|
|
176
197
|
|
|
177
|
-
export { combine, defineApp, foldChild, keyed, subscription };
|
|
198
|
+
export { attachModelRecorder, combine, defineApp, foldChild, keyed, subscription };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import * as Cause from "effect/Cause";
|
|
2
|
+
import * as Effect from "effect/Effect";
|
|
3
|
+
import * as Fiber from "effect/Fiber";
|
|
4
|
+
import * as Context from "effect/Context";
|
|
5
|
+
import * as Ref from "effect/Ref";
|
|
6
|
+
|
|
7
|
+
// src/internal.ts
|
|
8
|
+
/**
|
|
9
|
+
* Split one or many commands. Internal: shared by the runtime, `submodel`,
|
|
10
|
+
* and the test helpers, not part of the package surface. `Effect.isEffect`
|
|
11
|
+
* narrows the union where `Array.isArray` cannot, so no assertion is needed.
|
|
12
|
+
*/
|
|
13
|
+
const eachCmd = (cmd) => Effect.isEffect(cmd) ? [cmd] : cmd;
|
|
14
|
+
/**
|
|
15
|
+
* Per-instance keyed-command scope. The runner provides it around every
|
|
16
|
+
* command; `keyed` reads it back without adding a service requirement.
|
|
17
|
+
*/
|
|
18
|
+
const KeyedScope = Context.Service("ilha/model/KeyedScope");
|
|
19
|
+
/**
|
|
20
|
+
* Report an unexpected cause through the Effect logger. Interruption
|
|
21
|
+
* (unmount, keyed supersede, subscription restart) is silent; anything else
|
|
22
|
+
* is a defect worth logging.
|
|
23
|
+
*/
|
|
24
|
+
const reportCause = (cause) => Cause.hasInterruptsOnly(cause) ? Effect.void : Effect.logError(Cause.pretty(cause));
|
|
25
|
+
/**
|
|
26
|
+
* Run one command, delivering its message when it resolves to one. Shared
|
|
27
|
+
* by the app runtime and the test helpers: a `void` result delivers
|
|
28
|
+
* nothing, defects are logged, and interruption stays silent.
|
|
29
|
+
*/
|
|
30
|
+
const runCommand = (cmd, deliver, keyedMap) => Effect.provideService(Effect.catchCause(cmd.pipe(Effect.flatMap((out) => out === void 0 ? Effect.void : deliver(out))), reportCause), KeyedScope, keyedMap);
|
|
31
|
+
/**
|
|
32
|
+
* Run commands concurrently like the app runtime, collecting delivered
|
|
33
|
+
* messages in completion order. Commands fork in array order and each
|
|
34
|
+
* `keyed` registration is synchronous, so same-key commands supersede
|
|
35
|
+
* deterministically with the latest winning.
|
|
36
|
+
*/
|
|
37
|
+
const drainCommands = (cmds, keyedMap) => Effect.gen(function* drain() {
|
|
38
|
+
const outbox = yield* Ref.make([]);
|
|
39
|
+
const fibers = [];
|
|
40
|
+
for (const cmd of cmds) fibers.push(yield* Effect.forkChild(runCommand(cmd, (message) => Ref.update(outbox, (seen) => [...seen, message]), keyedMap)));
|
|
41
|
+
for (const fiber of fibers) yield* Fiber.join(fiber);
|
|
42
|
+
return yield* Ref.get(outbox);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
export { runCommand as a, reportCause as i, drainCommands as n, eachCmd as r, KeyedScope as t };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import * as Cause from "effect/Cause";
|
|
2
|
+
import * as Context from "effect/Context";
|
|
3
|
+
import * as Effect from "effect/Effect";
|
|
4
|
+
import type * as FiberMap from "effect/FiberMap";
|
|
5
|
+
import type { AtomRegistry } from "effect/reactivity/AtomRegistry";
|
|
6
|
+
import type { Cmd } from "./app";
|
|
7
|
+
/**
|
|
8
|
+
* Split one or many commands. Internal: shared by the runtime, `submodel`,
|
|
9
|
+
* and the test helpers, not part of the package surface. `Effect.isEffect`
|
|
10
|
+
* narrows the union where `Array.isArray` cannot, so no assertion is needed.
|
|
11
|
+
*/
|
|
12
|
+
export declare const eachCmd: <Message, R>(cmd: Cmd<Message, R> | readonly Cmd<Message, R>[]) => readonly Cmd<Message, R>[];
|
|
13
|
+
/**
|
|
14
|
+
* Per-instance keyed-command scope. The runner provides it around every
|
|
15
|
+
* command; `keyed` reads it back without adding a service requirement.
|
|
16
|
+
*/
|
|
17
|
+
export declare const KeyedScope: Context.Service<FiberMap.FiberMap<string, unknown, unknown>, FiberMap.FiberMap<string, unknown, unknown>>;
|
|
18
|
+
/**
|
|
19
|
+
* Report an unexpected cause through the Effect logger. Interruption
|
|
20
|
+
* (unmount, keyed supersede, subscription restart) is silent; anything else
|
|
21
|
+
* is a defect worth logging.
|
|
22
|
+
*/
|
|
23
|
+
export declare const reportCause: (cause: Cause.Cause<never>) => Effect.Effect<void>;
|
|
24
|
+
/**
|
|
25
|
+
* Run one command, delivering its message when it resolves to one. Shared
|
|
26
|
+
* by the app runtime and the test helpers: a `void` result delivers
|
|
27
|
+
* nothing, defects are logged, and interruption stays silent.
|
|
28
|
+
*/
|
|
29
|
+
export declare const runCommand: <Message, R>(cmd: Cmd<Message, R>, deliver: (message: Message) => Effect.Effect<void>, keyedMap: FiberMap.FiberMap<string, unknown, unknown>) => Effect.Effect<void, never, R | AtomRegistry>;
|
|
30
|
+
/**
|
|
31
|
+
* Run commands concurrently like the app runtime, collecting delivered
|
|
32
|
+
* messages in completion order. Commands fork in array order and each
|
|
33
|
+
* `keyed` registration is synchronous, so same-key commands supersede
|
|
34
|
+
* deterministically with the latest winning.
|
|
35
|
+
*/
|
|
36
|
+
export declare const drainCommands: <Message, R>(cmds: readonly Cmd<Message, R>[], keyedMap: FiberMap.FiberMap<string, unknown, unknown>) => Effect.Effect<readonly Message[], never, R | AtomRegistry>;
|
package/dist/log.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One dispatch as an attached recorder sees it: raw values, nothing copied
|
|
3
|
+
* or summarized. Summarizing, diffing, and buffering belong to the
|
|
4
|
+
* recorder, so apps never ship that code.
|
|
5
|
+
*/
|
|
6
|
+
export interface DispatchRecord {
|
|
7
|
+
/**
|
|
8
|
+
* The island instance's model atom. Join it against `atomsOf(id)` from
|
|
9
|
+
* `ilha/debug` to find which island dispatched.
|
|
10
|
+
*/
|
|
11
|
+
readonly instance: object;
|
|
12
|
+
/** The dispatched message. */
|
|
13
|
+
readonly message: unknown;
|
|
14
|
+
/** The model `update` received. */
|
|
15
|
+
readonly before: unknown;
|
|
16
|
+
/** The model `update` returned. */
|
|
17
|
+
readonly after: unknown;
|
|
18
|
+
/** How many commands the message started. */
|
|
19
|
+
readonly commands: number;
|
|
20
|
+
}
|
|
21
|
+
/** Receives every dispatch while attached. */
|
|
22
|
+
export type ModelRecorder = (record: DispatchRecord) => void;
|
|
23
|
+
/**
|
|
24
|
+
* Hand every `dispatch` of every model island to `recorder` until the
|
|
25
|
+
* returned detach runs. The devtools page script attaches one for you.
|
|
26
|
+
* Detached, `dispatch` pays one size check: no record, no allocation. A
|
|
27
|
+
* recorder that throws is logged and never breaks `dispatch`.
|
|
28
|
+
*/
|
|
29
|
+
export declare const attachModelRecorder: (recorder: ModelRecorder) => (() => void);
|
|
30
|
+
/** Whether any recorder is attached. Internal: `dispatch` checks it first. */
|
|
31
|
+
export declare const isRecording: () => boolean;
|
|
32
|
+
/** Deliver one dispatch to every recorder. Internal: `dispatch` calls it. */
|
|
33
|
+
export declare const recordDispatch: (record: DispatchRecord) => void;
|
package/dist/test.d.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import * as Effect from "effect/Effect";
|
|
2
|
+
import * as Layer from "effect/Layer";
|
|
3
|
+
import * as AtomRegistry from "effect/reactivity/AtomRegistry";
|
|
4
|
+
import type { Cmd, ModelApp, UpdateResult } from "./app";
|
|
5
|
+
/**
|
|
6
|
+
* Message a command resolves to, excluding `void` results that deliver
|
|
7
|
+
* nothing.
|
|
8
|
+
*/
|
|
9
|
+
type CommandMessage<T> = T extends Effect.Effect<infer M, never, unknown> ? Exclude<M, void> : never;
|
|
10
|
+
/**
|
|
11
|
+
* Services a command still requires, excluding the `AtomRegistry` the
|
|
12
|
+
* helpers provide themselves.
|
|
13
|
+
*/
|
|
14
|
+
type CommandServices<T> = T extends Effect.Effect<unknown, never, infer R> ? Exclude<R, AtomRegistry.AtomRegistry> : never;
|
|
15
|
+
/**
|
|
16
|
+
* Run one command without rendering and collect the message it resolves
|
|
17
|
+
* to. Commands run with the app runtime's semantics: `keyed` supersedes
|
|
18
|
+
* per key with the latest winning, defects are logged and dropped, and
|
|
19
|
+
* `void` results produce no message. Provide services with
|
|
20
|
+
* `Effect.provide` and time with `TestClock` from `effect/testing`.
|
|
21
|
+
*/
|
|
22
|
+
export declare function runCommands<Message, R>(input: Cmd<Message, R>): Effect.Effect<readonly Exclude<Message, void>[], never, Exclude<R, AtomRegistry.AtomRegistry>>;
|
|
23
|
+
/**
|
|
24
|
+
* Run several commands concurrently, collecting their messages in
|
|
25
|
+
* completion order.
|
|
26
|
+
*/
|
|
27
|
+
export declare function runCommands<const Cmds extends readonly Cmd<unknown, unknown>[]>(input: Cmds): Effect.Effect<readonly CommandMessage<Cmds[number]>[], never, CommandServices<Cmds[number]>>;
|
|
28
|
+
/** Run the command of an update result, collecting its messages. */
|
|
29
|
+
export declare function runCommands<Model, Message, R>(input: UpdateResult<Model, Message, R>): Effect.Effect<readonly Exclude<Message, void>[], never, Exclude<R, AtomRegistry.AtomRegistry>>;
|
|
30
|
+
/** Result of {@link simulate}: the final model and every message seen. */
|
|
31
|
+
export interface SimulateResult<Model, Message> {
|
|
32
|
+
/** Model after init and every message ran to quiet. */
|
|
33
|
+
readonly model: Model;
|
|
34
|
+
/** Every message `update` saw, in the order it saw them. */
|
|
35
|
+
readonly messages: readonly Message[];
|
|
36
|
+
}
|
|
37
|
+
/** Options for {@link simulate}. */
|
|
38
|
+
export interface SimulateOptions<R, E2 = never> {
|
|
39
|
+
/**
|
|
40
|
+
* Replace the app's layer for this run, for example with test doubles.
|
|
41
|
+
* Must provide the same services the app needs. Defaults to the app's
|
|
42
|
+
* own layer.
|
|
43
|
+
*/
|
|
44
|
+
readonly layer?: Layer.Layer<R, E2> | undefined;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Run an app's loop without rendering: init and its command first, then
|
|
48
|
+
* each input message through `update`, running every resulting command and
|
|
49
|
+
* feeding produced messages back through `update` until quiet. Takes the
|
|
50
|
+
* `ModelApp` from `defineApp` so the layer travels with `init` and
|
|
51
|
+
* `update`. Subscriptions never run here; test them through `update` with
|
|
52
|
+
* the messages their streams would emit. Commands that sleep run on the
|
|
53
|
+
* ambient clock, so `TestClock` from `effect/testing` controls them.
|
|
54
|
+
*/
|
|
55
|
+
export declare const simulate: <Model, Message, R, E, E2 = never>(app: ModelApp<Model, Message, R, E>, inputs: readonly NoInfer<Message>[], options?: SimulateOptions<NoInfer<R>, E2> | undefined) => Effect.Effect<SimulateResult<Model, Message>, E | E2>;
|
|
56
|
+
export {};
|
package/dist/test.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { n as drainCommands, r as eachCmd } from "./internal-kl_qCyzu.js";
|
|
2
|
+
import * as Effect from "effect/Effect";
|
|
3
|
+
import * as FiberMap from "effect/FiberMap";
|
|
4
|
+
import * as Layer from "effect/Layer";
|
|
5
|
+
import * as AtomRegistry from "effect/reactivity/AtomRegistry";
|
|
6
|
+
|
|
7
|
+
// src/test.ts
|
|
8
|
+
/**
|
|
9
|
+
* Normalize the `runCommands` input to a command list. Update results
|
|
10
|
+
* without a command run nothing.
|
|
11
|
+
*/
|
|
12
|
+
const toCmds = (input) => {
|
|
13
|
+
if (Effect.isEffect(input)) return [input];
|
|
14
|
+
if ("model" in input) return input.cmd === void 0 ? [] : eachCmd(input.cmd);
|
|
15
|
+
return input;
|
|
16
|
+
};
|
|
17
|
+
function runCommands(input) {
|
|
18
|
+
return Effect.scoped(Effect.flatMap(FiberMap.make(), (keyedMap) => Effect.provide(drainCommands(toCmds(input), keyedMap), AtomRegistry.layer)));
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Run an app's loop without rendering: init and its command first, then
|
|
22
|
+
* each input message through `update`, running every resulting command and
|
|
23
|
+
* feeding produced messages back through `update` until quiet. Takes the
|
|
24
|
+
* `ModelApp` from `defineApp` so the layer travels with `init` and
|
|
25
|
+
* `update`. Subscriptions never run here; test them through `update` with
|
|
26
|
+
* the messages their streams would emit. Commands that sleep run on the
|
|
27
|
+
* ambient clock, so `TestClock` from `effect/testing` controls them.
|
|
28
|
+
*/
|
|
29
|
+
const simulate = (app, inputs, options) => Effect.scoped(Effect.flatMap(FiberMap.make(), (keyedMap) => {
|
|
30
|
+
const provided = options?.layer ?? app.options.layer ?? Layer.empty;
|
|
31
|
+
const drain = (cmd) => cmd === void 0 ? Effect.succeed([]) : drainCommands(eachCmd(cmd), keyedMap);
|
|
32
|
+
return Effect.provide(Effect.gen(function* runLoop() {
|
|
33
|
+
const begun = app.options.init();
|
|
34
|
+
const first = yield* drain(begun.cmd);
|
|
35
|
+
let { model } = begun;
|
|
36
|
+
const queue = [...first, ...inputs];
|
|
37
|
+
const seen = [];
|
|
38
|
+
while (true) {
|
|
39
|
+
const message = queue.shift();
|
|
40
|
+
if (message === void 0) break;
|
|
41
|
+
seen.push(message);
|
|
42
|
+
const { cmd: command, model: next } = app.options.update(model, message);
|
|
43
|
+
model = next;
|
|
44
|
+
const produced = yield* drain(command);
|
|
45
|
+
queue.push(...produced);
|
|
46
|
+
}
|
|
47
|
+
return {
|
|
48
|
+
messages: seen,
|
|
49
|
+
model
|
|
50
|
+
};
|
|
51
|
+
}), Layer.merge(provided, AtomRegistry.layer));
|
|
52
|
+
}));
|
|
53
|
+
|
|
54
|
+
export { runCommands, simulate };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ilha/model",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Elm-style Model, update, commands, and subscriptions for Ilha islands",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"effect",
|
|
@@ -34,6 +34,10 @@
|
|
|
34
34
|
".": {
|
|
35
35
|
"types": "./dist/index.d.ts",
|
|
36
36
|
"import": "./dist/index.js"
|
|
37
|
+
},
|
|
38
|
+
"./test": {
|
|
39
|
+
"types": "./dist/test.d.ts",
|
|
40
|
+
"import": "./dist/test.js"
|
|
37
41
|
}
|
|
38
42
|
},
|
|
39
43
|
"publishConfig": {
|
|
@@ -48,10 +52,10 @@
|
|
|
48
52
|
"devDependencies": {
|
|
49
53
|
"@types/bun": "^1.4.4",
|
|
50
54
|
"effect": "^4.1.0",
|
|
51
|
-
"ilha": "^0.16.
|
|
55
|
+
"ilha": "^0.16.3"
|
|
52
56
|
},
|
|
53
57
|
"peerDependencies": {
|
|
54
58
|
"effect": "^4.1.0",
|
|
55
|
-
"ilha": ">=0.16.
|
|
59
|
+
"ilha": ">=0.16.3"
|
|
56
60
|
}
|
|
57
61
|
}
|