Merge #4327
4327: Flow.ifChanged(): Added variant that takes a mapping function. r=csadilek a=pocmo
This pattern emerged while migrating components. Often I want listen to value changes, but if the value changes then I need more than only the changed value.
One example is the downloads feature. I want to listen to download changes. But if that changes then I need the whole `SessionState` object.
Previously:
```kotlin
scope = store.flowScoped { flow ->
flow.mapNotNull { state -> state.findCustomTabOrSelectedTab(customTabId) }
.map { it.content.download }
.ifChanged()
.collect { download ->
// Okay, the DownloadState object now. But I also want the enclosing SessionState.
}
}
```
Now:
```kotlin
scope = store.flowScoped { flow ->
flow.mapNotNull { state -> state.findCustomTabOrSelectedTab(customTabId) }
.ifChanged { it.content.download }
.collect { state ->
// Yay, I have the whole session state and can get
// to the download by using state.content.download
}
}
```
Co-authored-by:
Sebastian Kaspari <s.kaspari@gmail.com>
Please register or sign in to comment