@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/dist/index.js
CHANGED
|
@@ -1,16 +1,51 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { t as __exportAll } from "./rolldown-runtime-CNh9ExCG.js";
|
|
2
|
+
import { a as reportCause, i as quietStream, n as initDollar, o as updateDollar, r as isStepValue, t as createStep } from "./internal-DsyiAF5H.js";
|
|
2
3
|
import * as Cause from "effect/Cause";
|
|
3
4
|
import * as Effect from "effect/Effect";
|
|
4
5
|
import * as Equal from "effect/Equal";
|
|
5
|
-
import * as Exit from "effect/Exit";
|
|
6
6
|
import * as Fiber from "effect/Fiber";
|
|
7
|
-
import * as FiberMap from "effect/FiberMap";
|
|
8
7
|
import * as Layer from "effect/Layer";
|
|
9
8
|
import * as Queue from "effect/Queue";
|
|
10
9
|
import * as Atom from "effect/reactivity/Atom";
|
|
11
|
-
import * as
|
|
10
|
+
import * as Schema from "effect/Schema";
|
|
12
11
|
import * as Stream from "effect/Stream";
|
|
13
|
-
import { atom, untrack, watch } from "ilha";
|
|
12
|
+
import { atom, h, untrack, watch } from "ilha";
|
|
13
|
+
|
|
14
|
+
// src/command.ts
|
|
15
|
+
var command_exports = /* @__PURE__ */ __exportAll({ make: () => make$1 });
|
|
16
|
+
/** Build a command from a body; issues run concurrently unless switched. */
|
|
17
|
+
const make$1 = (name, second, third) => {
|
|
18
|
+
const hasOptions = third !== void 0;
|
|
19
|
+
const options = hasOptions ? second : {};
|
|
20
|
+
const body = hasOptions ? third : second;
|
|
21
|
+
const command = (...args) => ({
|
|
22
|
+
args: args[0],
|
|
23
|
+
body: () => body(args[0]),
|
|
24
|
+
mode: options.mode ?? "concurrent",
|
|
25
|
+
name
|
|
26
|
+
});
|
|
27
|
+
Object.defineProperty(command, "name", { value: name });
|
|
28
|
+
return command;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
// src/subscription.ts
|
|
32
|
+
var subscription_exports = /* @__PURE__ */ __exportAll({ make: () => make });
|
|
33
|
+
/** A subscription with constant deps: same stream for the instance's life. */
|
|
34
|
+
const make = (name, second, third) => {
|
|
35
|
+
if (third === void 0) {
|
|
36
|
+
const stream = second;
|
|
37
|
+
return {
|
|
38
|
+
depsOf: () => void 0,
|
|
39
|
+
name,
|
|
40
|
+
run: () => stream
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
return {
|
|
44
|
+
depsOf: second,
|
|
45
|
+
name,
|
|
46
|
+
run: third
|
|
47
|
+
};
|
|
48
|
+
};
|
|
14
49
|
|
|
15
50
|
// src/log.ts
|
|
16
51
|
/** Attached recorders; `dispatch` builds a record only while non-empty. */
|
|
@@ -39,160 +74,225 @@ const recordDispatch = (record) => {
|
|
|
39
74
|
}
|
|
40
75
|
};
|
|
41
76
|
|
|
42
|
-
// src/
|
|
43
|
-
|
|
44
|
-
* Name one subscription. Each entry infers its own `Deps` from its `deps`
|
|
45
|
-
* function, so `stream` receives the exact type its deps return. Annotate
|
|
46
|
-
* the model parameter; everything else infers.
|
|
47
|
-
*/
|
|
48
|
-
const subscription = (sub) => sub;
|
|
77
|
+
// src/component.ts
|
|
78
|
+
const isSendMapper = (value) => typeof value === "function";
|
|
49
79
|
/**
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
* share them.
|
|
80
|
+
* Define a component. `model` is also the SSR snapshot codec; `message`
|
|
81
|
+
* is usually a `Schema.TaggedUnion`. Returns `{ make(impl) }`; `make`
|
|
82
|
+
* returns a `ModelComponent` usable as an Ilha component or JSX tag.
|
|
54
83
|
*/
|
|
55
|
-
const
|
|
56
|
-
const
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
84
|
+
const defineComponent = (options) => {
|
|
85
|
+
const withProps = options;
|
|
86
|
+
const emptyProps = Schema.Struct({});
|
|
87
|
+
const propsSchema = withProps.props ?? emptyProps;
|
|
88
|
+
const definition = {
|
|
89
|
+
events: options.events ?? Schema.Never,
|
|
90
|
+
message: options.message,
|
|
91
|
+
model: options.model,
|
|
92
|
+
name: options.name,
|
|
93
|
+
props: propsSchema
|
|
64
94
|
};
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
95
|
+
const make = (impl) => {
|
|
96
|
+
const modelSchema = definition.model;
|
|
97
|
+
const provided = impl.layer ?? Layer.empty;
|
|
98
|
+
const runtime = Atom.runtime(provided);
|
|
99
|
+
const render = (callOptions) => {
|
|
100
|
+
const rawProps = { ...callOptions?.props };
|
|
101
|
+
const onEmit = callOptions?.onEmit;
|
|
102
|
+
const propsState = atom.lazy(() => ({
|
|
103
|
+
onEmit,
|
|
104
|
+
props: Schema.decodeSync(propsSchema)(rawProps),
|
|
105
|
+
raw: rawProps
|
|
106
|
+
}), { snapshot: false });
|
|
107
|
+
const known = untrack(() => propsState());
|
|
108
|
+
if (known.onEmit !== onEmit || !Equal.equals(known.raw, rawProps)) propsState.set({
|
|
109
|
+
onEmit,
|
|
110
|
+
props: Schema.decodeSync(propsSchema)(rawProps),
|
|
111
|
+
raw: rawProps
|
|
112
|
+
});
|
|
113
|
+
const initResult = atom.lazy(() => {
|
|
114
|
+
const { props } = untrack(() => propsState());
|
|
115
|
+
const dollar = initDollar(props);
|
|
116
|
+
const out = impl.init(dollar, props);
|
|
117
|
+
return isStepValue(out) ? out : createStep(out);
|
|
118
|
+
}, { snapshot: false });
|
|
119
|
+
const state = atom.lazy(() => untrack(() => initResult()).model, { schema: modelSchema });
|
|
120
|
+
const cells = atom.lazy(() => ({
|
|
121
|
+
alive: true,
|
|
122
|
+
generations: /* @__PURE__ */ new Map(),
|
|
123
|
+
queue: Effect.runSync(Queue.unbounded()),
|
|
124
|
+
started: false,
|
|
125
|
+
switches: /* @__PURE__ */ new Map()
|
|
126
|
+
}), { snapshot: false });
|
|
127
|
+
const counts = atom.lazy(() => ({}), { snapshot: false });
|
|
128
|
+
const combo = atom.lazy(() => {
|
|
129
|
+
const live = untrack(() => state());
|
|
130
|
+
const { props: liveProps } = untrack(() => propsState());
|
|
131
|
+
return {
|
|
132
|
+
model: live,
|
|
133
|
+
props: liveProps,
|
|
134
|
+
subs: impl.subscriptions?.(live, liveProps) ?? []
|
|
135
|
+
};
|
|
136
|
+
}, { snapshot: false });
|
|
137
|
+
const activeSubs = /* @__PURE__ */ new Map();
|
|
138
|
+
const manageEntries = (latest, outbox) => Effect.gen(function* manage() {
|
|
139
|
+
const seen = /* @__PURE__ */ new Set();
|
|
140
|
+
for (const entry of latest.subs) {
|
|
141
|
+
seen.add(entry.name);
|
|
142
|
+
const deps = entry.depsOf(latest.model, latest.props);
|
|
143
|
+
const current = activeSubs.get(entry.name);
|
|
144
|
+
if (current !== void 0 && Equal.equals(current.deps, deps)) continue;
|
|
145
|
+
if (current !== void 0) yield* Fiber.interrupt(current.fiber);
|
|
146
|
+
const fiber = yield* Effect.forkScoped(Stream.runForEach(Stream.suspend(() => entry.run(deps)).pipe(Stream.catchCause(quietStream)), (message) => Queue.offer(outbox, message)));
|
|
147
|
+
activeSubs.set(entry.name, {
|
|
148
|
+
deps,
|
|
149
|
+
fiber
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
for (const [name, current] of activeSubs) if (!seen.has(name)) {
|
|
153
|
+
yield* Fiber.interrupt(current.fiber);
|
|
154
|
+
activeSubs.delete(name);
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
const isAlive = () => {
|
|
158
|
+
try {
|
|
159
|
+
return untrack(() => cells()).alive;
|
|
160
|
+
} catch {
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
const toJob = (issued, deliver) => {
|
|
165
|
+
let guarded = deliver;
|
|
166
|
+
if (issued.mode === "switch") {
|
|
167
|
+
const { generations } = untrack(() => cells());
|
|
168
|
+
const generation = (generations.get(issued.name) ?? 0) + 1;
|
|
169
|
+
generations.set(issued.name, generation);
|
|
170
|
+
guarded = (message) => {
|
|
171
|
+
if (untrack(() => cells()).generations.get(issued.name) === generation) deliver(message);
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
const run = Effect.catchCause(Effect.flatMap(issued.body(), (outcome) => outcome === void 0 ? Effect.void : Effect.sync(() => guarded(outcome))), reportCause);
|
|
175
|
+
return {
|
|
176
|
+
mode: issued.mode,
|
|
177
|
+
name: issued.name,
|
|
178
|
+
run
|
|
179
|
+
};
|
|
180
|
+
};
|
|
181
|
+
const dispatch = (message) => {
|
|
182
|
+
if (!isAlive()) return;
|
|
183
|
+
const { props: liveProps } = untrack(() => propsState());
|
|
184
|
+
const before = untrack(() => state());
|
|
185
|
+
const handler = impl.update[message._tag];
|
|
186
|
+
if (handler === void 0) throw new Error(`${definition.name}: no update for "${message._tag}"`);
|
|
187
|
+
const dollar = updateDollar(before, liveProps);
|
|
188
|
+
let out;
|
|
189
|
+
try {
|
|
190
|
+
out = handler(dollar, message);
|
|
191
|
+
} catch (error) {
|
|
192
|
+
Effect.runSync(Effect.logError(Cause.pretty(Cause.die(error))));
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
const step = isStepValue(out) ? out : createStep(out);
|
|
196
|
+
state.set(step.model);
|
|
197
|
+
let started = 0;
|
|
198
|
+
if (step.commands.length > 0) {
|
|
199
|
+
const { queue } = untrack(() => cells());
|
|
200
|
+
for (const issued of step.commands) Queue.offerUnsafe(queue, toJob(issued, dispatch));
|
|
201
|
+
started = step.commands.length;
|
|
202
|
+
}
|
|
203
|
+
if (isRecording()) recordDispatch({
|
|
204
|
+
after: step.model,
|
|
205
|
+
before,
|
|
206
|
+
commands: started,
|
|
207
|
+
instance: state.atom,
|
|
208
|
+
message
|
|
209
|
+
});
|
|
210
|
+
if (step.events.length > 0) {
|
|
211
|
+
const emit = untrack(() => propsState()).onEmit;
|
|
212
|
+
if (emit !== void 0) for (const event of step.events) emit(event);
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
const bump = (name, delta) => {
|
|
216
|
+
counts.update((previous) => ({
|
|
217
|
+
...previous,
|
|
218
|
+
[name]: (previous[name] ?? 0) + delta
|
|
219
|
+
}));
|
|
220
|
+
};
|
|
221
|
+
const runJob = (job) => job.mode === "switch" ? Effect.gen(function* runSwitch() {
|
|
222
|
+
const { switches } = untrack(() => cells());
|
|
223
|
+
const previous = switches.get(job.name);
|
|
224
|
+
if (previous !== void 0) yield* Fiber.interrupt(previous);
|
|
225
|
+
bump(job.name, 1);
|
|
226
|
+
const cell = {};
|
|
227
|
+
const fiber = yield* Effect.forkScoped(Effect.ensuring(job.run, Effect.sync(() => {
|
|
228
|
+
bump(job.name, -1);
|
|
229
|
+
if (switches.get(job.name) === cell.current) switches.delete(job.name);
|
|
230
|
+
})));
|
|
231
|
+
cell.current = fiber;
|
|
232
|
+
switches.set(job.name, fiber);
|
|
233
|
+
}) : Effect.flatMap(Effect.sync(() => bump(job.name, 1)), () => Effect.forkScoped(Effect.ensuring(job.run, Effect.sync(() => bump(job.name, -1)))));
|
|
234
|
+
const subEffect = Effect.flatMap(Queue.unbounded(), (outbox) => Stream.runForEach(Stream.mergeAll([Stream.fromQueue(outbox), Atom.toStream(combo.atom).pipe(Stream.tap((latest) => manageEntries(latest, outbox)), Stream.flatMap(() => Stream.empty))], { concurrency: "unbounded" }), (message) => Effect.catchCause(Effect.sync(() => dispatch(message)), reportCause)));
|
|
235
|
+
const runner = atom(Atom.fn((queue, get) => Effect.flatMap(get.result(runtime), (services) => Effect.provideContext(Effect.scoped(Effect.flatMap(Effect.forkScoped(subEffect), () => Effect.forever(Effect.flatMap(Queue.take(queue), runJob)))), services))));
|
|
236
|
+
const runnerFailure = atom(runner.atom.pipe(Atom.map((result) => result._tag === "Failure" && !Cause.hasInterruptsOnly(result.cause) ? Cause.pretty(result.cause) : "")));
|
|
237
|
+
runner();
|
|
238
|
+
watch(runnerFailure, (failure) => {
|
|
239
|
+
if (failure !== "") Effect.runSync(Effect.logError(failure));
|
|
240
|
+
});
|
|
241
|
+
watch.once(({ ssr }) => {
|
|
242
|
+
const helpers = untrack(() => cells());
|
|
243
|
+
if (!ssr && !helpers.started) {
|
|
244
|
+
helpers.started = true;
|
|
245
|
+
const begun = untrack(() => initResult());
|
|
246
|
+
for (const issued of begun.commands) Queue.offerUnsafe(helpers.queue, toJob(issued, dispatch));
|
|
247
|
+
if (begun.events.length > 0) {
|
|
248
|
+
const emit = untrack(() => propsState()).onEmit;
|
|
249
|
+
if (emit !== void 0) for (const event of begun.events) emit(event);
|
|
250
|
+
}
|
|
251
|
+
runner.set(helpers.queue);
|
|
252
|
+
}
|
|
253
|
+
return () => {
|
|
254
|
+
helpers.alive = false;
|
|
255
|
+
};
|
|
256
|
+
});
|
|
257
|
+
const model = state();
|
|
258
|
+
const { props } = untrack(() => propsState());
|
|
259
|
+
const subs = impl.subscriptions?.(model, props) ?? [];
|
|
260
|
+
const seen = /* @__PURE__ */ new Set();
|
|
261
|
+
for (const sub of subs) {
|
|
262
|
+
if (seen.has(sub.name)) throw new Error(`${definition.name}: duplicate subscription "${sub.name}"`);
|
|
263
|
+
seen.add(sub.name);
|
|
119
264
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
265
|
+
combo.set({
|
|
266
|
+
model,
|
|
267
|
+
props,
|
|
268
|
+
subs
|
|
269
|
+
});
|
|
270
|
+
const send = ((input) => {
|
|
271
|
+
if (isSendMapper(input)) return (arg) => {
|
|
272
|
+
const resolved = input(arg);
|
|
273
|
+
if (resolved !== void 0) dispatch(resolved);
|
|
274
|
+
};
|
|
275
|
+
return () => {
|
|
276
|
+
dispatch(input);
|
|
277
|
+
};
|
|
278
|
+
});
|
|
279
|
+
const isRunning = (command) => (counts()[command.name] ?? 0) > 0;
|
|
280
|
+
const callH = ((node, attrs, ...rest) => h(node, attrs, ...rest));
|
|
281
|
+
const viewH = Object.assign(callH, {
|
|
282
|
+
isRunning,
|
|
283
|
+
props,
|
|
284
|
+
send
|
|
126
285
|
});
|
|
286
|
+
return impl.view(viewH, model);
|
|
127
287
|
};
|
|
128
|
-
const
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
watch(runnerFailure, (failure) => {
|
|
134
|
-
if (failure !== "") Effect.runSync(Effect.logError(failure));
|
|
288
|
+
const component = render;
|
|
289
|
+
Object.defineProperty(component, "name", { value: definition.name });
|
|
290
|
+
return Object.assign(component, {
|
|
291
|
+
definition,
|
|
292
|
+
impl
|
|
135
293
|
});
|
|
136
|
-
watch.once(({ ssr }) => {
|
|
137
|
-
const helpers = untrack(() => cells());
|
|
138
|
-
if (!ssr && !helpers.started) {
|
|
139
|
-
helpers.started = true;
|
|
140
|
-
const begin = untrack(() => initResult()).cmd;
|
|
141
|
-
if (begin !== void 0) for (const cmd of eachCmd(begin)) Queue.offerUnsafe(helpers.queue, cmd);
|
|
142
|
-
if (subscriptions.length > 0) Queue.offerUnsafe(helpers.queue, Stream.runForEach(untrack(() => merged()).stream, (message) => Effect.catchCause(Effect.sync(() => dispatch(message)), reportCause)));
|
|
143
|
-
runner.set(helpers.queue);
|
|
144
|
-
}
|
|
145
|
-
return () => {
|
|
146
|
-
helpers.alive = false;
|
|
147
|
-
Effect.runFork(Scope.close(helpers.keyedScope, Exit.void));
|
|
148
|
-
};
|
|
149
|
-
});
|
|
150
|
-
return view(state(), dispatch);
|
|
151
|
-
};
|
|
152
|
-
Object.defineProperty(component, "name", { value: options.name ?? "ModelApp" });
|
|
153
|
-
return {
|
|
154
|
-
component,
|
|
155
|
-
options
|
|
156
|
-
};
|
|
157
|
-
};
|
|
158
|
-
|
|
159
|
-
// src/submodel.ts
|
|
160
|
-
/**
|
|
161
|
-
* Run steps in order, threading each model into the next. The steps run in
|
|
162
|
-
* order, but the commands from all steps run concurrently, like any command
|
|
163
|
-
* list.
|
|
164
|
-
*/
|
|
165
|
-
const combine = (model, steps) => {
|
|
166
|
-
let current = model;
|
|
167
|
-
const cmds = [];
|
|
168
|
-
for (const step of steps) {
|
|
169
|
-
const result = step(current);
|
|
170
|
-
current = result.model;
|
|
171
|
-
if (result.cmd !== void 0) cmds.push(...eachCmd(result.cmd));
|
|
172
|
-
}
|
|
173
|
-
if (cmds.length === 0) return { model: current };
|
|
174
|
-
return {
|
|
175
|
-
cmd: cmds.length === 1 ? cmds[0] : cmds,
|
|
176
|
-
model: current
|
|
177
|
-
};
|
|
178
|
-
};
|
|
179
|
-
/**
|
|
180
|
-
* Embed a child update in a parent. Runs the child, writes its model back,
|
|
181
|
-
* and lifts its commands so results arrive wrapped for the parent.
|
|
182
|
-
*/
|
|
183
|
-
const foldChild = (options) => {
|
|
184
|
-
const { read, toParentMessage, update, write } = options;
|
|
185
|
-
return (model, message) => {
|
|
186
|
-
const child = read(model);
|
|
187
|
-
if (child === void 0) return { model };
|
|
188
|
-
const result = update(child, message);
|
|
189
|
-
const next = write(model, result.model);
|
|
190
|
-
if (result.cmd === void 0) return { model: next };
|
|
191
|
-
return {
|
|
192
|
-
cmd: eachCmd(result.cmd).map((cmd) => cmd.pipe(Effect.map((out) => out === void 0 ? void 0 : toParentMessage(out)))),
|
|
193
|
-
model: next
|
|
194
|
-
};
|
|
195
294
|
};
|
|
295
|
+
return { make };
|
|
196
296
|
};
|
|
197
297
|
|
|
198
|
-
export {
|
|
298
|
+
export { command_exports as Command, subscription_exports as Subscription, attachModelRecorder, defineComponent };
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import * as Cause from "effect/Cause";
|
|
2
|
+
import * as Effect from "effect/Effect";
|
|
3
|
+
import * as Stream from "effect/Stream";
|
|
4
|
+
|
|
5
|
+
// src/internal.ts
|
|
6
|
+
/**
|
|
7
|
+
* Shared runtime bits for the component loop and the `Story` test helper.
|
|
8
|
+
* Everything here is generic over the component's own types, so no value
|
|
9
|
+
* is ever erased to `unknown`: each bundle keeps full static evidence.
|
|
10
|
+
*/
|
|
11
|
+
/** Log a defect through the Effect logger; interruption stays silent. */
|
|
12
|
+
const reportCause = (cause) => Cause.hasInterruptsOnly(cause) ? Effect.void : Effect.logError(Cause.pretty(cause));
|
|
13
|
+
/**
|
|
14
|
+
* A defect inside a subscription stream is logged and silences only that
|
|
15
|
+
* stream, so the next deps change starts a fresh one. Interruption passes
|
|
16
|
+
* through so restarts and unmount stay silent.
|
|
17
|
+
*/
|
|
18
|
+
const quietStream = (cause) => Cause.hasInterruptsOnly(cause) ? Stream.failCause(cause) : Stream.unwrap(Effect.as(Effect.logError(Cause.pretty(cause)), Stream.empty));
|
|
19
|
+
/** Whether `value` is a non-null object. */
|
|
20
|
+
const isObject = (value) => typeof value === "object" && value !== null;
|
|
21
|
+
const isUpdater = (change) => typeof change === "function";
|
|
22
|
+
/** Brand marking step values built by {@link createStep}. */
|
|
23
|
+
const stepMarker = "__ilhaModelStep";
|
|
24
|
+
/** Whether `value` is a step built by {@link createStep}. */
|
|
25
|
+
const isStepValue = (value) => isObject(value) && Object.getOwnPropertyDescriptor(value, stepMarker)?.value === true;
|
|
26
|
+
/** Replace every field listed in `patch`; a function receives the previous value. */
|
|
27
|
+
const applyEvolve = (model, patch) => {
|
|
28
|
+
if (!isObject(model) || Array.isArray(model)) throw new Error("@ilha/model evolve needs an object model");
|
|
29
|
+
const out = { ...model };
|
|
30
|
+
const keys = Object.keys(patch);
|
|
31
|
+
for (const key of keys) {
|
|
32
|
+
const change = patch[key];
|
|
33
|
+
if (change === void 0) continue;
|
|
34
|
+
out[key] = isUpdater(change) ? change(model[key]) : change;
|
|
35
|
+
}
|
|
36
|
+
return out;
|
|
37
|
+
};
|
|
38
|
+
/** Build a step; every builder returns a new step with the change applied. */
|
|
39
|
+
const createStep = (model, commands = [], events = []) => {
|
|
40
|
+
return Object.assign({
|
|
41
|
+
commands,
|
|
42
|
+
emit: (...emitted) => createStep(model, commands, [...events, ...emitted]),
|
|
43
|
+
events,
|
|
44
|
+
evolve: (patch) => createStep(applyEvolve(model, patch), commands, events),
|
|
45
|
+
model,
|
|
46
|
+
run: (...issued) => createStep(model, [...commands, ...issued], events),
|
|
47
|
+
set: (next) => createStep(next, commands, events)
|
|
48
|
+
}, { [stepMarker]: true });
|
|
49
|
+
};
|
|
50
|
+
/** `$` for `init`: the current props, and `set` to build the first model. */
|
|
51
|
+
const initDollar = (props) => ({
|
|
52
|
+
props,
|
|
53
|
+
set: (model) => createStep(model)
|
|
54
|
+
});
|
|
55
|
+
/** `$` for `update`: a step seeded with the current model, plus props. */
|
|
56
|
+
const updateDollar = (model, props) => Object.assign(createStep(model), { props });
|
|
57
|
+
|
|
58
|
+
export { reportCause as a, quietStream as i, initDollar as n, updateDollar as o, isStepValue as r, createStep as t };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// \0rolldown/runtime.js
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __exportAll = (all, no_symbols) => {
|
|
4
|
+
let target = {};
|
|
5
|
+
for (var name in all) {
|
|
6
|
+
__defProp(target, name, {
|
|
7
|
+
get: all[name],
|
|
8
|
+
enumerable: true
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
if (!no_symbols) {
|
|
12
|
+
__defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
13
|
+
}
|
|
14
|
+
return target;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export { __exportAll as t };
|
package/dist/test.d.ts
CHANGED
|
@@ -1,56 +1,32 @@
|
|
|
1
|
-
import
|
|
2
|
-
import * as Layer from "effect/Layer";
|
|
3
|
-
import * as AtomRegistry from "effect/reactivity/AtomRegistry";
|
|
4
|
-
import type { Cmd, ModelApp, UpdateResult } from "./app";
|
|
1
|
+
import { t as ModelComponent } from "./component-BlwL7V1h.js";
|
|
5
2
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
3
|
+
* Synchronous, pure component tests: no Effect, no DOM. Runs `init`
|
|
4
|
+
* (commands are recorded, never executed), then `send` feeds messages
|
|
5
|
+
* through `update`. Step commands accumulate as pending issues;
|
|
6
|
+
* `resolve` drops one and optionally feeds a message back.
|
|
8
7
|
*/
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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;
|
|
8
|
+
export interface StoryHandle<Model, Msg, Events> {
|
|
9
|
+
/** The current model. */
|
|
10
|
+
readonly model: Model;
|
|
11
|
+
/** Emitted events, in order. */
|
|
12
|
+
readonly emitted: readonly Events[];
|
|
13
|
+
/** Pending command issues, in order, as `{ name, args }`. */
|
|
14
|
+
readonly commands: readonly {
|
|
15
|
+
readonly name: string;
|
|
16
|
+
readonly args: unknown;
|
|
17
|
+
}[];
|
|
18
|
+
/** Run one message through `update`. Throws on an unknown tag. */
|
|
19
|
+
readonly send: (message: Msg) => void;
|
|
20
|
+
/**
|
|
21
|
+
* Drop the oldest pending issue of `command`, then send `message` when
|
|
22
|
+
* given (as if that issue resolved with it).
|
|
23
|
+
*/
|
|
24
|
+
readonly resolve: (command: {
|
|
25
|
+
readonly name: string;
|
|
26
|
+
}, message?: Msg) => void;
|
|
45
27
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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 {};
|
|
28
|
+
export declare const Story: {
|
|
29
|
+
make<Props, Events, Model, Msg extends {
|
|
30
|
+
readonly _tag: string;
|
|
31
|
+
}>(component: ModelComponent<Props, Events, Model, Msg>, ...args: Record<never, never> extends Props ? [props?: Props] : [props: Props]): StoryHandle<Model, Msg, Events>;
|
|
32
|
+
};
|