Convert to async/await.

This commit is contained in:
Brent Simmons committed 2025-11-10 08:53:54 -08:00
1 parent a4249fc554
commit 02bddb792d
12 files changed
+64 -53

No files matched your search

@@ -87,12 +87,12 @@ final class DetailViewController: NSViewController, WKUIDelegate {
currentWebViewController.stopMediaPlayback()
}
func canScrollDown(_ callback: @escaping (Bool) -> Void) {
currentWebViewController.canScrollDown(callback)
func canScrollDown() async -> Bool {
await currentWebViewController.canScrollDown()
}
func canScrollUp(_ callback: @escaping (Bool) -> Void) {
currentWebViewController.canScrollUp(callback)
func canScrollUp() async -> Bool {
await currentWebViewController.canScrollUp()
}
override func scrollPageDown(_ sender: Any?) {
@@ -150,16 +150,14 @@ final class DetailWebViewController: NSViewController {
// MARK: Scrolling
func canScrollDown(_ completion: @escaping (Bool) -> Void) {
fetchScrollInfo { (scrollInfo) in
completion(scrollInfo?.canScrollDown ?? false)
}
func canScrollDown() async -> Bool {
let scrollInfo = await fetchScrollInfo()
return scrollInfo?.canScrollDown ?? false
}
func canScrollUp(_ completion: @escaping (Bool) -> Void) {
fetchScrollInfo { (scrollInfo) in
completion(scrollInfo?.canScrollUp ?? false)
}
func canScrollUp() async -> Bool {
let scrollInfo = await fetchScrollInfo()
return scrollInfo?.canScrollUp ?? false
}
override func scrollPageDown(_ sender: Any?) {
@@ -306,7 +304,15 @@ private extension DetailWebViewController {
webView.loadHTMLString(html, baseURL: URL(string: rendering.baseURL))
}
func fetchScrollInfo(_ completion: @escaping (ScrollInfo?) -> Void) {
func fetchScrollInfo() async -> ScrollInfo? {
await withCheckedContinuation { continuation in
self.fetchScrollInfo { scrollInfo in
continuation.resume(returning: scrollInfo)
}
}
}
private func fetchScrollInfo(_ completion: @escaping (ScrollInfo?) -> Void) {
let javascriptString = "var x = {contentHeight: document.body.scrollHeight, offsetY: document.body.scrollTop}; x"
webView.evaluateJavaScript(javascriptString) { (info, error) in
+12 -6
View File
@@ -279,21 +279,27 @@ final class MainWindowController : NSWindowController, NSUserInterfaceValidation
// MARK: - Actions
@IBAction func scrollOrGoToNextUnread(_ sender: Any?) {
guard let detailViewController = detailViewController else {
guard let detailViewController else {
return
}
detailViewController.canScrollDown { (canScroll) in
Task { @MainActor in
let canScroll = await detailViewController.canScrollDown()
NSCursor.setHiddenUntilMouseMoves(true)
canScroll ? detailViewController.scrollPageDown(sender) : self.nextUnread(sender)
if canScroll {
detailViewController.scrollPageDown(sender)
} else {
nextUnread(sender)
}
}
}
@IBAction func scrollUp(_ sender: Any?) {
guard let detailViewController = detailViewController else {
guard let detailViewController else {
return
}
detailViewController.canScrollUp { (canScroll) in
if (canScroll) {
Task { @MainActor in
let canScroll = await detailViewController.canScrollUp()
if canScroll {
NSCursor.setHiddenUntilMouseMoves(true)
detailViewController.scrollPageUp(sender)
}
@@ -310,7 +310,9 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
self.fetchAllUnreadCounts()
}
self.delegate.accountDidInitialize(self)
MainActor.assumeIsolated {
self.delegate.accountDidInitialize(self)
}
}
// MARK: - API
@@ -394,7 +396,7 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
await delegate.receiveRemoteNotification(for: self, userInfo: userInfo)
}
public func refreshAll() async throws {
@MainActor public func refreshAll() async throws {
try await delegate.refreshAll(for: self)
}
@@ -425,7 +427,7 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
}
}
public func suspendNetwork() {
@MainActor public func suspendNetwork() {
delegate.suspendNetwork()
}
@@ -438,7 +440,7 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
/// Re-open the SQLite database and allow database calls.
/// Call this *before* calling resume.
public func resumeDatabaseAndDelegate() {
@MainActor public func resumeDatabaseAndDelegate() {
#if os(iOS)
database.resume()
#endif
@@ -456,7 +458,7 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
opmlFile.save()
}
public func prepareForDeletion() {
@MainActor public func prepareForDeletion() {
delegate.accountWillBeDeleted(self)
}
@@ -48,18 +48,18 @@ protocol AccountDelegate {
@MainActor func markArticles(for account: Account, articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool) async throws
// Called at the end of account’s init method.
func accountDidInitialize(_ account: Account)
@MainActor func accountDidInitialize(_ account: Account)
func accountWillBeDeleted(_ account: Account)
@MainActor func accountWillBeDeleted(_ account: Account)
static func validateCredentials(transport: Transport, credentials: Credentials, endpoint: URL?) async throws -> Credentials?
@MainActor static func validateCredentials(transport: Transport, credentials: Credentials, endpoint: URL?) async throws -> Credentials?
/// Suspend all network activity
func suspendNetwork()
@MainActor func suspendNetwork()
/// Suspend the SQLite databases
func suspendDatabase()
@MainActor func suspendDatabase()
/// Make sure no SQLite databases are open and we are ready to issue network requests.
func resume()
@MainActor func resume()
}
@@ -167,7 +167,7 @@ public final class AccountManager: UnreadCountProvider {
return account
}
public func deleteAccount(_ account: Account) {
@MainActor public func deleteAccount(_ account: Account) {
guard !account.refreshInProgress else {
return
}
@@ -236,7 +236,7 @@ public final class AccountManager: UnreadCountProvider {
return nil
}
public func suspendNetworkAll() {
@MainActor public func suspendNetworkAll() {
isSuspended = true
for account in accounts {
account.suspendNetwork()
@@ -249,7 +249,7 @@ public final class AccountManager: UnreadCountProvider {
}
}
public func resumeAll() {
@MainActor public func resumeAll() {
isSuspended = false
for account in accounts {
account.resumeDatabaseAndDelegate()
@@ -278,9 +278,9 @@ public final class AccountManager: UnreadCountProvider {
combinedRefreshProgress.start()
Task { @MainActor in
await withTaskGroup(of: Void.self) { group in
await withTaskGroup(of: Void.self, isolation: MainActor.shared) { group in
for account in activeAccounts {
group.addTask {
group.addTask { @MainActor in
do {
try await account.refreshAll()
} catch {
@@ -313,9 +313,6 @@ public final class AccountManager: UnreadCountProvider {
try? await account.syncArticleStatus()
group.leave()
}
}
group.notify(queue: DispatchQueue.global(qos: .background)) {
completion?()
}
}
@@ -381,15 +381,15 @@ final class CloudKitAccountDelegate: AccountDelegate {
// MARK: - Suspend and Resume (for iOS)
func suspendNetwork() {
@MainActor func suspendNetwork() {
refresher.suspend()
}
func suspendDatabase() {
@MainActor func suspendDatabase() {
syncDatabase.suspend()
}
func resume() {
@MainActor func resume() {
refresher.resume()
syncDatabase.resume()
}
@@ -63,7 +63,7 @@ final class CloudKitArticlesZone: CloudKitZone {
migrateChangeToken()
}
func refreshArticles() async throws {
@MainActor func refreshArticles() async throws {
do {
try await fetchChangesInZone()
} catch {
@@ -26,13 +26,13 @@ final class CloudKitReceiveStatusOperation: MainThreadOperation {
self.articlesZone = articlesZone
}
func run() {
@MainActor func run() {
guard let articlesZone else {
self.operationDelegate?.operationDidComplete(self)
return
}
Task {
Task { @MainActor in
Self.logger.debug("iCloud: Refreshing article statuses")
do {
try await articlesZone.refreshArticles()
@@ -38,7 +38,7 @@ final class LocalAccountDelegate: AccountDelegate {
func receiveRemoteNotification(for account: Account, userInfo: [AnyHashable : Any]) async {
}
func refreshAll(for account: Account) async throws {
@MainActor func refreshAll(for account: Account) async throws {
guard refreshProgress.isComplete, !Platform.isRunningUnitTests else {
return
}
@@ -137,15 +137,15 @@ final class LocalAccountDelegate: AccountDelegate {
// MARK: Suspend and Resume (for iOS)
func suspendNetwork() {
@MainActor func suspendNetwork() {
refresher.suspend()
}
func suspendDatabase() {
@MainActor func suspendDatabase() {
// Nothing to do
}
func resume() {
@MainActor func resume() {
refresher.resume()
}
}
@@ -36,7 +36,7 @@ final class LocalAccountRefresher {
private static let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "LocalAccountRefresher")
public func refreshFeeds(_ feeds: Set<Feed>) async {
@MainActor public func refreshFeeds(_ feeds: Set<Feed>) async {
await withCheckedContinuation { continuation in
refreshFeeds(feeds) {
continuation.resume()
@@ -44,7 +44,7 @@ final class LocalAccountRefresher {
}
}
private func refreshFeeds(_ feeds: Set<Feed>, completion: (() -> Void)? = nil) {
@MainActor private func refreshFeeds(_ feeds: Set<Feed>, completion: (() -> Void)? = nil) {
let specialCaseCutoffDate = Date().bySubtracting(hours: 25)
let filteredFeeds = feeds.filter { !Self.feedShouldBeSkipped($0, specialCaseCutoffDate) }
@@ -66,12 +66,12 @@ final class LocalAccountRefresher {
downloadSession.download(Set(urls))
}
public func suspend() {
@MainActor public func suspend() {
downloadSession.cancelAll()
isSuspended = true
}
public func resume() {
@MainActor public func resume() {
isSuspended = false
}
}
@@ -50,7 +50,7 @@ public protocol MainThreadOperation: AnyObject {
/// When this is called, you don’t need to check isCanceled:
/// it’s guaranteed to not be canceled. However, if you run code
/// in another thread, you should check isCanceled in that code.
func run()
@MainActor func run()
/// Cancel this operation.
///