Convert addFeed to async await.

This commit is contained in:
Brent Simmons 2024-03-28 08:24:35 -07:00
parent 0ce67a3f3f
commit c5441bddc3
11 changed files with 163 additions and 121 deletions

View File

@ -607,8 +607,9 @@ public enum FetchType {
return feed
}
public func addFeed(_ feed: Feed, to container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
delegate.addFeed(for: self, with: feed, to: container, completion: completion)
public func addFeed(_ feed: Feed, to container: Container) async throws {
try await delegate.addFeed(for: self, with: feed, to: container)
}
public func createFeed(url: String, name: String?, container: Container, validateFeed: Bool, completion: @escaping (Result<Feed, Error>) -> Void) {

View File

@ -38,7 +38,7 @@ import Secrets
func createFeed(for account: Account, url: String, name: String?, container: Container, validateFeed: Bool, completion: @escaping (Result<Feed, Error>) -> Void)
func renameFeed(for account: Account, with feed: Feed, to name: String) async throws
func addFeed(for account: Account, with: Feed, to container: Container, completion: @escaping (Result<Void, Error>) -> Void)
func addFeed(for account: Account, with: Feed, to container: Container) async throws
func removeFeed(for account: Account, with feed: Feed, from container: Container, completion: @escaping (Result<Void, Error>) -> Void)
func moveFeed(for account: Account, with feed: Feed, from: Container, to: Container, completion: @escaping (Result<Void, Error>) -> Void)

View File

@ -321,7 +321,22 @@ enum CloudKitAccountDelegateError: LocalizedError {
}
}
func addFeed(for account: Account, with feed: Feed, to container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
func addFeed(for account: Account, with feed: Feed, to container: any Container) async throws {
try await withCheckedThrowingContinuation { continuation in
self.addFeed(for: account, with: feed, to: container) { result in
switch result {
case .success:
continuation.resume()
case .failure(let error):
continuation.resume(throwing: error)
}
}
}
}
private func addFeed(for account: Account, with feed: Feed, to container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
refreshProgress.addToNumberOfTasksAndRemaining(1)
accountZone.addFeed(feed, to: container) { result in
self.refreshProgress.completeTask()

View File

@ -564,7 +564,22 @@ final class FeedbinAccountDelegate: AccountDelegate {
}
}
func addFeed(for account: Account, with feed: Feed, to container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
func addFeed(for account: Account, with feed: Feed, to container: any Container) async throws {
try await withCheckedThrowingContinuation { continuation in
self.addFeed(for: account, with: feed, to: container) { result in
switch result {
case .success:
continuation.resume()
case .failure(let error):
continuation.resume(throwing: error)
}
}
}
}
private func addFeed(for account: Account, with feed: Feed, to container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
if let folder = container as? Folder, let feedID = Int(feed.feedID) {
refreshProgress.addToNumberOfTasksAndRemaining(1)
@ -614,11 +629,13 @@ final class FeedbinAccountDelegate: AccountDelegate {
private func restoreFeed(for account: Account, feed: Feed, container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
if let existingFeed = account.existingFeed(withURL: feed.url) {
account.addFeed(existingFeed, to: container) { result in
switch result {
case .success:
Task { @MainActor in
do {
try await account.addFeed(existingFeed, to: container)
completion(.success(()))
case .failure(let error):
} catch {
completion(.failure(error))
}
}
@ -632,9 +649,8 @@ final class FeedbinAccountDelegate: AccountDelegate {
}
}
}
}
func restoreFolder(for account: Account, folder: Folder) async throws {
try await withCheckedThrowingContinuation { continuation in
@ -1190,34 +1206,22 @@ private extension FeedbinAccountDelegate {
}
func createFeed( account: Account, subscription sub: FeedbinSubscription, name: String?, container: Container, completion: @escaping (Result<Feed, Error>) -> Void) {
DispatchQueue.main.async {
Task { @MainActor in
let feed = account.createFeed(with: sub.name, url: sub.url, feedID: String(sub.feedID), homePageURL: sub.homePageURL)
feed.externalID = String(sub.subscriptionID)
feed.iconURL = sub.jsonFeed?.icon
feed.faviconURL = sub.jsonFeed?.favicon
account.addFeed(feed, to: container) { result in
switch result {
case .success:
if let name = name {
Task { @MainActor in
do {
try await account.renameFeed(feed, to: name)
self.initialFeedDownload(account: account, feed: feed, completion: completion)
} catch {
completion(.failure(error))
}
}
} else {
self.initialFeedDownload(account: account, feed: feed, completion: completion)
}
case .failure(let error):
completion(.failure(error))
do {
try await account.addFeed(feed, to: container)
if let name {
try await self.renameFeed(for: account, with: feed, to: name)
}
self.initialFeedDownload(account: account, feed: feed, completion: completion)
} catch {
completion(.failure(error))
}
}
}

View File

@ -494,7 +494,22 @@ final class FeedlyAccountDelegate: AccountDelegate {
feed.editedName = name
}
@MainActor func addFeed(for account: Account, with feed: Feed, to container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
func addFeed(for account: Account, with feed: Feed, to container: any Container) async throws {
try await withCheckedThrowingContinuation { continuation in
self.addFeed(for: account, with: feed, to: container) { result in
switch result {
case .success:
continuation.resume()
case .failure(let error):
continuation.resume(throwing: error)
}
}
}
}
func addFeed(for account: Account, with feed: Feed, to container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
do {
guard let credentials = credentials else {
@ -594,12 +609,14 @@ final class FeedlyAccountDelegate: AccountDelegate {
}
private func restoreFeed(for account: Account, feed: Feed, container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
if let existingFeed = account.existingFeed(withURL: feed.url) {
account.addFeed(existingFeed, to: container) { result in
switch result {
case .success:
Task { @MainActor in
do {
try await account.addFeed(existingFeed, to: container)
completion(.success(()))
case .failure(let error):
} catch {
completion(.failure(error))
}
}

View File

@ -107,11 +107,11 @@ final class LocalAccountDelegate: AccountDelegate {
completion(.success(()))
}
func addFeed(for account: Account, with feed: Feed, to container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
func addFeed(for account: Account, with feed: Feed, to container: any Container) async throws {
container.addFeed(feed)
completion(.success(()))
}
func restoreFeed(for account: Account, feed: Feed, container: any Container) async throws {
container.addFeed(feed)

View File

@ -434,30 +434,19 @@ extension NewsBlurAccountDelegate {
return
}
DispatchQueue.main.async {
Task { @MainActor in
let feed = account.createFeed(with: feed.name, url: feed.feedURL, feedID: String(feed.feedID), homePageURL: feed.homePageURL)
feed.externalID = String(feed.feedID)
feed.faviconURL = feed.faviconURL
account.addFeed(feed, to: container) { result in
switch result {
case .success:
if let name = name {
Task { @MainActor in
do {
try await account.renameFeed(feed, to: name)
self.initialFeedDownload(account: account, feed: feed, completion: completion)
} catch {
completion(.failure(error))
}
}
} else {
self.initialFeedDownload(account: account, feed: feed, completion: completion)
}
case .failure(let error):
completion(.failure(error))
do {
try await account.addFeed(feed, to: container)
if let name {
try await self.renameFeed(for: account, with: feed, to: name)
}
self.initialFeedDownload(account: account, feed: feed, completion: completion)
} catch {
completion(.failure(error))
}
}
}

View File

@ -576,7 +576,22 @@ final class NewsBlurAccountDelegate: AccountDelegate {
}
}
func addFeed(for account: Account, with feed: Feed, to container: Container, completion: @escaping (Result<Void, Error>) -> ()) {
func addFeed(for account: Account, with feed: Feed, to container: any Container) async throws {
try await withCheckedThrowingContinuation { continuation in
self.addFeed(for: account, with: feed, to: container) { result in
switch result {
case .success:
continuation.resume()
case .failure(let error):
continuation.resume(throwing: error)
}
}
}
}
private func addFeed(for account: Account, with feed: Feed, to container: Container, completion: @escaping (Result<Void, Error>) -> ()) {
guard let folder = container as? Folder else {
DispatchQueue.main.async {
if let account = container as? Account {
@ -643,11 +658,12 @@ final class NewsBlurAccountDelegate: AccountDelegate {
private func restoreFeed(for account: Account, feed: Feed, container: Container, completion: @escaping (Result<Void, Error>) -> ()) {
if let existingFeed = account.existingFeed(withURL: feed.url) {
account.addFeed(existingFeed, to: container) { result in
switch result {
case .success:
Task { @MainActor in
do {
try await account.addFeed(existingFeed, to: container)
completion(.success(()))
case .failure(let error):
} catch {
completion(.failure(error))
}
}

View File

@ -618,7 +618,22 @@ final class ReaderAPIAccountDelegate: AccountDelegate {
}
}
func addFeed(for account: Account, with feed: Feed, to container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
func addFeed(for account: Account, with feed: Feed, to container: any Container) async throws {
try await withCheckedThrowingContinuation { continuation in
self.addFeed(for: account, with: feed, to: container) { result in
switch result {
case .success:
continuation.resume()
case .failure(let error):
continuation.resume(throwing: error)
}
}
}
}
private func addFeed(for account: Account, with feed: Feed, to container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
if let folder = container as? Folder, let feedExternalID = feed.externalID {
refreshProgress.addToNumberOfTasksAndRemaining(1)
caller.createTagging(subscriptionID: feedExternalID, tagName: folder.name ?? "") { result in
@ -666,11 +681,12 @@ final class ReaderAPIAccountDelegate: AccountDelegate {
private func restoreFeed(for account: Account, feed: Feed, container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
if let existingFeed = account.existingFeed(withURL: feed.url) {
account.addFeed(existingFeed, to: container) { result in
switch result {
case .success:
Task { @MainActor in
do {
try await account.addFeed(existingFeed, to: container)
completion(.success(()))
case .failure(let error):
} catch {
completion(.failure(error))
}
}
@ -1086,34 +1102,22 @@ private extension ReaderAPIAccountDelegate {
}
func createFeed( account: Account, subscription sub: ReaderAPISubscription, name: String?, container: Container, completion: @escaping (Result<Feed, Error>) -> Void) {
DispatchQueue.main.async {
Task { @MainActor in
let feed = account.createFeed(with: sub.name, url: sub.url, feedID: String(sub.feedID), homePageURL: sub.homePageURL)
feed.externalID = String(sub.feedID)
account.addFeed(feed, to: container) { result in
switch result {
case .success:
if let name = name {
self.renameFeed(for: account, with: feed, to: name) { result in
switch result {
case .success:
self.initialFeedDownload(account: account, feed: feed, completion: completion)
case .failure(let error):
completion(.failure(error))
}
}
} else {
self.initialFeedDownload(account: account, feed: feed, completion: completion)
}
case .failure(let error):
completion(.failure(error))
do {
try await account.addFeed(feed, to: container)
if let name {
try await self.renameFeed(for: account, with: feed, to: name)
}
self.initialFeedDownload(account: account, feed: feed, completion: completion)
} catch {
completion(.failure(error))
}
}
}
func initialFeedDownload( account: Account, feed: Feed, completion: @escaping (Result<Feed, Error>) -> Void) {

View File

@ -308,17 +308,16 @@ private extension SidebarOutlineDataSource {
}
return localDragOperation(parentNode: parentNode)
}
func copyFeedInAccount(node: Node, to parentNode: Node) {
guard let feed = node.representedObject as? Feed, let destination = parentNode.representedObject as? Container else {
return
}
destination.account?.addFeed(feed, to: destination) { result in
switch result {
case .success:
break
case .failure(let error):
Task { @MainActor in
do {
try await destination.account?.addFeed(feed, to: destination)
} catch {
NSApplication.shared.presentError(error)
}
}
@ -351,11 +350,11 @@ private extension SidebarOutlineDataSource {
}
if let existingFeed = destinationAccount.existingFeed(withURL: feed.url) {
destinationAccount.addFeed(existingFeed, to: destinationContainer) { result in
switch result {
case .success:
break
case .failure(let error):
Task { @MainActor in
do {
try await destinationAccount.addFeed(existingFeed, to: destinationContainer)
} catch {
NSApplication.shared.presentError(error)
}
}
@ -437,14 +436,7 @@ private extension SidebarOutlineDataSource {
for feed in folder.topLevelFeeds {
if let existingFeed = destinationAccount.existingFeed(withURL: feed.url) {
destinationAccount.addFeed(existingFeed, to: destinationFolder) { result in
switch result {
case .success:
break
case .failure(let error):
NSApplication.shared.presentError(error)
}
}
try await destinationAccount.addFeed(existingFeed, to: destinationFolder)
} else {
destinationAccount.createFeed(url: feed.url, name: feed.nameForDisplay, container: destinationFolder, validateFeed: false) { result in
switch result {

View File

@ -116,13 +116,16 @@ extension SidebarViewController: UITableViewDropDelegate {
}
func moveFeedBetweenAccounts(feed: Feed, sourceContainer: Container, destinationContainer: Container) {
if let existingFeed = destinationContainer.account?.existingFeed(withURL: feed.url) {
BatchUpdate.shared.start()
destinationContainer.account?.addFeed(existingFeed, to: destinationContainer) { result in
switch result {
case .success:
Task { @MainActor in
do {
try await destinationContainer.account?.addFeed(existingFeed, to: destinationContainer)
sourceContainer.account?.removeFeed(feed, from: sourceContainer) { result in
BatchUpdate.shared.end()
switch result {
@ -132,14 +135,15 @@ extension SidebarViewController: UITableViewDropDelegate {
self.presentError(error)
}
}
case .failure(let error):
} catch {
BatchUpdate.shared.end()
self.presentError(error)
}
}
} else {
BatchUpdate.shared.start()
destinationContainer.account?.createFeed(url: feed.url, name: feed.editedName, container: destinationContainer, validateFeed: false) { result in
switch result {