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 = { profileManager.availabilityFilter = {
self.isEligibleProfile(withHeader: $0) 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 providerManager.rateLimitMilliseconds = Constants.RateLimit.providerManager
vpnManager.isOnDemandRulesSupported = { vpnManager.isOnDemandRulesSupported = {
self.isEligibleForOnDemandRules() self.isEligibleForOnDemandRules()
@ -146,6 +139,16 @@ class AppContext {
profileManager.observeUpdates() profileManager.observeUpdates()
vpnManager.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 // app
reviewer.eventCountBeforeRating = Constants.Rating.eventCount reviewer.eventCountBeforeRating = Constants.Rating.eventCount

View File

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

View File

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

View File

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

View File

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