Skip to content

Debouncing state emissions #336

@TheKeeperOfPie

Description

@TheKeeperOfPie

I'm trying to migrate

val data = snapshotFlow { buildFromState() }.debounce(1.seconds)
...
data.collectAsState(null)

to remove the view side default null value.

But there's no easy way to debounce in Molecule. I think the closest you can get would be

val debouncedFlow = snapshotFlow { buildFromState() }.debounce(1.seconds)
val state = scope.launchMolecule { 
    debouncedFlow.collectAsState(buildFromState()).value
}

but this moves from Compose (the actual state) -> Flow (snapshotFlow) -> Molecule -> Compose (collectAsState) -> Compose UI, which is very inefficient compared to reading the state directly inside Molecule. It also requires calling buildFromState() redundantly.

If there was a way to control/throttle the recomposition timing in Molecule, then I could try and make it only recompose every second or when the debounce concluded.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions