Make .mark functions async await.

This commit is contained in:
Brent Simmons 2024-04-04 20:47:13 -07:00
parent 26de8d8b9c
commit 3f2db0ef12
2 changed files with 22 additions and 74 deletions

View File

@ -856,24 +856,29 @@ public enum FetchType {
}
func update(_ articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool, completion: @escaping ArticleSetResultBlock) {
// Returns set of Articles whose statuses did change.
guard !articles.isEmpty else {
completion(.success(Set<Article>()))
return
}
database.mark(articles, statusKey: statusKey, flag: flag) { result in
MainActor.assumeIsolated {
switch result {
case .success(let updatedStatuses):
Task { @MainActor in
do {
var updatedArticles = Set<Article>()
if let updatedStatuses = try await database.mark(articles: articles, statusKey: statusKey, flag: flag) {
let updatedArticleIDs = updatedStatuses.articleIDs()
let updatedArticles = Set(articles.filter{ updatedArticleIDs.contains($0.articleID) })
updatedArticles = Set(articles.filter{ updatedArticleIDs.contains($0.articleID) })
self.noteStatusesForArticlesDidChange(updatedArticles)
completion(.success(updatedArticles))
case .failure(let error):
completion(.failure(error))
}
completion(.success(updatedArticles))
} catch {
completion(.failure(.suspended))
}
}
}
@ -899,14 +904,14 @@ public enum FetchType {
completion?(nil)
return
}
database.mark(articleIDs: articleIDs, statusKey: statusKey, flag: flag) { error in
MainActor.assumeIsolated {
if let error {
completion?(error)
} else {
self.noteStatusesForArticleIDsDidChange(articleIDs: articleIDs, statusKey: statusKey, flag: flag)
completion?(nil)
}
Task { @MainActor in
do {
try await database.mark(articleIDs: articleIDs, statusKey: statusKey, flag: flag)
self.noteStatusesForArticleIDsDidChange(articleIDs: articleIDs, statusKey: statusKey, flag: flag)
completion?(nil)
} catch {
completion?(.suspended)
}
}
}

View File

@ -20,12 +20,6 @@ public typealias UpdateArticlesCompletionBlock = @Sendable (UpdateArticlesResult
public typealias ArticleSetResult = Result<Set<Article>, DatabaseError>
public typealias ArticleSetResultBlock = (ArticleSetResult) -> Void
public typealias ArticleIDsResult = Result<Set<String>, DatabaseError>
public typealias ArticleIDsCompletionBlock = @Sendable (ArticleIDsResult) -> Void
public typealias ArticleStatusesResult = Result<Set<ArticleStatus>, DatabaseError>
public typealias ArticleStatusesResultBlock = (ArticleStatusesResult) -> Void
public extension ArticlesDatabase {
// MARK: - Saving, Updating, and Deleting Articles
@ -56,61 +50,10 @@ public extension ArticlesDatabase {
}
}
// MARK: - Status
nonisolated func mark(_ articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool, completion: @escaping ArticleStatusesResultBlock) {
Task {
do {
let statuses = try await mark(articles: articles, statusKey: statusKey, flag: flag)!
callArticleStatusesCompletion(completion, .success(statuses))
} catch {
callArticleStatusesCompletion(completion, .failure(.suspended))
}
}
}
nonisolated func mark(articleIDs: Set<String>, statusKey: ArticleStatus.Key, flag: Bool, completion: @escaping DatabaseCompletionBlock) {
Task {
do {
try await mark(articleIDs: articleIDs, statusKey: statusKey, flag: flag)
callDatabaseCompletion(completion)
} catch {
callDatabaseCompletion(completion, .suspended)
}
}
}
nonisolated private func callUpdateArticlesCompletion(_ completion: @escaping UpdateArticlesCompletionBlock, _ result: UpdateArticlesResult) {
Task { @MainActor in
completion(result)
}
}
nonisolated private func callArticleStatusesCompletion(_ completion: @escaping ArticleStatusesResultBlock, _ result: ArticleStatusesResult) {
Task { @MainActor in
completion(result)
}
}
nonisolated private func callArticleIDsCompletion(_ completion: @escaping ArticleIDsCompletionBlock, _ result: ArticleIDsResult) {
Task { @MainActor in
completion(result)
}
}
nonisolated private func callDatabaseCompletion(_ completion: DatabaseCompletionBlock?, _ error: DatabaseError? = nil) {
guard let completion else {
return
}
Task { @MainActor in
completion(error)
}
}
}