Skip to content

Add throttleFirst Flow operator #1927

@fluidsonic

Description

@fluidsonic

We have no operator that prevents subsequent emissions for a certain time after an emission.
In RxJava there is throttleFirst for that.

flow {
    repeat(10) {
        emit(it)
        delay(101)
    }
}
    .throttleFirst(500)
    .collect {
        println(it)
    }

The example above should print 0 and 5. Both should be emitted without any added delay.

One use-case is button clicks where (accidental) repeated clicks should be filtered. If I click a button twice accidentally, only the first click event would make it downstream and the second click be ignored because it came in too fast after the first one.

debounce(500) is not suitable because it
a) delays the emission of the first value by at least 500ms and
b) delays the emission of any value until the user stops clicking for at least 500ms.

sample(500) is not suitable because it
a) delays the emission of the first value by 500ms and
b) emits nothing if the Flow is closed within 500ms after collection has started.

The related throttleLatest was mentioned in #1107 but no reason was given why it nor it's siblings have made it into Flow so far.

Activity

jxdabc

jxdabc commented on May 7, 2020

@jxdabc
Contributor

@fluidsonic
If I'm not wrong.
The above throttleLatest example should print 0, 4.
The operator you want seems being called throttleFirst.
Plese see https://github.com/ReactiveX/RxJava/wiki/Filtering-Observables#throttlefirst

fluidsonic

fluidsonic commented on May 7, 2020

@fluidsonic
Author

@jxdabc you're right, throttleFirst is more appropriate for solving the problem. I'll update the issue.

changed the title [-]Add throttleLatest Flow operator[/-] [+]Add throttleFirst Flow operator[/+] on May 7, 2020
davidmigloz

davidmigloz commented on Dec 7, 2021

@davidmigloz

Duplicate of #1446

hoc081098

hoc081098 commented on Mar 24, 2022

@hoc081098
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @fluidsonic@elizarov@qwwdfsad@jxdabc@davidmigloz

        Issue actions

          Add throttleFirst Flow operator · Issue #1927 · Kotlin/kotlinx.coroutines