Skip to content

Commit 6de4efe

Browse files
authored
EFF-715 Add Stream.catchIf (#1753)
1 parent 84ba83c commit 6de4efe

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

.changeset/odd-bulldogs-sleep.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect": patch
3+
---
4+
5+
Add dtslint coverage for `Stream.catchIf` to lock in predicate and refinement inference behavior in both data-first and data-last forms.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { Data, pipe, Stream } from "effect"
2+
import { describe, expect, it } from "tstyche"
3+
4+
class ErrorA extends Data.TaggedError("ErrorA")<{
5+
readonly message: string
6+
}> {}
7+
8+
class ErrorB extends Data.TaggedError("ErrorB")<{
9+
readonly code: number
10+
}> {}
11+
12+
declare const stream: Stream.Stream<string, ErrorA | ErrorB, "dep-1">
13+
declare const predicate: (error: ErrorA | ErrorB) => boolean
14+
15+
describe("Stream.catchIf", () => {
16+
it("supports refinement in data-last usage", () => {
17+
const result = pipe(
18+
stream,
19+
Stream.catchIf(
20+
(error): error is ErrorA => error._tag === "ErrorA",
21+
(error) => {
22+
expect(error).type.toBe<ErrorA>()
23+
return Stream.succeed("recovered")
24+
}
25+
)
26+
)
27+
expect(result).type.toBe<Stream.Stream<string, ErrorB, "dep-1">>()
28+
})
29+
30+
it("supports refinement with orElse", () => {
31+
const result = pipe(
32+
stream,
33+
Stream.catchIf(
34+
(error): error is ErrorA => error._tag === "ErrorA",
35+
() => Stream.succeed(1),
36+
(error) => {
37+
expect(error).type.toBe<ErrorB>()
38+
return Stream.succeed(2)
39+
}
40+
)
41+
)
42+
expect(result).type.toBe<Stream.Stream<string | number, never, "dep-1">>()
43+
})
44+
45+
it("supports predicate in data-first usage", () => {
46+
const result = Stream.catchIf(
47+
stream,
48+
predicate,
49+
(error) => {
50+
expect(error).type.toBe<ErrorA | ErrorB>()
51+
return Stream.succeed(0)
52+
}
53+
)
54+
expect(result).type.toBe<Stream.Stream<string | number, ErrorA | ErrorB, "dep-1">>()
55+
})
56+
})

0 commit comments

Comments
 (0)