Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

debounce with coroutines #1107

Merged
merged 5 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove GlobalScope usage
  • Loading branch information
IKupriyanov-HORIS committed May 27, 2024
commit 97086c252647617e943a31f797e20952df4979a4
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@

package org.jetbrains.letsPlot.commons

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


// TODO: investigate can GlobalScope be replaced with some other scope
@OptIn(DelicateCoroutinesApi::class)
// Not thread-safe
fun <T> debounce(
delayMs: Long,
scope: CoroutineScope = GlobalScope,
scope: CoroutineScope,
action: (T) -> Unit
): (T) -> Unit {
var activeJob: Job? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

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
Expand Down Expand Up @@ -47,9 +49,10 @@ internal class PlotToolEventDispatcher(
deactivateOverlappingInteractions(origin, interactionSpec)

val interactionName = interactionSpec.getValue(ToolInteractionSpec.NAME) as String
val debouncedWheelZoom = debounce<Pair<DoubleRectangle, InteractionTarget>>(500) { (rect, _) ->
println("Wheel zoom tool: apply: $rect")
}
val debouncedWheelZoom =
debounce<Pair<DoubleRectangle, InteractionTarget>>(500, CoroutineScope(Dispatchers.Default)) { (rect, _) ->
println("Wheel zoom tool: apply: $rect")
}

// ToDo: sent "completed" event in "onCompleted"
val feedback = when (interactionName) {
Expand Down