2019-07-06 05:06:31 +02:00
|
|
|
//
|
|
|
|
// FetchRequestOperation.swift
|
|
|
|
// NetNewsWire
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 6/20/19.
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import RSCore
|
|
|
|
import Account
|
|
|
|
import Articles
|
|
|
|
|
|
|
|
// Main thread only.
|
|
|
|
// Runs an asynchronous fetch.
|
|
|
|
|
|
|
|
typealias FetchRequestOperationResultBlock = (Set<Article>, FetchRequestOperation) -> Void
|
|
|
|
|
2019-10-14 07:08:05 +02:00
|
|
|
final class FetchRequestOperation {
|
2019-07-06 05:06:31 +02:00
|
|
|
|
|
|
|
let id: Int
|
2020-09-09 00:14:49 +02:00
|
|
|
let readFilterEnabledTable: [FeedIdentifier: Bool]
|
2019-07-06 05:06:31 +02:00
|
|
|
let resultBlock: FetchRequestOperationResultBlock
|
|
|
|
var isCanceled = false
|
|
|
|
var isFinished = false
|
2020-09-13 01:09:42 +02:00
|
|
|
private let fetchers: [ArticleFetcher]
|
2019-07-06 05:06:31 +02:00
|
|
|
|
2020-09-13 01:09:42 +02:00
|
|
|
init(id: Int, readFilterEnabledTable: [FeedIdentifier: Bool], fetchers: [ArticleFetcher], resultBlock: @escaping FetchRequestOperationResultBlock) {
|
2019-07-06 05:06:31 +02:00
|
|
|
precondition(Thread.isMainThread)
|
|
|
|
self.id = id
|
2020-09-09 00:14:49 +02:00
|
|
|
self.readFilterEnabledTable = readFilterEnabledTable
|
2020-09-13 01:09:42 +02:00
|
|
|
self.fetchers = fetchers
|
2019-07-06 05:06:31 +02:00
|
|
|
self.resultBlock = resultBlock
|
|
|
|
}
|
|
|
|
|
|
|
|
func run(_ completion: @escaping (FetchRequestOperation) -> Void) {
|
|
|
|
precondition(Thread.isMainThread)
|
|
|
|
precondition(!isFinished)
|
|
|
|
|
2019-10-14 07:08:05 +02:00
|
|
|
var didCallCompletion = false
|
|
|
|
|
|
|
|
func callCompletionIfNeeded() {
|
|
|
|
if !didCallCompletion {
|
|
|
|
didCallCompletion = true
|
|
|
|
completion(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-06 05:06:31 +02:00
|
|
|
if isCanceled {
|
2019-10-14 07:08:05 +02:00
|
|
|
callCompletionIfNeeded()
|
2019-07-06 05:06:31 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-13 01:09:42 +02:00
|
|
|
if fetchers.isEmpty {
|
2019-07-06 05:06:31 +02:00
|
|
|
isFinished = true
|
|
|
|
resultBlock(Set<Article>(), self)
|
2019-10-14 07:08:05 +02:00
|
|
|
callCompletionIfNeeded()
|
2019-07-06 05:06:31 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-13 01:09:42 +02:00
|
|
|
let numberOfFetchers = fetchers.count
|
2019-07-06 05:06:31 +02:00
|
|
|
var fetchersReturned = 0
|
|
|
|
var fetchedArticles = Set<Article>()
|
2019-11-22 02:54:35 +01:00
|
|
|
|
2019-12-17 07:45:59 +01:00
|
|
|
func process(_ articles: Set<Article>) {
|
2019-11-22 02:54:35 +01:00
|
|
|
precondition(Thread.isMainThread)
|
|
|
|
guard !self.isCanceled else {
|
|
|
|
callCompletionIfNeeded()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(!self.isFinished)
|
|
|
|
|
|
|
|
fetchedArticles.formUnion(articles)
|
|
|
|
fetchersReturned += 1
|
|
|
|
if fetchersReturned == numberOfFetchers {
|
|
|
|
self.isFinished = true
|
|
|
|
self.resultBlock(fetchedArticles, self)
|
|
|
|
callCompletionIfNeeded()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-13 01:09:42 +02:00
|
|
|
for fetcher in fetchers {
|
|
|
|
if (fetcher as? Feed)?.readFiltered(readFilterEnabledTable: readFilterEnabledTable) ?? true {
|
|
|
|
fetcher.fetchUnreadArticlesAsync { articleSetResult in
|
2019-12-17 07:45:59 +01:00
|
|
|
let articles = (try? articleSetResult.get()) ?? Set<Article>()
|
|
|
|
process(articles)
|
2019-07-06 05:06:31 +02:00
|
|
|
}
|
2020-09-13 01:09:42 +02:00
|
|
|
} else {
|
|
|
|
fetcher.fetchArticlesAsync { articleSetResult in
|
2019-12-17 07:45:59 +01:00
|
|
|
let articles = (try? articleSetResult.get()) ?? Set<Article>()
|
|
|
|
process(articles)
|
2019-07-06 05:06:31 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-13 01:09:42 +02:00
|
|
|
|
2019-07-06 05:06:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|