diff --git a/platform/util-ex/src/com/intellij/util/flow/flow.kt b/platform/util-ex/src/com/intellij/util/flow/flow.kt index 579934281f33..c2cf7d70e0f2 100644 --- a/platform/util-ex/src/com/intellij/util/flow/flow.kt +++ b/platform/util-ex/src/com/intellij/util/flow/flow.kt @@ -31,23 +31,28 @@ import kotlinx.coroutines.launch * 1, 3, 4 * ``` */ -fun Flow.throttle(timeMs: Long): Flow = channelFlow { - val latch = Channel() - val latchJob = launch(start = CoroutineStart.UNDISPATCHED) { - while (isActive) { - latch.send(Unit) - delay(timeMs) - } +fun Flow.throttle(timeMs: Long): Flow { + if (timeMs <= 0) { + return this } - try { - collectLatest { - latch.receive() - @Suppress("EXPERIMENTAL_API_USAGE") - send(it) + return channelFlow { + val latch = Channel() + val latchJob = launch(start = CoroutineStart.UNDISPATCHED) { + while (isActive) { + latch.send(Unit) + delay(timeMs) + } + } + try { + collectLatest { + latch.receive() + @Suppress("EXPERIMENTAL_API_USAGE") + send(it) + } + } + finally { + latchJob.cancel() + latch.close() } - } - finally { - latchJob.cancel() - latch.close() } }