Handling effect cancellation when changing state #2665
Unanswered
lukeredpath
asked this question in
Q&A
Replies: 1 comment 3 replies
-
Hi @lukeredpath, are you using the Would it be possible for you to cook up a really simple unit test that demonstrates the behavior you are seeing? That would help us figure out how to move forward. |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
There has been some previous discussion on whether or not
TaskResult
should not wrap cancellation errors, see:#1934
#2281
I think the conclusion was that this is the correct behaviour as you might need to react to cancellation like any other error to clean up some state (e.g. some kind of
requestInFlight: Bool
property) and that in general, automatic effect cancellation when some feature state goes away is now handled by the built-in navigation tools.However, I still have one case in my app which I imagine is common and is not currently managed by the built-in navigation tools (and I'm not sure how I would use them) where I have to manage this cancellation manually and that is when the user gets logged out (either explicitly or for some other reason).
The user's session state is managed by a
var session: Session.State
property in the root of our app feature state, is non-optional and is an enum value:The session feature manages the logout process which can be summarised as:
The intention here is to cancel any in-flight logged-in network requests when the user is logged out - the way we do this is that we override our
apiClient
dependency on theLoggedInFeature()
reducer so that all requests are wrapped with a single global cancellation ID (CancelId.loggedInRequests
) - this allows the session reducer to trigger the cancellation of all API requests started within theLoggedInFeature
and any of its child features with a single.cancel(id:)
call.This works as expected but now in the process of updating to TCA 1.4.0, I'm no longer using
TaskResult
and am just usingResult
. This means that after transitioning to the.loggedOut
state, I can receive logged in actions for the cancelled requests, e.g..loggedIn(.someRequest(.failed(CancellationError())))
- this causes purple runtime warnings in dev builds and more importantly, can cause test failures (and we have some tests explicitly testing this cancellation scenario).What would be the best way to handle this?
Beta Was this translation helpful? Give feedback.
All reactions