Skip to content

Commit b00cd6a

Browse files
committed
Simplify block with Result extension
1 parent f2679ea commit b00cd6a

File tree

5 files changed

+37
-29
lines changed

5 files changed

+37
-29
lines changed

WooFoundation/WooFoundation.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
9FA5113235035AC9A6079B0D /* Pods_WooFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C1733C61561AE3A1AD3C16B7 /* Pods_WooFoundation.framework */; };
3333
AE948D0A28CF67CF009F3246 /* Date+StartAndEnd.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE948D0928CF67CE009F3246 /* Date+StartAndEnd.swift */; };
3434
AE948D0D28CF6D50009F3246 /* DateStartAndEndTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE948D0C28CF6D50009F3246 /* DateStartAndEndTests.swift */; };
35+
B97190D1292CF3BC0065E413 /* Result+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = B97190D0292CF3BC0065E413 /* Result+Extensions.swift */; };
3536
B987B06F284540D300C53CF6 /* CurrencyCode.swift in Sources */ = {isa = PBXBuildFile; fileRef = B987B06E284540D300C53CF6 /* CurrencyCode.swift */; };
3637
B9C9C63F283E703C001B879F /* WooFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B9C9C635283E703C001B879F /* WooFoundation.framework */; };
3738
B9C9C659283E7195001B879F /* NSDecimalNumber+Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9C9C658283E7195001B879F /* NSDecimalNumber+Helpers.swift */; };
@@ -79,6 +80,7 @@
7980
A21D73D352B4162AB096E276 /* Pods-WooFoundationTests.release-alpha.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-WooFoundationTests.release-alpha.xcconfig"; path = "Target Support Files/Pods-WooFoundationTests/Pods-WooFoundationTests.release-alpha.xcconfig"; sourceTree = "<group>"; };
8081
AE948D0928CF67CE009F3246 /* Date+StartAndEnd.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Date+StartAndEnd.swift"; sourceTree = "<group>"; };
8182
AE948D0C28CF6D50009F3246 /* DateStartAndEndTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DateStartAndEndTests.swift; sourceTree = "<group>"; };
83+
B97190D0292CF3BC0065E413 /* Result+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Result+Extensions.swift"; sourceTree = "<group>"; };
8284
B987B06E284540D300C53CF6 /* CurrencyCode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CurrencyCode.swift; sourceTree = "<group>"; };
8385
B9AED558283E7553002A2668 /* Yosemite.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = Yosemite.framework; sourceTree = BUILT_PRODUCTS_DIR; };
8486
B9AED55B283E755A002A2668 /* Hardware.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = Hardware.framework; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -246,6 +248,7 @@
246248
B9C9C658283E7195001B879F /* NSDecimalNumber+Helpers.swift */,
247249
68FBC5B228926B2C00A05461 /* Collection+Extensions.swift */,
248250
03B8C3882914083F002235B1 /* Bundle+Woo.swift */,
251+
B97190D0292CF3BC0065E413 /* Result+Extensions.swift */,
249252
);
250253
path = Extensions;
251254
sourceTree = "<group>";
@@ -480,6 +483,7 @@
480483
03597A9B28F87BFC005E4A98 /* WooCommerceComUTMProvider.swift in Sources */,
481484
B9C9C663283E7296001B879F /* Logging.swift in Sources */,
482485
6874E81428998AD300074A97 /* LogErrorAndExit.swift in Sources */,
486+
B97190D1292CF3BC0065E413 /* Result+Extensions.swift in Sources */,
483487
B9C9C65D283E71C8001B879F /* CurrencyFormatter.swift in Sources */,
484488
AE948D0A28CF67CF009F3246 /* Date+StartAndEnd.swift in Sources */,
485489
03597A9428F85686005E4A98 /* UTMParameters.swift in Sources */,
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
extension Result where Failure == Error {
2+
/// Initializes asyncrhonously a `Result` with a `async throws` closure returning a object of the specified type
3+
///
4+
public init(catching body: () async throws -> Success) async {
5+
do {
6+
let value = try await body()
7+
self = .success(value)
8+
} catch {
9+
self = .failure(error)
10+
}
11+
}
12+
}

