0

This question is the same like this and this, but about Kotlin flow.

What needs to be achieved:

  • Deliver the first item as soon as it comes
  • Debounce all the following items the way debounce function works
Kiryl Tkach
  • 2,717
  • 3
  • 19
  • 31

1 Answers1

2

There is simple solution with dynamic debounce timeout:

var firstEmission = true
flow.debounce {
    if (firstEmission) {
        firstEmission = false
        0L
    } else DEBOUNCE_TIMEOUT
}

Also possible to do this way:

merge(
    flow.take(1),
    flow.drop(1).debounce(DEBOUNCE_TIMEOUT)
)
Kiryl Tkach
  • 2,717
  • 3
  • 19
  • 31