From 304741e01951fac57f948edd34ba73f52f019779 Mon Sep 17 00:00:00 2001 From: Max Goedjen Date: Sun, 5 Jan 2025 00:37:03 -0800 Subject: [PATCH] Tweak async for updater --- Sources/Packages/Sources/Brief/Updater.swift | 30 ++++++++++--------- .../BriefTests/ReleaseParsingTests.swift | 8 ++--- Sources/Secretive/Views/UpdateView.swift | 4 ++- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/Sources/Packages/Sources/Brief/Updater.swift b/Sources/Packages/Sources/Brief/Updater.swift index d64bf40..334689d 100644 --- a/Sources/Packages/Sources/Brief/Updater.swift +++ b/Sources/Packages/Sources/Brief/Updater.swift @@ -28,29 +28,31 @@ import Synchronization testBuild = currentVersion == SemVer("0.0.0") if checkOnLaunch { // Don't do a launch check if the user hasn't seen the setup prompt explaining updater yet. - checkForUpdates() + Task { + await checkForUpdates() + } } - let timer = Timer.scheduledTimer(withTimeInterval: checkFrequency, repeats: true) { _ in - self.checkForUpdates() + Task { + while !Task.isCancelled { + try? await Task.sleep(for: .seconds(Int(checkFrequency))) + await checkForUpdates() + } } - timer.tolerance = 60*60 } /// Manually trigger an update check. - public func checkForUpdates() { - URLSession.shared.dataTask(with: Constants.updateURL) { data, _, _ in - guard let data = data else { return } - guard let releases = try? JSONDecoder().decode([Release].self, from: data) else { return } - self.evaluate(releases: releases) - }.resume() + public func checkForUpdates() async { + guard let (data, _) = try? await URLSession.shared.data(from: Constants.updateURL) else { return } + guard let releases = try? JSONDecoder().decode([Release].self, from: data) else { return } + await evaluate(releases: releases) } /// Ignores a specified release. `update` will be nil if the user has ignored the latest available release. /// - Parameter release: The release to ignore. - public func ignore(release: Release) { + public func ignore(release: Release) async { guard !release.critical else { return } defaults.set(true, forKey: release.name) - Task { @MainActor in + await MainActor.run { _update.withLock { value in value = nil } @@ -63,7 +65,7 @@ extension Updater { /// Evaluates the available downloadable releases, and selects the newest non-prerelease release that the user is able to run. /// - Parameter releases: An array of ``Release`` objects. - func evaluate(releases: [Release]) { + func evaluate(releases: [Release]) async { guard let release = releases .sorted() .reversed() @@ -73,7 +75,7 @@ extension Updater { guard !release.prerelease else { return } let latestVersion = SemVer(release.name) if latestVersion > currentVersion { - Task { @MainActor in + await MainActor.run { _update.withLock { value in value = release } diff --git a/Sources/Packages/Tests/BriefTests/ReleaseParsingTests.swift b/Sources/Packages/Tests/BriefTests/ReleaseParsingTests.swift index 044a056..1fc6364 100644 --- a/Sources/Packages/Tests/BriefTests/ReleaseParsingTests.swift +++ b/Sources/Packages/Tests/BriefTests/ReleaseParsingTests.swift @@ -72,8 +72,8 @@ import Foundation Release(name: "1.0.2", prerelease: false, html_url: URL(string: "https://example.com")!, body: "Emergency patch! Minimum macOS Version: 1.2.3"), ] - updater.evaluate(releases: releases) - try await Task.sleep(nanoseconds: 500) + await updater.evaluate(releases: releases) + try await Task.sleep(nanoseconds: 1) #expect(updater.update == two) } @@ -89,8 +89,8 @@ import Foundation Release(name: "2.0.0", prerelease: false, html_url: URL(string: "https://example.com")!, body: "2.0 available! Minimum macOS Version: 2.2.3"), Release(name: "1.0.2", prerelease: false, html_url: URL(string: "https://example.com")!, body: "Emergency patch! Minimum macOS Version: 1.2.3"), ] - updater.evaluate(releases: releases) - try await Task.sleep(nanoseconds: 500) + await updater.evaluate(releases: releases) + try await Task.sleep(nanoseconds: 1) #expect(updater.update == oneOhTwo) } diff --git a/Sources/Secretive/Views/UpdateView.swift b/Sources/Secretive/Views/UpdateView.swift index a997d3a..b262861 100644 --- a/Sources/Secretive/Views/UpdateView.swift +++ b/Sources/Secretive/Views/UpdateView.swift @@ -18,7 +18,9 @@ struct UpdateDetailView: View { HStack { if !update.critical { Button("update_ignore_button") { - updater.ignore(release: update) + Task { + await updater.ignore(release: update) + } } Spacer() }