Yosemite/Yosemite/Stores/DomainStore.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Foundation
22
import Networking
3+
import WooFoundation
34
import protocol Storage.StorageManagerType
45

56
/// Handles `DomainAction`.
@@ -39,12 +40,8 @@ public final class DomainStore: Store {
3940
private extension DomainStore {
4041
func loadFreeDomainSuggestions(query: String, completion: @escaping (Result<[FreeDomainSuggestion], Error>) -> Void) {
4142
Task { @MainActor in
42-
do {
43-
let suggestions = try await remote.loadFreeDomainSuggestions(query: query)
44-
completion(.success(suggestions))
45-
} catch {
46-
completion(.failure(error))
47-
}
43+
let result = await Result { try await remote.loadFreeDomainSuggestions(query: query) }
44+
completion(result)
4845
}
4946
}
5047
}

Yosemite/Yosemite/Stores/JustInTimeMessageStore.swift

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Foundation
22
import Storage
33
import Networking
4+
import WooFoundation
45

56
// MARK: - JustInTimeMessageStore
67
//
@@ -46,22 +47,20 @@ private extension JustInTimeMessageStore {
4647
hook: JustInTimeMessageHook,
4748
completion: @escaping (Result<[JustInTimeMessage], Error>) -> ()) {
4849
Task {
49-
do {
50-
let result = try await remote.loadAllJustInTimeMessages(
50+
let result = await Result {
51+
let messages = try await remote.loadAllJustInTimeMessages(
5152
for: siteID,
5253
messagePath: .init(app: .wooMobile,
5354
screen: screen,
5455
hook: hook),
5556
query: justInTimeMessageQuery(),
5657
locale: localeLanguageRegionIdentifier())
5758

58-
await MainActor.run {
59-
completion(.success(displayMessages(result)))
60-
}
61-
} catch {
62-
await MainActor.run {
63-
completion(.failure(error))
64-
}
59+
return displayMessages(messages)
60+
}
61+
62+
await MainActor.run {
63+
completion(result)
6564
}
6665
}
6766
}
@@ -120,17 +119,14 @@ private extension JustInTimeMessageStore {
120119
for siteID: Int64,
121120
completion: @escaping (Result<Bool, Error>) -> ()) {
122121
Task {
123-
do {
124-
let result = try await remote.dismissJustInTimeMessage(for: siteID,
122+
let result = await Result {
123+
try await remote.dismissJustInTimeMessage(for: siteID,
125124
messageID: message.messageID,
126125
featureClass: message.featureClass)
127-
await MainActor.run {
128-
completion(.success(result))
129-
}
130-
} catch {
131-
await MainActor.run {
132-
completion(.failure(error))
133-
}
126+
}
127+
128+
await MainActor.run {
129+
completion(result)
134130
}
135131
}
136132
}

Yosemite/Yosemite/Stores/StatsStoreV4.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Foundation
22
import Networking
33
import Storage
4-
4+
import WooFoundation
55

66
// MARK: - StatsStoreV4
77
//
@@ -165,17 +165,16 @@ private extension StatsStoreV4 {
165165
onCompletion(.success(()))
166166
} catch {
167167
if let error = error as? DotcomError, error == .noRestRoute {
168-
do {
168+
let result = await Result {
169169
try await loadTopEarnerStatsWithDeprecatedAPI(siteID: siteID,
170170
timeRange: timeRange,
171171
earliestDateToInclude: earliestDateToInclude,
172172
latestDateToInclude: latestDateToInclude,
173173
quantity: quantity,
174174
forceRefresh: forceRefresh)
175-
onCompletion(.success(()))
176-
} catch {
177-
onCompletion(.failure(error))
178175
}
176+
177+
onCompletion(result)
179178
} else {
180179
onCompletion(.failure(error))
181180
}

0 commit comments

Comments
 (0)