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

remove onEnter flatMapPolicy and fix cancellation when leaving state #174

Merged
merged 1 commit into from
Aug 2, 2021

Conversation

gabrielittner
Copy link
Member

Closes #171 and adresses the onEnter part of #128.

The old flatMapWithPolicy didn't get any events when leaving the state which is why it didn't cancel even if it was using LATEST. The new implementation turns each action emission to an isInState boolean and then has a flatMapLatest that will only return the onAction Flow if the boolean was true. This way the previous Flow will get cancelled when isInState changes from true to false. distintUntilChanged is used to ignore any additional emissions after the first true.

Instead of map to a boolean and distintUntilChanged this could also use filterState from the CollectWhileInState implementation. However this seems a lot less complex to me and I can't think of any issues with this implementation.

@@ -576,10 +576,9 @@ could complete before the first execution (because using a random time of waitin
- `CONCAT`: In contrast to `MERGE` and `LATEST` `CONCAT` will not run `on<BarAction>` in parallel and will not cancel
any previous execution. Instead, `CONCAT` will preserve the order and execute one block after another.

All execution blocks can specify a `FlatMapPolicy`:
All execution blocks except `onEnter` can specify a `FlatMapPolicy`:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for updating!

@@ -59,13 +59,11 @@ class InStateBuilderBlock<InputState : S, S : Any, A : Any>(
* TODO add a sample
*/
fun onEnter(
flatMapPolicy: FlatMapPolicy = FlatMapPolicy.LATEST,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

.filter { it == MapStateChange.StateChanged.ENTERED }
.flatMapWithPolicy(flatMapPolicy) {
setStateFlow(getState)
.map { isInState(getState()) }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is a mich simpler implementation 🙏

@gabrielittner gabrielittner merged commit fdd3346 into tests-unconfined Aug 2, 2021
@gabrielittner gabrielittner deleted the onenter branch August 2, 2021 06:34
gabrielittner added a commit that referenced this pull request Aug 2, 2021
* fix issues hidden by Unconfined Dispatcher

* break OnAction and OnEnter canellation tests (#170)

* fix race condition in reduxStore (#172)

* remove onEnter flatMapPolicy and fix cancellation when leaving state (#174)

* cancel onAction when leaving the current state (#175)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants