@@ -9,22 +9,41 @@ struct FactClient {
9
9
struct Error : Swift . Error , Equatable { }
10
10
}
11
11
12
- extension FactClient {
13
12
// This is the "live" fact dependency that reaches into the outside world to fetch trivia.
14
13
// Typically this live implementation of the dependency would live in its own module so that the
15
14
// main feature doesn't need to compile it.
15
+ extension FactClient {
16
+ #if compiler(>=5.5)
16
17
static let live = Self (
17
18
fetch: { number in
18
- URLSession . shared. reactive. data (
19
- with: URLRequest ( url: URL ( string: " http://numbersapi.com/ \( number) /trivia " ) !)
20
- )
21
- . map { data, _ in String ( decoding: data, as: UTF8 . self) }
22
- . flatMapError { _ in
23
- Effect ( value: " \( number) is a good number Brent " )
24
- . delay ( 1 , on: QueueScheduler . main)
19
+ Effect . task {
20
+ do {
21
+ let ( data, _) = try await URLSession . shared
22
+ . data ( from: URL ( string: " http://numbersapi.com/ \( number) /trivia " ) !)
23
+ return String ( decoding: data, as: UTF8 . self)
24
+ } catch {
25
+ await Task . sleep ( NSEC_PER_SEC)
26
+ return " \( number) is a good number Brent "
27
+ }
28
+ }
29
+ . setFailureType ( to: Error . self)
30
+ . eraseToEffect ( )
25
31
}
26
- . promoteError ( Error . self)
27
- } )
32
+ )
33
+ #else
34
+ static let live = Self (
35
+ fetch: { number in
36
+ URLSession . shared. reactive. data (
37
+ with: URLRequest ( url: URL ( string: " http://numbersapi.com/ \( number) /trivia " ) !)
38
+ )
39
+ . map { data, _ in String ( decoding: data, as: UTF8 . self) }
40
+ . flatMapError { _ in
41
+ Effect ( value: " \( number) is a good number Brent " )
42
+ . delay ( 1 , on: QueueScheduler . main)
43
+ }
44
+ . promoteError ( Error . self)
45
+ } )
46
+ #endif
28
47
}
29
48
30
49
#if DEBUG
0 commit comments