mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2025-08-12 06:26:36 +00:00
Begin adopting async/await.
This commit is contained in:
@@ -695,6 +695,20 @@ public enum FetchType {
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor public func asyncFetchArticles(_ fetchType: FetchType) async throws -> Set<Article> {
|
||||
// Temporary until we can go async await all the way down.
|
||||
return try await withCheckedThrowingContinuation { continuation in
|
||||
fetchArticlesAsync(fetchType) { articleSetResult in
|
||||
switch articleSetResult {
|
||||
case .success(let articles):
|
||||
continuation.resume(returning: articles)
|
||||
case .failure(let error):
|
||||
continuation.resume(throwing: error)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public func fetchArticlesAsync(_ fetchType: FetchType, _ completion: @escaping ArticleSetResultBlock) {
|
||||
switch fetchType {
|
||||
case .starred(let limit):
|
||||
@@ -1113,6 +1127,12 @@ private extension Account {
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor func articlesForFeed(_ feed: Feed) async throws -> Set<Article> {
|
||||
let articles = try await database.articlesForFeed(feed.feedID)
|
||||
validateUnreadCount(feed, articles)
|
||||
return articles
|
||||
}
|
||||
|
||||
func fetchArticlesMatching(_ searchString: String) throws -> Set<Article> {
|
||||
return try database.fetchArticlesMatching(searchString, flattenedFeeds().feedIDs())
|
||||
}
|
||||
|
||||
@@ -380,6 +380,33 @@ import RSDatabase
|
||||
|
||||
// These fetch articles from active accounts and return a merged Set<Article>.
|
||||
|
||||
@MainActor public func asyncFetchArticles(_ fetchType: FetchType) async throws -> Set<Article> {
|
||||
|
||||
guard activeAccounts.count > 0 else {
|
||||
return Set<Article>()
|
||||
}
|
||||
|
||||
let articles = try await withThrowingTaskGroup(of: Set<Article>.self) { taskGroup in
|
||||
for account in activeAccounts {
|
||||
taskGroup.addTask {
|
||||
let articles = try await account.asyncFetchArticles(fetchType)
|
||||
return articles
|
||||
}
|
||||
}
|
||||
|
||||
var allFetchedArticles = Set<Article>()
|
||||
for try await oneAccountArticles in taskGroup {
|
||||
allFetchedArticles.formUnion(oneAccountArticles)
|
||||
}
|
||||
|
||||
return allFetchedArticles
|
||||
}
|
||||
|
||||
return articles
|
||||
}
|
||||
|
||||
|
||||
|
||||
@MainActor public func fetchArticles(_ fetchType: FetchType) throws -> Set<Article> {
|
||||
precondition(Thread.isMainThread)
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ import ArticlesDatabase
|
||||
func fetchUnreadArticles() throws -> Set<Article>
|
||||
func fetchUnreadArticlesBetween(before: Date?, after: Date?) throws -> Set<Article>
|
||||
func fetchUnreadArticlesAsync(_ completion: @escaping ArticleSetResultBlock)
|
||||
// func asyncFetchUnreadArticles() async throws -> Set<Article>
|
||||
}
|
||||
|
||||
extension Feed: ArticleFetcher {
|
||||
@@ -57,6 +58,15 @@ extension Feed: ArticleFetcher {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public func asyncFetchUnreadArticles() async throws -> Set<Article> {
|
||||
guard let account else {
|
||||
assertionFailure("Expected feed.account, but got nil.")
|
||||
return Set<Article>()
|
||||
}
|
||||
let articles = try await account.asyncFetchArticles(.feed(self))
|
||||
return articles.unreadArticles()
|
||||
}
|
||||
}
|
||||
|
||||
extension Folder: ArticleFetcher {
|
||||
|
||||
@@ -136,7 +136,11 @@ public typealias ArticleStatusesResultBlock = (ArticleStatusesResult) -> Void
|
||||
|
||||
// MARK: - Fetching Articles Async
|
||||
|
||||
public func fetchArticlesAsync(_ feedID: String, _ completion: @escaping ArticleSetResultBlock) {
|
||||
public func articlesForFeed(_ feedID: String) async throws -> Set<Article> {
|
||||
try await articlesTable.articlesForFeed(feedID)
|
||||
}
|
||||
|
||||
public func fetchArticlesAsync(_ feedID: String, _ completion: @escaping ArticleSetResultBlock) {
|
||||
articlesTable.fetchArticlesAsync(feedID, completion)
|
||||
}
|
||||
|
||||
|
||||
@@ -47,7 +47,20 @@ final class ArticlesTable: DatabaseTable {
|
||||
}
|
||||
|
||||
// MARK: - Fetching Articles for Feed
|
||||
|
||||
|
||||
func articlesForFeed(_ feedID: String) async throws -> Set<Article> {
|
||||
return try await withCheckedThrowingContinuation { continuation in
|
||||
fetchArticlesAsync({ self.fetchArticlesForFeedID(feedID, $0) }) { articleSetResult in
|
||||
switch articleSetResult {
|
||||
case .success(let articles):
|
||||
continuation.resume(returning: articles)
|
||||
case .failure(let error):
|
||||
continuation.resume(throwing: error)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func fetchArticles(_ feedID: String) throws -> Set<Article> {
|
||||
return try fetchArticles{ self.fetchArticlesForFeedID(feedID, $0) }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user