-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7319bc5
commit 9a25862
Showing
3 changed files
with
46 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
kotlinx-coroutines-core/common/src/flow/internal/DiscardingCollector.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package kotlinx.coroutines.flow.internal | ||
|
||
import kotlinx.coroutines.InternalCoroutinesApi | ||
import kotlinx.coroutines.channels.SendChannel | ||
import kotlinx.coroutines.flow.FlowCollector | ||
|
||
/** | ||
* A [FlowCollector] specifically built to be used for `Flow.dropWhileBusy` | ||
* | ||
* This collector ensures that the fist element is always sent. It then offers subsequent elements, | ||
* allowing them to be dropped. | ||
*/ | ||
@InternalCoroutinesApi | ||
internal class DiscardingCollector<T>(private val channel: SendChannel<T>) : FlowCollector<T> { | ||
|
||
private var hasSentFirstValue: Boolean = false | ||
|
||
override suspend fun emit(value: T) { | ||
if (hasSentFirstValue) { | ||
channel.offer(value) | ||
} else { | ||
channel.send(value) | ||
hasSentFirstValue = true | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters