Skip to content

Commit

Permalink
Tweak async for updater
Browse files Browse the repository at this point in the history
  • Loading branch information
maxgoedjen committed Jan 5, 2025
1 parent 8e70754 commit 304741e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
30 changes: 16 additions & 14 deletions Sources/Packages/Sources/Brief/Updater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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()
Expand All @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/Packages/Tests/BriefTests/ReleaseParsingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand Down
4 changes: 3 additions & 1 deletion Sources/Secretive/Views/UpdateView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ struct UpdateDetailView<UpdaterType: Updater>: View {
HStack {
if !update.critical {
Button("update_ignore_button") {
updater.ignore(release: update)
Task {
await updater.ignore(release: update)
}
}
Spacer()
}
Expand Down

0 comments on commit 304741e

Please sign in to comment.