Skip to content

Commit

Permalink
Fix profile access on ProfileStore (#1384)
Browse files Browse the repository at this point in the history
  • Loading branch information
GhenadieVP authored Nov 14, 2024
1 parent ef319ad commit 865721a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct OnboardingClient: Sendable {
}

extension OnboardingClient {
typealias LoadProfileState = @Sendable () async -> ProfileState
typealias LoadProfileState = @Sendable () async throws -> ProfileState
typealias CreateNewProfile = @Sendable () async throws -> Void

typealias FinishOnboardingWithRecoveredAccountsAndBDFS = @Sendable (AccountsRecoveredFromScanningUsingMnemonic) async throws -> Void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ extension OnboardingClient: DependencyKey {
static func live(profileStore: ProfileStore = .shared) -> Self {
Self(
loadProfileState: {
await profileStore.profileState()
try await profileStore.profileStateSequence().first()
},
createNewProfile: {
try await profileStore.createNewProfile()
Expand Down
22 changes: 9 additions & 13 deletions RadixWallet/Clients/ProfileStore/ProfileStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Sargon
final actor ProfileStore {
static let shared = ProfileStore()

private let profileSubject: AsyncReplaySubject<Profile> = .init(bufferSize: 1)
private let profileSubject: AsyncCurrentValueSubject<Profile?> = .init(nil)
private let profileStateSubject: AsyncReplaySubject<ProfileState> = .init(bufferSize: 1)

private init() {
Expand All @@ -23,16 +23,11 @@ final actor ProfileStore {
}

extension ProfileStore {
func profile() async -> Profile {
try! await profileSubject.first()
}

func profileState() async -> ProfileState {
try! await profileStateSubject.first()
}

func profileSequence() async -> AnyAsyncSequence<Profile> {
profileSubject.eraseToAnyAsyncSequence()
func profile() -> Profile {
guard let profile = profileSubject.value else {
fatalError("Programmer error - tried to access profile when it was not loaded yet.")
}
return profile
}

func profileStateSequence() async -> AnyAsyncSequence<ProfileState> {
Expand Down Expand Up @@ -84,7 +79,7 @@ extension ProfileStore {
func updating<T: Sendable>(
_ transform: @Sendable (inout Profile) async throws -> T
) async throws -> T {
var updated = await profile()
var updated = profile()
let result = try await transform(&updated)
try await SargonOS.shared.setProfile(profile: updated)
return result
Expand All @@ -106,7 +101,8 @@ extension ProfileStore {
func _lens<Property>(
_ transform: @escaping @Sendable (Profile) -> Property?
) -> AnyAsyncSequence<Property> where Property: Sendable & Equatable {
profileSubject.compactMap(transform)
profileSubject
.compactMap { $0.flatMap(transform) }
.share() // Multicast
.removeDuplicates()
.eraseToAnyAsyncSequence()
Expand Down
4 changes: 2 additions & 2 deletions RadixWallet/Features/SplashFeature/Splash.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ struct Splash: Sendable, FeatureReducer {

func loadAdvancedLockState() -> Effect<Action> {
.run { send in
let profileState = await onboardingClient.loadProfileState()
let profileState = try await onboardingClient.loadProfileState()

if case let .loaded(profile) = profileState {
let isAdvancedLockEnabled = profile.appPreferences.security.isAdvancedLockEnabled
Expand Down Expand Up @@ -252,7 +252,7 @@ struct Splash: Sendable, FeatureReducer {
.run { send in
switch context {
case .appStarted:
await send(.delegate(.completed(onboardingClient.loadProfileState())))
try await send(.delegate(.completed(onboardingClient.loadProfileState())))
case .appForegrounded:
localAuthenticationClient.setAuthenticatedSuccessfully()
}
Expand Down

0 comments on commit 865721a

Please sign in to comment.