Make load*Profile() async

- Handle Task in consumers

- Drop makeReady, always make loaded profile ready

- Rename misleading loadProfile() to profileEx()
This commit is contained in:
Davide De Rosa 2022-04-27 17:02:54 +02:00
parent 02b2e3194e
commit 2b1efb8fec
5 changed files with 42 additions and 37 deletions

View File

@ -131,13 +131,6 @@ class AppContext {
profileManager.availabilityFilter = {
self.isEligibleProfile(withHeader: $0)
}
if let activeProfileId = appManager.activeProfileId {
do {
try profileManager.loadActiveProfile(withId: activeProfileId)
} catch {
pp_log.warning("Unable to load active profile: \(error)")
}
}
providerManager.rateLimitMilliseconds = Constants.RateLimit.providerManager
vpnManager.isOnDemandRulesSupported = {
self.isEligibleForOnDemandRules()
@ -146,6 +139,16 @@ class AppContext {
profileManager.observeUpdates()
vpnManager.observeUpdates()
if let activeProfileId = appManager.activeProfileId {
Task {
do {
try await profileManager.loadActiveProfile(withId: activeProfileId)
} catch {
pp_log.warning("Unable to load active profile: \(error)")
}
}
}
// app
reviewer.eventCountBeforeRating = Constants.Rating.eventCount

View File

@ -158,13 +158,15 @@ extension OrganizerView.ProfilesList {
}
private func presentProfile(withId id: UUID) {
do {
try profileManager.loadCurrentProfile(withId: id, makeReady: true)
Task {
isPresentingProfile = true
do {
try await profileManager.loadCurrentProfile(withId: id)
} catch {
pp_log.error("Unable to load profile: \(error)")
}
}
}
private func removeActiveProfile(_ indexSet: IndexSet) {
guard let activeHeader = activeHeaders?.first else {

View File

@ -210,8 +210,9 @@ extension ProfileView {
return
}
if switchCurrentProfile {
Task {
do {
try profileManager.loadCurrentProfile(withId: copy.id, makeReady: true)
try await profileManager.loadCurrentProfile(withId: copy.id)
} catch {
pp_log.warning("Unable to load profile duplicate: \(error)")
}
@ -219,3 +220,4 @@ extension ProfileView {
}
}
}
}

View File

@ -43,7 +43,7 @@ extension VPNManager {
}
public func connect(with profileId: UUID) async throws {
let result = try profileManager.loadProfile(withId: profileId)
let result = try profileManager.profileEx(withId: profileId)
let profile = result.profile
guard !profileManager.isActiveProfile(profileId) ||
currentState.vpnStatus != .connected else {
@ -64,7 +64,7 @@ extension VPNManager {
}
public func connect(with profileId: UUID, toServer newServerId: String) async throws {
let result = try profileManager.loadProfile(withId: profileId)
let result = try profileManager.profileEx(withId: profileId)
var profile = result.profile
guard profile.isProvider else {
assertionFailure("Profile \(profile.logDescription) is not a provider")

View File

@ -31,7 +31,7 @@ import PassepartoutUtils
@MainActor
public class ProfileManager: ObservableObject {
public typealias LoadResult = (isReady: Bool, profile: Profile)
public typealias ProfileEx = (profile: Profile, isReady: Bool)
// MARK: Initialization
@ -87,15 +87,6 @@ public class ProfileManager: ObservableObject {
currentProfile = ObservableProfile()
}
public func loadActiveProfile(withId id: UUID) throws {
guard isExistingProfile(withId: id) else {
pp_log.warning("Active profile \(id) does not exist, ignoring")
return
}
activeProfileId = id
try loadCurrentProfile(withId: id, makeReady: true)
}
}
// MARK: Index
@ -157,13 +148,13 @@ extension ProfileManager {
saveProfile(profile, isActive: true)
}
public func loadProfile(withId id: UUID) throws -> LoadResult {
public func profileEx(withId id: UUID) throws -> ProfileEx {
guard let profile = profile(withId: id) else {
pp_log.error("Profile not found: \(id)")
throw PassepartoutError.missingProfile
}
pp_log.info("Found profile: \(profile.logDescription)")
return (isProfileReady(profile), profile)
return (profile, isProfileReady(profile))
}
private func profile(withId id: UUID) -> Profile? {
@ -259,7 +250,16 @@ extension ProfileManager {
// MARK: Observation
extension ProfileManager {
public func loadCurrentProfile(withId id: UUID, makeReady: Bool) throws {
public func loadActiveProfile(withId id: UUID) async throws {
guard isExistingProfile(withId: id) else {
pp_log.warning("Active profile \(id) does not exist, ignoring")
return
}
activeProfileId = id
try await loadCurrentProfile(withId: id)
}
public func loadCurrentProfile(withId id: UUID) async throws {
guard !isLoadingCurrentProfile else {
pp_log.warning("Already loading another profile")
return
@ -274,19 +274,17 @@ extension ProfileManager {
saveCurrentProfile()
}
do {
let result = try loadProfile(withId: id)
let result = try profileEx(withId: id)
pp_log.info("Current profile: \(result.profile.logDescription)")
if !makeReady || result.isReady {
if result.isReady {
currentProfile.value = result.profile
isLoadingCurrentProfile = false
} else {
Task {
try await makeProfileReady(result.profile)
currentProfile.value = result.profile
isLoadingCurrentProfile = false
}
}
} catch {
currentProfile.value = .placeholder
isLoadingCurrentProfile = false