Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small improvement to Personas loading #1169

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public struct PersonaList: Sendable, FeatureReducer {
}

public enum InternalAction: Sendable, Equatable {
case personasLoaded(IdentifiedArrayOf<PersonaFeature.State>)
case setPersonas([Persona])
case setSecurityProblems([SecurityProblem])
}

Expand Down Expand Up @@ -80,27 +80,12 @@ public struct PersonaList: Sendable, FeatureReducer {
case personasMissingFromClient(OrderedSet<Persona.ID>)
}

/// Returns the ids of personas to include under the given strategy. nil means that all ids should be included
private func personaIDs(_ strategy: State.ReloadingStrategy) async throws -> OrderedSet<Persona.ID>? {
switch strategy {
case .all:
return nil
case let .ids(ids):
return ids
case let .dApp(dAppID):
guard let dApp = try? await authorizedDappsClient.getDetailedDapp(dAppID) else { return [] }
return OrderedSet(dApp.detailedAuthorizedPersonas.map(\.id))
}
}

public func reduce(into state: inout State, internalAction: InternalAction) -> Effect<Action> {
switch internalAction {
case var .personasLoaded(personas):
personas.mutateAll { persona in
persona.securityProblemsConfig.update(problems: state.problems)
}

case let .setPersonas(personas):
state.personas = personas
.map { PersonaFeature.State(persona: $0, problems: state.problems) }
.asIdentified()
return .none

case let .setSecurityProblems(problems):
Expand Down Expand Up @@ -133,15 +118,15 @@ public struct PersonaList: Sendable, FeatureReducer {
}

private func personasEffect(state: State) -> Effect<Action> {
.run { [strategy = state.strategy, problems = state.problems] send in
.run { [strategy = state.strategy] send in
for try await personas in await personasClient.personas() {
guard !Task.isCancelled else { return }
let ids = try await personaIDs(strategy) ?? OrderedSet(validating: personas.ids)
let result = ids.compactMap { personas[id: $0] }.map { PersonaFeature.State(persona: $0, problems: problems) }
let result = ids.compactMap { personas[id: $0] }
guard result.count == ids.count else {
throw UpdatePersonaError.personasMissingFromClient(ids.subtracting(result.map(\.id)))
}
await send(.internal(.personasLoaded(result.asIdentified())))
await send(.internal(.setPersonas(result)))
}
} catch: { error, _ in
loggerGlobal.error("Failed to update personas from client, error: \(error)")
Expand All @@ -157,4 +142,17 @@ public struct PersonaList: Sendable, FeatureReducer {
}
}
}

/// Returns the ids of personas to include under the given strategy. nil means that all ids should be included
private func personaIDs(_ strategy: State.ReloadingStrategy) async throws -> OrderedSet<Persona.ID>? {
switch strategy {
case .all:
return nil
case let .ids(ids):
return ids
case let .dApp(dAppID):
guard let dApp = try? await authorizedDappsClient.getDetailedDapp(dAppID) else { return [] }
return OrderedSet(dApp.detailedAuthorizedPersonas.map(\.id))
}
}
}
Loading