@effect-app/vue 4.0.0-beta.22 → 4.0.0-beta.220
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/CHANGELOG.md +1605 -0
- package/dist/commander.d.ts +628 -0
- package/dist/commander.d.ts.map +1 -0
- package/dist/commander.js +1060 -0
- package/dist/confirm.d.ts +19 -0
- package/dist/confirm.d.ts.map +1 -0
- package/dist/confirm.js +24 -0
- package/dist/errorReporter.d.ts +4 -4
- package/dist/errorReporter.d.ts.map +1 -1
- package/dist/errorReporter.js +12 -18
- package/dist/form.d.ts +14 -5
- package/dist/form.d.ts.map +1 -1
- package/dist/form.js +41 -12
- package/dist/index.d.ts +1 -1
- package/dist/intl.d.ts +15 -0
- package/dist/intl.d.ts.map +1 -0
- package/dist/intl.js +9 -0
- package/dist/lib.d.ts +6 -9
- package/dist/lib.d.ts.map +1 -1
- package/dist/lib.js +35 -10
- package/dist/makeClient.d.ts +152 -339
- package/dist/makeClient.d.ts.map +1 -1
- package/dist/makeClient.js +221 -376
- package/dist/makeContext.d.ts +1 -1
- package/dist/makeContext.d.ts.map +1 -1
- package/dist/makeIntl.d.ts +1 -1
- package/dist/makeIntl.d.ts.map +1 -1
- package/dist/makeUseCommand.d.ts +8 -0
- package/dist/makeUseCommand.d.ts.map +1 -0
- package/dist/makeUseCommand.js +13 -0
- package/dist/mutate.d.ts +52 -34
- package/dist/mutate.d.ts.map +1 -1
- package/dist/mutate.js +137 -46
- package/dist/query.d.ts +19 -39
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +128 -72
- package/dist/routeParams.d.ts +1 -1
- package/dist/runtime.d.ts +7 -4
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +27 -17
- package/dist/toast.d.ts +50 -0
- package/dist/toast.d.ts.map +1 -0
- package/dist/toast.js +32 -0
- package/dist/withToast.d.ts +27 -0
- package/dist/withToast.d.ts.map +1 -0
- package/dist/withToast.js +59 -0
- package/examples/streamMutation.ts +70 -0
- package/package.json +48 -50
- package/src/commander.ts +3393 -0
- package/src/{experimental/confirm.ts → confirm.ts} +10 -14
- package/src/errorReporter.ts +62 -74
- package/src/form.ts +56 -17
- package/src/intl.ts +12 -0
- package/src/lib.ts +47 -20
- package/src/makeClient.ts +568 -1134
- package/src/{experimental/makeUseCommand.ts → makeUseCommand.ts} +6 -4
- package/src/mutate.ts +265 -127
- package/src/query.ts +197 -183
- package/src/runtime.ts +41 -20
- package/src/{experimental/toast.ts → toast.ts} +13 -27
- package/src/{experimental/withToast.ts → withToast.ts} +40 -12
- package/test/Mutation.test.ts +176 -23
- package/test/dist/form.test.d.ts.map +1 -1
- package/test/dist/lib.test.d.ts.map +1 -0
- package/test/dist/streamFinal.test.d.ts.map +1 -0
- package/test/dist/streamFn.test.d.ts.map +1 -0
- package/test/dist/stubs.d.ts +3529 -122
- package/test/dist/stubs.d.ts.map +1 -1
- package/test/dist/stubs.js +182 -31
- package/test/form-validation-errors.test.ts +23 -19
- package/test/form.test.ts +20 -2
- package/test/lib.test.ts +240 -0
- package/test/makeClient.test.ts +292 -38
- package/test/streamFinal.test.ts +63 -0
- package/test/streamFn.test.ts +455 -0
- package/test/stubs.ts +218 -42
- package/tsconfig.examples.json +20 -0
- package/tsconfig.json +0 -1
- package/tsconfig.json.bak +5 -2
- package/tsconfig.src.json +34 -34
- package/tsconfig.test.json +2 -2
- package/vitest.config.ts +5 -5
- package/dist/experimental/commander.d.ts +0 -359
- package/dist/experimental/commander.d.ts.map +0 -1
- package/dist/experimental/commander.js +0 -557
- package/dist/experimental/confirm.d.ts +0 -19
- package/dist/experimental/confirm.d.ts.map +0 -1
- package/dist/experimental/confirm.js +0 -28
- package/dist/experimental/intl.d.ts +0 -16
- package/dist/experimental/intl.d.ts.map +0 -1
- package/dist/experimental/intl.js +0 -5
- package/dist/experimental/makeUseCommand.d.ts +0 -8
- package/dist/experimental/makeUseCommand.d.ts.map +0 -1
- package/dist/experimental/makeUseCommand.js +0 -13
- package/dist/experimental/toast.d.ts +0 -47
- package/dist/experimental/toast.d.ts.map +0 -1
- package/dist/experimental/toast.js +0 -41
- package/dist/experimental/withToast.d.ts +0 -25
- package/dist/experimental/withToast.d.ts.map +0 -1
- package/dist/experimental/withToast.js +0 -45
- package/eslint.config.mjs +0 -24
- package/src/experimental/commander.ts +0 -1835
- package/src/experimental/intl.ts +0 -9
|
@@ -0,0 +1,455 @@
|
|
|
1
|
+
import { expect, it } from "@effect/vitest"
|
|
2
|
+
import { Deferred, Effect, Fiber } from "effect-app"
|
|
3
|
+
import * as Stream from "effect/Stream"
|
|
4
|
+
import { TestClock } from "effect/testing"
|
|
5
|
+
import { CommanderStatic } from "../src/commander.js"
|
|
6
|
+
import { AsyncResult } from "../src/lib.js"
|
|
7
|
+
import { useExperimental, useExperimentalE } from "./stubs.js"
|
|
8
|
+
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
// Helpers
|
|
11
|
+
// ---------------------------------------------------------------------------
|
|
12
|
+
|
|
13
|
+
/** Wait for the fiber spawned by `cmd.handle()` to finish. */
|
|
14
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
15
|
+
const join = (fiber: Fiber.Fiber<any, any>) => Fiber.join(fiber)
|
|
16
|
+
|
|
17
|
+
// ---------------------------------------------------------------------------
|
|
18
|
+
// Non-generator form — (arg) => Stream
|
|
19
|
+
// ---------------------------------------------------------------------------
|
|
20
|
+
|
|
21
|
+
it.live("streamFn: non-generator returning Stream runs stream and updates result", () =>
|
|
22
|
+
Effect.gen(function*() {
|
|
23
|
+
const Command = useExperimental({ toasts: [] })
|
|
24
|
+
|
|
25
|
+
const cmd = Command.streamFn("test-stream-plain")(
|
|
26
|
+
(_arg: number) => Stream.make(10, 20, 30)
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
expect(cmd.waiting).toBe(false)
|
|
30
|
+
yield* join(cmd.handle(1))
|
|
31
|
+
|
|
32
|
+
expect(cmd.result._tag).toBe("Success")
|
|
33
|
+
if (cmd.result._tag === "Success") {
|
|
34
|
+
expect(cmd.result.value).toBe(30)
|
|
35
|
+
expect(cmd.result.waiting).toBe(false)
|
|
36
|
+
}
|
|
37
|
+
}))
|
|
38
|
+
|
|
39
|
+
// ---------------------------------------------------------------------------
|
|
40
|
+
// Generator form — function*(arg) { yield* effect; return Stream }
|
|
41
|
+
// ---------------------------------------------------------------------------
|
|
42
|
+
|
|
43
|
+
it.live("streamFn: generator form executes yielded effects and subscribes to returned stream", () =>
|
|
44
|
+
Effect.gen(function*() {
|
|
45
|
+
const Command = useExperimental({ toasts: [] })
|
|
46
|
+
|
|
47
|
+
let generatorBodyExecuted = false
|
|
48
|
+
|
|
49
|
+
const cmd = Command.streamFn("test-stream-gen")(
|
|
50
|
+
function*(arg: number) {
|
|
51
|
+
generatorBodyExecuted = true
|
|
52
|
+
const doubled = yield* Effect.succeed(arg * 2)
|
|
53
|
+
return Stream.make(doubled, doubled + 1, doubled + 2)
|
|
54
|
+
}
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
yield* join(cmd.handle(5))
|
|
58
|
+
|
|
59
|
+
// Generator body MUST have run
|
|
60
|
+
expect(generatorBodyExecuted).toBe(true)
|
|
61
|
+
|
|
62
|
+
// Stream emitted three values: 10, 11, 12; last one should be in result
|
|
63
|
+
expect(cmd.result._tag).toBe("Success")
|
|
64
|
+
if (cmd.result._tag === "Success") {
|
|
65
|
+
expect(cmd.result.value).toBe(12)
|
|
66
|
+
expect(cmd.result.waiting).toBe(false)
|
|
67
|
+
}
|
|
68
|
+
}))
|
|
69
|
+
|
|
70
|
+
// ---------------------------------------------------------------------------
|
|
71
|
+
// Generator form with async effect (Effect.promise)
|
|
72
|
+
// ---------------------------------------------------------------------------
|
|
73
|
+
|
|
74
|
+
it.live("streamFn: generator form with async Effect.promise works", () =>
|
|
75
|
+
Effect.gen(function*() {
|
|
76
|
+
const Command = useExperimental({ toasts: [] })
|
|
77
|
+
|
|
78
|
+
let asyncValueReceived: string | undefined
|
|
79
|
+
|
|
80
|
+
const cmd = Command.streamFn("test-stream-gen-async")(
|
|
81
|
+
function*(arg: string) {
|
|
82
|
+
// Simulate the pattern from the bug report: yield* Effect.promise(...)
|
|
83
|
+
const result = yield* Effect.promise(() => Promise.resolve(`processed:${arg}`))
|
|
84
|
+
asyncValueReceived = result
|
|
85
|
+
return Stream.make(result, result + "!")
|
|
86
|
+
}
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
yield* join(cmd.handle("hello"))
|
|
90
|
+
|
|
91
|
+
expect(asyncValueReceived).toBe("processed:hello")
|
|
92
|
+
expect(cmd.result._tag).toBe("Success")
|
|
93
|
+
if (cmd.result._tag === "Success") {
|
|
94
|
+
expect(cmd.result.value).toBe("processed:hello!")
|
|
95
|
+
}
|
|
96
|
+
}))
|
|
97
|
+
|
|
98
|
+
// ---------------------------------------------------------------------------
|
|
99
|
+
// Non-generator returning Effect<Stream>
|
|
100
|
+
// ---------------------------------------------------------------------------
|
|
101
|
+
|
|
102
|
+
it.live("streamFn: non-generator returning Effect<Stream> runs stream", () =>
|
|
103
|
+
Effect.gen(function*() {
|
|
104
|
+
const Command = useExperimental({ toasts: [] })
|
|
105
|
+
|
|
106
|
+
const cmd = Command.streamFn("test-stream-effect-stream")(
|
|
107
|
+
(arg: number) => Effect.succeed(Stream.make(arg * 3, arg * 3 + 1))
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
yield* join(cmd.handle(4))
|
|
111
|
+
|
|
112
|
+
expect(cmd.result._tag).toBe("Success")
|
|
113
|
+
if (cmd.result._tag === "Success") {
|
|
114
|
+
expect(cmd.result.value).toBe(13) // 4*3+1 = 13
|
|
115
|
+
}
|
|
116
|
+
}))
|
|
117
|
+
|
|
118
|
+
// ---------------------------------------------------------------------------
|
|
119
|
+
// Generator form — waiting state flips correctly
|
|
120
|
+
// ---------------------------------------------------------------------------
|
|
121
|
+
|
|
122
|
+
it.live("streamFn: generator form sets waiting=true during execution then false after", () =>
|
|
123
|
+
Effect.gen(function*() {
|
|
124
|
+
const Command = useExperimental({ toasts: [] })
|
|
125
|
+
|
|
126
|
+
const cmd = Command.streamFn("test-stream-gen-waiting")(
|
|
127
|
+
function*(_arg: void) {
|
|
128
|
+
return Stream.make(1, 2, 3)
|
|
129
|
+
}
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
expect(cmd.waiting).toBe(false)
|
|
133
|
+
const fiber = cmd.handle()
|
|
134
|
+
|
|
135
|
+
// result transitions to initial(true) = waiting synchronously inside runStream
|
|
136
|
+
// after the fiber runs, waiting should settle to false
|
|
137
|
+
yield* join(fiber)
|
|
138
|
+
expect(cmd.waiting).toBe(false)
|
|
139
|
+
|
|
140
|
+
expect(AsyncResult.isSuccess(cmd.result)).toBe(true)
|
|
141
|
+
}))
|
|
142
|
+
|
|
143
|
+
// ---------------------------------------------------------------------------
|
|
144
|
+
// Generator form with a stream-transformer combinator
|
|
145
|
+
// ---------------------------------------------------------------------------
|
|
146
|
+
|
|
147
|
+
it.live("streamFn: generator form with combinator — combinator transforms the stream", () =>
|
|
148
|
+
Effect.gen(function*() {
|
|
149
|
+
const Command = useExperimental({ toasts: [] })
|
|
150
|
+
|
|
151
|
+
const emittedByCombinator: number[] = []
|
|
152
|
+
|
|
153
|
+
// A combinator that records each element it sees.
|
|
154
|
+
// The first argument may be a Stream or an Effect<Stream> (for generator-form handlers),
|
|
155
|
+
// matching how withDefaultToastStream handles it.
|
|
156
|
+
const spyCombinator = (
|
|
157
|
+
input: Stream.Stream<number, never, never> | Effect.Effect<Stream.Stream<number, never, never>>
|
|
158
|
+
) => {
|
|
159
|
+
const stream: Stream.Stream<number, never, never> = Stream.isStream(input)
|
|
160
|
+
? input
|
|
161
|
+
: Stream.unwrap(input as Effect.Effect<Stream.Stream<number, never, never>>)
|
|
162
|
+
return stream.pipe(
|
|
163
|
+
Stream.tap((v) =>
|
|
164
|
+
Effect.sync(() => {
|
|
165
|
+
emittedByCombinator.push(v)
|
|
166
|
+
})
|
|
167
|
+
)
|
|
168
|
+
)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const cmd = Command.streamFn("test-stream-gen-combinator")(
|
|
172
|
+
function*(arg: number) {
|
|
173
|
+
const base = yield* Effect.succeed(arg * 10)
|
|
174
|
+
return Stream.make(base, base + 1, base + 2)
|
|
175
|
+
},
|
|
176
|
+
// combinator receives (input, arg, ctx) — input is Stream or Effect<Stream> depending on handler form
|
|
177
|
+
(input: Stream.Stream<number, never, never> | Effect.Effect<Stream.Stream<number, never, never>>) =>
|
|
178
|
+
spyCombinator(input)
|
|
179
|
+
)
|
|
180
|
+
|
|
181
|
+
yield* join(cmd.handle(3))
|
|
182
|
+
|
|
183
|
+
// combinator must have seen all elements: 30, 31, 32
|
|
184
|
+
expect(emittedByCombinator).toEqual([30, 31, 32])
|
|
185
|
+
|
|
186
|
+
expect(cmd.result._tag).toBe("Success")
|
|
187
|
+
if (cmd.result._tag === "Success") {
|
|
188
|
+
expect(cmd.result.value).toBe(32)
|
|
189
|
+
}
|
|
190
|
+
}))
|
|
191
|
+
|
|
192
|
+
// ---------------------------------------------------------------------------
|
|
193
|
+
// Generator form — Stream.ensuring runs after stream completes
|
|
194
|
+
// ---------------------------------------------------------------------------
|
|
195
|
+
|
|
196
|
+
it.live("streamFn: generator form Stream.ensuring cleanup runs after stream ends", () =>
|
|
197
|
+
Effect.gen(function*() {
|
|
198
|
+
const Command = useExperimental({ toasts: [] })
|
|
199
|
+
|
|
200
|
+
let cleanupRan = false
|
|
201
|
+
|
|
202
|
+
const cmd = Command.streamFn("test-stream-gen-ensuring")(
|
|
203
|
+
function*(arg: number) {
|
|
204
|
+
const value = yield* Effect.succeed(arg + 100)
|
|
205
|
+
return Stream.make(value).pipe(
|
|
206
|
+
Stream.ensuring(Effect.sync(() => {
|
|
207
|
+
cleanupRan = true
|
|
208
|
+
}))
|
|
209
|
+
)
|
|
210
|
+
}
|
|
211
|
+
)
|
|
212
|
+
|
|
213
|
+
yield* join(cmd.handle(7))
|
|
214
|
+
|
|
215
|
+
expect(cleanupRan).toBe(true)
|
|
216
|
+
expect(cmd.result._tag).toBe("Success")
|
|
217
|
+
if (cmd.result._tag === "Success") {
|
|
218
|
+
expect(cmd.result.value).toBe(107)
|
|
219
|
+
}
|
|
220
|
+
}))
|
|
221
|
+
|
|
222
|
+
// ---------------------------------------------------------------------------
|
|
223
|
+
// Command.mapProgress — updates progress ref for each element
|
|
224
|
+
// ---------------------------------------------------------------------------
|
|
225
|
+
|
|
226
|
+
it.live("streamFn: Command.mapProgress updates progress ref for each stream element", () =>
|
|
227
|
+
Effect.gen(function*() {
|
|
228
|
+
const Command = useExperimental({ toasts: [] })
|
|
229
|
+
|
|
230
|
+
const cmd = Command.streamFn("test-map-progress")(
|
|
231
|
+
function*(_arg: void) {
|
|
232
|
+
return Stream.make(1, 2, 3).pipe(
|
|
233
|
+
CommanderStatic.mapProgress((r) =>
|
|
234
|
+
AsyncResult.isSuccess(r)
|
|
235
|
+
? { text: `item-${r.value}`, percentage: r.value * 10 }
|
|
236
|
+
: undefined
|
|
237
|
+
)
|
|
238
|
+
)
|
|
239
|
+
}
|
|
240
|
+
)
|
|
241
|
+
|
|
242
|
+
// progress starts undefined (reactive unwraps the ref)
|
|
243
|
+
expect(cmd.progress).toBeUndefined()
|
|
244
|
+
|
|
245
|
+
yield* join(cmd.handle())
|
|
246
|
+
|
|
247
|
+
// after stream drains, last mapped progress value should be set
|
|
248
|
+
expect(cmd.progress).toEqual({ text: "item-3", percentage: 30 })
|
|
249
|
+
}))
|
|
250
|
+
|
|
251
|
+
// ---------------------------------------------------------------------------
|
|
252
|
+
// Command.updateProgress — imperative progress update from stream
|
|
253
|
+
// ---------------------------------------------------------------------------
|
|
254
|
+
|
|
255
|
+
it.live("streamFn: Command.updateProgress imperatively drives the progress ref", () =>
|
|
256
|
+
Effect.gen(function*() {
|
|
257
|
+
const Command = useExperimental({ toasts: [] })
|
|
258
|
+
|
|
259
|
+
const cmd = Command.streamFn("test-update-progress")(
|
|
260
|
+
function*(_arg: void) {
|
|
261
|
+
return Stream.make("a", "b").pipe(
|
|
262
|
+
Stream.tap((v) => CommanderStatic.updateProgress(`processing ${v}`))
|
|
263
|
+
)
|
|
264
|
+
}
|
|
265
|
+
)
|
|
266
|
+
|
|
267
|
+
expect(cmd.progress).toBeUndefined()
|
|
268
|
+
yield* join(cmd.handle())
|
|
269
|
+
|
|
270
|
+
expect(cmd.progress).toBe("processing b")
|
|
271
|
+
}))
|
|
272
|
+
|
|
273
|
+
// ---------------------------------------------------------------------------
|
|
274
|
+
// Command.withDefaultToastStream — in-progress (waiting) initial toast
|
|
275
|
+
// ---------------------------------------------------------------------------
|
|
276
|
+
|
|
277
|
+
it.effect("withDefaultToastStream: shows info toast while stream is running", () =>
|
|
278
|
+
Effect.gen(function*() {
|
|
279
|
+
const toasts: any[] = []
|
|
280
|
+
const Command = yield* useExperimentalE({ toasts, messages: { "handle.waiting": "{action} waiting…" } })
|
|
281
|
+
|
|
282
|
+
// Gate that holds the stream open past the 1s waiting-toast delay so we can
|
|
283
|
+
// observe the info toast while the stream is still in-flight.
|
|
284
|
+
const resume = yield* Deferred.make<void>()
|
|
285
|
+
|
|
286
|
+
const cmd = Command.streamFn("doWork")(
|
|
287
|
+
function*(_arg: void) {
|
|
288
|
+
return Stream.make(1).pipe(
|
|
289
|
+
Stream.tap(() => Deferred.await(resume))
|
|
290
|
+
)
|
|
291
|
+
},
|
|
292
|
+
Command.withDefaultToastStream()
|
|
293
|
+
)
|
|
294
|
+
|
|
295
|
+
const fiber = cmd.handle()
|
|
296
|
+
|
|
297
|
+
// Advance virtual time past the 1s waiting-toast delay; the stream is still
|
|
298
|
+
// paused on `resume`, so the info toast must have surfaced.
|
|
299
|
+
yield* TestClock.adjust("1100 millis")
|
|
300
|
+
|
|
301
|
+
expect(toasts.some((t) => t.type === "info")).toBe(true)
|
|
302
|
+
const infoToast = toasts.find((t) => t.type === "info")
|
|
303
|
+
expect(infoToast.message).toContain("doWork")
|
|
304
|
+
|
|
305
|
+
// Let the stream finish.
|
|
306
|
+
yield* Deferred.succeed(resume, undefined)
|
|
307
|
+
yield* join(fiber)
|
|
308
|
+
|
|
309
|
+
// After completion the same toast slot is replaced with a success toast.
|
|
310
|
+
expect(toasts.some((t) => t.type === "success")).toBe(true)
|
|
311
|
+
}))
|
|
312
|
+
|
|
313
|
+
// ---------------------------------------------------------------------------
|
|
314
|
+
// Command.withDefaultToastStream — fast streams skip the waiting toast
|
|
315
|
+
// ---------------------------------------------------------------------------
|
|
316
|
+
|
|
317
|
+
it.live("withDefaultToastStream: fast stream finishes before delay → no waiting toast", () =>
|
|
318
|
+
Effect.gen(function*() {
|
|
319
|
+
const toasts: any[] = []
|
|
320
|
+
const Command = useExperimental({ toasts, messages: { "handle.waiting": "{action} waiting…" } })
|
|
321
|
+
|
|
322
|
+
const cmd = Command.streamFn("doWorkFast")(
|
|
323
|
+
function*(_arg: void) {
|
|
324
|
+
return Stream.make(1, 2, 3)
|
|
325
|
+
},
|
|
326
|
+
Command.withDefaultToastStream()
|
|
327
|
+
)
|
|
328
|
+
|
|
329
|
+
yield* join(cmd.handle())
|
|
330
|
+
|
|
331
|
+
// Stream completed instantly; the 1s waiting fiber must have been interrupted before emitting an info toast.
|
|
332
|
+
expect(toasts.some((t) => t.type === "info")).toBe(false)
|
|
333
|
+
expect(toasts.some((t) => t.type === "success")).toBe(true)
|
|
334
|
+
}))
|
|
335
|
+
|
|
336
|
+
// ---------------------------------------------------------------------------
|
|
337
|
+
// Command.withDefaultToastStream — progress text/percent updates the toast
|
|
338
|
+
// ---------------------------------------------------------------------------
|
|
339
|
+
|
|
340
|
+
it.live("withDefaultToastStream: progress option updates waiting toast message", () =>
|
|
341
|
+
Effect.gen(function*() {
|
|
342
|
+
const toasts: any[] = []
|
|
343
|
+
const Command = useExperimental({
|
|
344
|
+
toasts,
|
|
345
|
+
messages: { "handle.waiting": "Working…", "handle.success": "{action} done" }
|
|
346
|
+
})
|
|
347
|
+
|
|
348
|
+
const progressSnapshots: string[] = []
|
|
349
|
+
|
|
350
|
+
const cmd = Command.streamFn("doWorkProgress")(
|
|
351
|
+
function*(_arg: void) {
|
|
352
|
+
return Stream.make(10, 50, 100).pipe(
|
|
353
|
+
Stream.tap((pct) =>
|
|
354
|
+
Effect.sync(() => {
|
|
355
|
+
progressSnapshots.push(`${pct}%`)
|
|
356
|
+
})
|
|
357
|
+
)
|
|
358
|
+
)
|
|
359
|
+
},
|
|
360
|
+
Command.withDefaultToastStream({
|
|
361
|
+
progress: (r) =>
|
|
362
|
+
AsyncResult.isSuccess(r)
|
|
363
|
+
? { text: `${r.value}%`, percentage: r.value }
|
|
364
|
+
: undefined
|
|
365
|
+
})
|
|
366
|
+
)
|
|
367
|
+
|
|
368
|
+
yield* join(cmd.handle())
|
|
369
|
+
|
|
370
|
+
// All three stream elements were visited by the tap above
|
|
371
|
+
expect(progressSnapshots).toEqual(["10%", "50%", "100%"])
|
|
372
|
+
|
|
373
|
+
// cmd.progress reflects the last mapped value (reactive unwraps the ref)
|
|
374
|
+
expect(cmd.progress).toEqual({ text: "100%", percentage: 100 })
|
|
375
|
+
|
|
376
|
+
// A success toast should appear after the stream completes
|
|
377
|
+
expect(toasts.some((t) => t.type === "success")).toBe(true)
|
|
378
|
+
}))
|
|
379
|
+
|
|
380
|
+
// ---------------------------------------------------------------------------
|
|
381
|
+
// Command.withDefaultToastStream — failure shows warning/error toast
|
|
382
|
+
// ---------------------------------------------------------------------------
|
|
383
|
+
|
|
384
|
+
it.live("withDefaultToastStream: failure shows failure toast, not success toast", () =>
|
|
385
|
+
Effect.gen(function*() {
|
|
386
|
+
const toasts: any[] = []
|
|
387
|
+
const Command = useExperimental({ toasts })
|
|
388
|
+
|
|
389
|
+
class BoomError {
|
|
390
|
+
readonly _tag = "BoomError"
|
|
391
|
+
readonly message = "boom"
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
const cmd = Command.streamFn("doWorkFail")(
|
|
395
|
+
function*(_arg: void) {
|
|
396
|
+
return Stream.fail(new BoomError())
|
|
397
|
+
},
|
|
398
|
+
Command.withDefaultToastStream()
|
|
399
|
+
)
|
|
400
|
+
|
|
401
|
+
yield* join(cmd.handle())
|
|
402
|
+
|
|
403
|
+
// Typed errors → withDefaultToastStream calls toast.warning (level: "warn")
|
|
404
|
+
expect(toasts.some((t) => t.type === "warning" || t.type === "error")).toBe(true)
|
|
405
|
+
expect(toasts.some((t) => t.type === "success")).toBe(false)
|
|
406
|
+
}))
|
|
407
|
+
|
|
408
|
+
// ---------------------------------------------------------------------------
|
|
409
|
+
// Command.withDefaultToastStream — die (defect) shows error toast
|
|
410
|
+
// ---------------------------------------------------------------------------
|
|
411
|
+
|
|
412
|
+
it.live("withDefaultToastStream: die/defect shows error toast, not warning or success", () =>
|
|
413
|
+
Effect.gen(function*() {
|
|
414
|
+
const toasts: any[] = []
|
|
415
|
+
const Command = useExperimental({ toasts })
|
|
416
|
+
|
|
417
|
+
const cmd = Command.streamFn("doWorkDie")(
|
|
418
|
+
function*(_arg: void) {
|
|
419
|
+
// Stream.die produces a defect — Cause.findErrorOption returns Option.none()
|
|
420
|
+
// so defaultFailureMessageHandler returns a plain string → toast.error
|
|
421
|
+
return Stream.die(new Error("unexpected defect"))
|
|
422
|
+
},
|
|
423
|
+
Command.withDefaultToastStream()
|
|
424
|
+
)
|
|
425
|
+
|
|
426
|
+
yield* join(cmd.handle())
|
|
427
|
+
|
|
428
|
+
expect(toasts.some((t) => t.type === "error")).toBe(true)
|
|
429
|
+
expect(toasts.some((t) => t.type === "warning")).toBe(false)
|
|
430
|
+
expect(toasts.some((t) => t.type === "success")).toBe(false)
|
|
431
|
+
}))
|
|
432
|
+
|
|
433
|
+
// ---------------------------------------------------------------------------
|
|
434
|
+
// Command.withDefaultToastStream — success shows success toast
|
|
435
|
+
// ---------------------------------------------------------------------------
|
|
436
|
+
|
|
437
|
+
it.live("withDefaultToastStream: success shows success toast after stream drains", () =>
|
|
438
|
+
Effect.gen(function*() {
|
|
439
|
+
const toasts: any[] = []
|
|
440
|
+
const Command = useExperimental({ toasts, messages: { "handle.success": "{action} complete" } })
|
|
441
|
+
|
|
442
|
+
const cmd = Command.streamFn("doWorkSuccess")(
|
|
443
|
+
function*(_arg: void) {
|
|
444
|
+
return Stream.make(42)
|
|
445
|
+
},
|
|
446
|
+
Command.withDefaultToastStream()
|
|
447
|
+
)
|
|
448
|
+
|
|
449
|
+
yield* join(cmd.handle())
|
|
450
|
+
|
|
451
|
+
const successToast = toasts.find((t) => t.type === "success")
|
|
452
|
+
expect(successToast).toBeDefined()
|
|
453
|
+
expect(successToast.message).toContain("doWorkSuccess")
|
|
454
|
+
expect(toasts.some((t) => t.type === "error")).toBe(false)
|
|
455
|
+
}))
|