Francis Mariano
05/17/2024, 5:21 PMArkadii Ivanov
05/17/2024, 5:43 PMArkadii Ivanov
05/17/2024, 5:46 PMArkadii Ivanov
05/17/2024, 5:47 PMArkadii Ivanov
05/17/2024, 5:47 PMFrancis Mariano
05/17/2024, 5:50 PMYou can create LifecycleRegistry inside the Executor and drive it via intents. And still use flowWithLifecycle.I will try. I was creating a child scope from store and start/cancel it via intents
Francis Mariano
05/17/2024, 5:57 PMArkadii Ivanov
05/17/2024, 6:00 PMFrancis Mariano
05/17/2024, 6:02 PMExecution failed for task ':shared:transformIosMainCInteropDependenciesMetadataForIde'.
> Could not resolve all files for configuration ':shared:iosMainResolvableDependenciesMetadata'.
> Could not find com.arkivanov.decompose:extensions-compose-jetbrains:3.0.0.
Required by:
project :shared
The gradle sync is failing. Do you know the reason??Arkadii Ivanov
05/17/2024, 6:04 PMFrancis Mariano
05/20/2024, 1:30 PMonIntent<HomeStore.Intent.Start> {
logger.d { "START" }
scopeObservations = CoroutineScope(Job() + Dispatchers.Main)
observations()
}
onIntent<HomeStore.Intent.Stop> {
logger.d { "STOP" }
scopeObservations.cancel()
}
And in the component I call the intent in the lifecycle correspondent.Francis Mariano
05/20/2024, 2:10 PMprivate fun CoroutineExecutorScope<HomeStore.State, Msg, Unit, HomeStore.Label>.observations() {
peripheralRepository.power.onEach {
dispatch(Msg.UpdatePower(it))
}.launchIn(scopeObservation)
}
The observation function has CoroutineExecutorScope as its scope because I need of Msg and State, but the onEach is launched in another scope.Arkadii Ivanov
05/20/2024, 3:43 PMI have a stateFlow collect which is launched in onActionSo I assumed that you need to collect the flow in an action handler, and also have the flow lifecycle-aware. With your current approach in the snippet you are just starting the flow on Start and cancel it on Stop. Also I still find the approach with
LifecycleRegistry
cleaner. I.e. you would need to only collect
once (e.g. in onAction
) and then just start/stop
the lifecycle via intents.Francis Mariano
05/20/2024, 5:41 PMval lifecycleRegistry = LifecycleRegistry()
onAction<Unit> {
peripheralRepository.power.onEach {
dispatch(Msg.UpdatePower(it))
}.launchIn(this). // scope of store executor
}
onIntent<HomeStore.Intent.Start> {
lifecycleRegistry.onStart()
}
onIntent<HomeStore.Intent.Stop> {
lifecycleRegistry.onStart()
}
I do not know how to bind the collect with lifecycleRegistry :(Arkadii Ivanov
05/20/2024, 5:43 PMresume
and stop
extension functions. And lastly, you can use flowWithLifecycle
operator from Essenty, and then collect
. https://github.com/arkivanov/Essenty?tab=readme-ov-file#coroutines-extensionsArkadii Ivanov
05/20/2024, 5:43 PMFrancis Mariano
05/20/2024, 6:17 PMprivate val lifecycleRegistry = LifecycleRegistry()
onAction<Unit> {
logger.d { "ACTION" }
peripheralRepository.power.withLifecycle(lifecycleRegistry).onEach {
logger.d { "power" }
}.launchIn(scope = this)
}
onIntent<HomeStore.Intent.Start> {
logger.d { "START" }
lifecycleRegistry.resume()
}
onIntent<HomeStore.Intent.Stop> {
logger.d { "STOP" }
lifecycleRegistry.pause()
}
power is logged even after lifecycleRegistry.pause() is called 😞Arkadii Ivanov
05/20/2024, 6:19 PMFrancis Mariano
05/20/2024, 6:24 PMonIntent<HomeStore.Intent.Start> {
logger.d { "START" }
lifecycleRegistry.start()
}
onIntent<HomeStore.Intent.Stop> {
logger.d { "STOP" }
lifecycleRegistry.stop()
}
Arkadii Ivanov
05/20/2024, 6:27 PMFrancis Mariano
05/20/2024, 6:38 PM