Replies: 4 comments 1 reply
-
I'm facing the same issue. When a logged user logs out or switchs to a different account, we recreate our dependencies tree and recreate all needed objects. Using @dependency is not working on that scenario since I'm guessing the answer is not, but is there any way to force recreation of this type of dependencies? Maybe that's out of the scope of this library. |
Beta Was this translation helpful? Give feedback.
-
Sorry, we must have missed this when it first came in. I don't think there is any way to have a In isowords we have a client that exposes session information, but of course it is optional: struct APIClient {
public var authenticate: @Sendable (ServerRoute.AuthenticateRequest) async throws -> CurrentPlayerEnvelope
public var currentPlayer: @Sendable () -> CurrentPlayerEnvelope?
public var logout: @Sendable () async -> Void
public var refreshCurrentPlayer: @Sendable () async throws -> CurrentPlayerEnvelope
// ...
} It works well enough for us.
In the situation of a user switch or logout I imagine you would recreate the base view of the application. Just seems like a good way to reset everything, and in that can you can do: withDependencies {
$0 = .live
$0...
} operation: {
AppModel()
} …to reset everything back to the default live implementations, as well as any further customizations you want to make. However, due to the design of the library (in particular, using |
Beta Was this translation helpful? Give feedback.
-
This is related to the discussion here pointfreeco/swift-composable-architecture#1775 maybe it helps ^^ |
Beta Was this translation helpful? Give feedback.
-
Thanks for the feedback @mbrandonw! Sorry for the delay, just getting back to this project now after working on something else. So removing the requirement that the dependency returns a non-nil value, and assuming it's modeled as an optional, I'm still unsure where is the appropriate place to do this using TCA:
Take a simplified version of the TicTacToe example: public var body: some ReducerProtocol<State, Action> {
Reduce { state, action in
switch action {
case let .login(.loginResponse(.success))
state = .newGame(NewGame.State())
return .none
case .login:
return .none
case .newGame(.logoutButtonTapped):
state = .login(Login.State())
return .none
case .newGame:
return .none
}
}
.ifCaseLet(/State.login, action: /Action.login) {
Login()
}
.ifCaseLet(/State.newGame, action: /Action.newGame) {
NewGame()
}
} Where would you handle updating the dependency in a case like this? Feels like we'd want to do it when we're changing the state between .ifCaseLet(/State.login, action: /Action.login) {
Login()
.dependency(\.currentUser, nil)
}
.ifCaseLet(/State.newGame, action: /Action.newGame) {
@Dependency(\.keychain) var keychain
NewGame()
.dependency(\.currentUser, try? keychain.get(.currentUser)))
} It seems to work, but I'm not sure it's correct. The docs for
I noticed the code above behaves the same, |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm starting to adopt the library, but I'm still confused about the correct way to handle session-based dependencies that don't have a reasonable default/static
liveValue
and that will need to change over time? For example, if you have aUser
type and want acurrentUser
dependency, what would you use asliveValue
, and how do you update that value on sign in/out? I'm assuming here that we don't want it to be an optional and only screens that require authentication will reference@Dependency(\.currentUser) var currentUser
.Can you provide some examples for that use case? I couldn't find any clear answers in the docs or the discussions. Thanks!
Beta Was this translation helpful? Give feedback.
All reactions