Skip to content

Commit

Permalink
debounce with coroutines (#1107)
Browse files Browse the repository at this point in the history
  • Loading branch information
IKupriyanov-HORIS committed Jun 3, 2024
1 parent bf4e9f6 commit 5a2fbc7
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 1 deletion.
9 changes: 9 additions & 0 deletions commons/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ plugins {

val mockkVersion = extra["mockk_version"] as String
val kotlinLoggingVersion = extra["kotlinLogging_version"] as String
val kotlinxCoroutinesVersion = extra["kotlinx_coroutines_version"] as String
val hamcrestVersion = extra["hamcrest_version"] as String
val mockitoVersion = extra["mockito_version"] as String
val assertjVersion = extra["assertj_version"] as String
Expand All @@ -24,6 +25,14 @@ kotlin {
}

sourceSets {
commonMain {
dependencies {
// Can't use compileOnly
// > Task :commons:compileTestDevelopmentExecutableKotlinJs FAILED
//e: Could not find "org.jetbrains.kotlinx:kotlinx-coroutines-core" in [/home/me/.local/share/kotlin/daemon]
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinxCoroutinesVersion")
}
}
commonTest {
dependencies {
implementation(kotlin("test"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2024. JetBrains s.r.o.
* Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/

package org.jetbrains.letsPlot.commons

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch


// Not thread-safe
fun <T> debounce(
delayMs: Long,
scope: CoroutineScope,
action: (T) -> Unit
): (T) -> Unit {
var activeJob: Job? = null

return { v: T ->
activeJob?.cancel()
activeJob = scope.launch {
delay(delayMs)
action(v)
}
}
}
2 changes: 2 additions & 0 deletions demo/plot/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ kotlin {
val batikVersion = extra["batik_version"] as String
val kotlinLoggingVersion = extra["kotlinLogging_version"] as String
val kotlinxHtmlVersion = extra["kotlinx_html_version"] as String
val kotlinxCoroutinesVersion = extra["kotlinx_coroutines_version"] as String
val ktorVersion = extra["ktor_version"] as String
val jfxPlatform = extra["jfxPlatformResolved"] as String
val jfxVersion = extra["jfx_version"] as String
Expand All @@ -39,6 +40,7 @@ kotlin {
commonMain {
dependencies {
implementation(kotlin("stdlib-common"))
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinxCoroutinesVersion")

implementation(project(":commons"))
implementation(project(":datamodel"))
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ kotlin_version=1.9.23
kotlinLogging_version=2.0.5

kotlinx_html_version=0.7.3
kotlinx_coroutines_version=1.7.1

slf4j_version=1.7.29

Expand Down
6 changes: 6 additions & 0 deletions plot-builder/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ plugins {

val mockkVersion = extra["mockk_version"] as String
val kotlinLoggingVersion = extra["kotlinLogging_version"] as String
val kotlinxCoroutinesVersion = extra["kotlinx_coroutines_version"] as String
val hamcrestVersion = extra["hamcrest_version"] as String
val mockitoVersion = extra["mockito_version"] as String
val assertjVersion = extra["assertj_version"] as String
Expand All @@ -22,6 +23,11 @@ kotlin {
sourceSets {
commonMain {
dependencies {
// Can't use compileOnly
// > Task :commons:compileTestDevelopmentExecutableKotlinJs FAILED
//e: Could not find "org.jetbrains.kotlinx:kotlinx-coroutines-core" in [/home/me/.local/share/kotlin/daemon]
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinxCoroutinesVersion")

compileOnly(project(":commons"))
compileOnly(project(":datamodel"))
compileOnly(project(":plot-base"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

package org.jetbrains.letsPlot.core.plot.builder.interact

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import org.jetbrains.letsPlot.commons.debounce
import org.jetbrains.letsPlot.commons.geometry.DoubleRectangle
import org.jetbrains.letsPlot.commons.registration.Registration
import org.jetbrains.letsPlot.core.interact.DrawRectFeedback
Expand Down Expand Up @@ -47,6 +50,11 @@ internal class PlotToolEventDispatcher(
deactivateOverlappingInteractions(origin, interactionSpec)

val interactionName = interactionSpec.getValue(ToolInteractionSpec.NAME) as String
val completeInteractionDebounced =
debounce<DoubleRectangle>(DEBOUNCE_DELAY_MS, CoroutineScope(Dispatchers.Default)) { dataBounds ->
println("Debounced interaction: $interactionName, dataBounds: $dataBounds")
completeInteraction(origin, interactionName, dataBounds)
}

// ToDo: sent "completed" event in "onCompleted"
val feedback = when (interactionName) {
Expand All @@ -66,7 +74,7 @@ internal class PlotToolEventDispatcher(

ToolInteractionSpec.WHEEL_ZOOM -> WheelZoomFeedback(
onCompleted = { dataBounds ->
completeInteraction(origin, interactionName, dataBounds)
completeInteractionDebounced(dataBounds)
}
)

Expand Down Expand Up @@ -151,4 +159,8 @@ internal class PlotToolEventDispatcher(
) {
val interactionName = interactionSpec.getValue(ToolInteractionSpec.NAME) as String
}

companion object {
private const val DEBOUNCE_DELAY_MS = 30L
}
}

0 comments on commit 5a2fbc7

Please sign in to comment.