replace debounce by sample

This commit is contained in:
Alexander Nozik 2023-11-08 22:33:49 +03:00
parent 74301afb42
commit fb8ee59f14

View File

@ -5,9 +5,9 @@ package space.kscience.controls.vision
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.FlowPreview import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.sample
import kotlinx.coroutines.flow.transform import kotlinx.coroutines.flow.transform
import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.sync.withLock
@ -93,13 +93,13 @@ public fun Plot.plotDeviceProperty(
maxAge: Duration = 1.hours, maxAge: Duration = 1.hours,
maxPoints: Int = 800, maxPoints: Int = 800,
minPoints: Int = 400, minPoints: Int = 400,
debounceDuration: Duration = 10.milliseconds, sampling: Duration = 10.milliseconds,
coroutineScope: CoroutineScope = device.context, coroutineScope: CoroutineScope = device.context,
configuration: Scatter.() -> Unit = {}, configuration: Scatter.() -> Unit = {},
): Job = scatter(configuration).run { ): Job = scatter(configuration).run {
val clock = device.context.clock val clock = device.context.clock
val data = TimeData() val data = TimeData()
device.propertyMessageFlow(propertyName).debounce(debounceDuration).transform { device.propertyMessageFlow(propertyName).sample(sampling).transform {
data.append(it.time ?: clock.now(), it.value.extractValue()) data.append(it.time ?: clock.now(), it.value.extractValue())
data.trim(maxAge, maxPoints, minPoints) data.trim(maxAge, maxPoints, minPoints)
emit(data) emit(data)
@ -115,11 +115,11 @@ private fun <T> Trace.updateFromState(
maxAge: Duration = 1.hours, maxAge: Duration = 1.hours,
maxPoints: Int = 800, maxPoints: Int = 800,
minPoints: Int = 400, minPoints: Int = 400,
debounceDuration: Duration = 10.milliseconds, sampling: Duration = 10.milliseconds,
): Job { ): Job {
val clock = context.clock val clock = context.clock
val data = TimeData() val data = TimeData()
return state.valueFlow.debounce(debounceDuration).transform<T, TimeData> { return state.valueFlow.sample(sampling).transform<T, TimeData> {
data.append(clock.now(), it.extractValue()) data.append(clock.now(), it.extractValue())
data.trim(maxAge, maxPoints, minPoints) data.trim(maxAge, maxPoints, minPoints)
}.onEach { }.onEach {
@ -134,10 +134,10 @@ public fun <T> Plot.plotDeviceState(
maxAge: Duration = 1.hours, maxAge: Duration = 1.hours,
maxPoints: Int = 800, maxPoints: Int = 800,
minPoints: Int = 400, minPoints: Int = 400,
debounceDuration: Duration = 10.milliseconds, sampling: Duration = 10.milliseconds,
configuration: Scatter.() -> Unit = {}, configuration: Scatter.() -> Unit = {},
): Job = scatter(configuration).run { ): Job = scatter(configuration).run {
updateFromState(context, state, extractValue, maxAge, maxPoints, minPoints, debounceDuration) updateFromState(context, state, extractValue, maxAge, maxPoints, minPoints, sampling)
} }
@ -147,10 +147,10 @@ public fun Plot.plotNumberState(
maxAge: Duration = 1.hours, maxAge: Duration = 1.hours,
maxPoints: Int = 800, maxPoints: Int = 800,
minPoints: Int = 400, minPoints: Int = 400,
debounceDuration: Duration = 10.milliseconds, sampling: Duration = 10.milliseconds,
configuration: Scatter.() -> Unit = {}, configuration: Scatter.() -> Unit = {},
): Job = scatter(configuration).run { ): Job = scatter(configuration).run {
updateFromState(context, state, { asValue() }, maxAge, maxPoints, minPoints, debounceDuration) updateFromState(context, state, { asValue() }, maxAge, maxPoints, minPoints, sampling)
} }
@ -160,8 +160,8 @@ public fun Plot.plotBooleanState(
maxAge: Duration = 1.hours, maxAge: Duration = 1.hours,
maxPoints: Int = 800, maxPoints: Int = 800,
minPoints: Int = 400, minPoints: Int = 400,
debounceDuration: Duration = 10.milliseconds, sampling: Duration = 10.milliseconds,
configuration: Bar.() -> Unit = {}, configuration: Bar.() -> Unit = {},
): Job = bar(configuration).run { ): Job = bar(configuration).run {
updateFromState(context, state, { asValue() }, maxAge, maxPoints, minPoints, debounceDuration) updateFromState(context, state, { asValue() }, maxAge, maxPoints, minPoints, sampling)
} }