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

RecompositionMode.Immediate with non-ui dispatcher can cause "missed" recompositions #416

Open
j2esu opened this issue Apr 7, 2024 · 2 comments

Comments

@j2esu
Copy link

j2esu commented Apr 7, 2024

I've noticed that if molecule composition happens together with ui composition the molecule composition is "missed".
It never happens with the ContextClock mode but it's quite easy to reproduce with the Immediate mode and non-ui dispatcher.

Below there is an example of counter that updates the local state and sends a message to update the state produced by molecule. In 5-15 clicks (no need to click fast though) it's possible to get the wrong state when the states are not equal. The next click after that usually makes them equal again (which I think means that the state in molecule gets updated, but it doesn't trigger a recomposition, probably because the ui recomposition is currently in progress).

Screenshot 2024-04-07 191342

Molecule 1.4.2
Compose compiler 1.5.11
Compose runtime 1.7.0-alpha06 (to get #396 fixed)
Android SDK 34

class MainActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val messages = MutableSharedFlow<Unit>(extraBufferCapacity = 10)
        val state = lifecycleScope.launchMolecule(RecompositionMode.Immediate, Dispatchers.Default) {
            var count by remember { mutableIntStateOf(0) }
            LaunchedEffect(Unit) {
                messages.collect { count++ }
            }
            count
        }
        setContent {
            val count by state.collectAsState()
            var countLocal by remember { mutableIntStateOf(0) }
            Column(modifier = Modifier.background(Color.Black)) {
                Text(text = "Count: $count", color = Color.White)
                Text(text = "Count local: $countLocal", color = Color.White)
                Text(
                    text = "Click me",
                    color = Color.White,
                    modifier = Modifier
                        .clickable {
                            countLocal++
                            messages.tryEmit(Unit)
                        }
                )
            }
        }
    }
}
@j2esu j2esu changed the title RecompositionMode.Immediate with non-ui dispatcher can cause "skipped" recompositions RecompositionMode.Immediate with non-ui dispatcher can cause "missed" recompositions Apr 9, 2024
@j2esu
Copy link
Author

j2esu commented May 29, 2024

It's much harder to reproduce (takes 50-100 clicks) with molecule 2.0.0, compose 1.7.0-beta01 and compose compiler shipped with kotlin 2.0.0

@j2esu
Copy link
Author

j2esu commented Aug 2, 2024

The issue is till there on compose 1.7.0-beta06

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant