-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #647 from gemini-hlsw/view-deglitcher
Deglitcher interface for Views
- Loading branch information
Showing
14 changed files
with
364 additions
and
58 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,3 +70,6 @@ smartgcal | |
# Node modules | ||
node_modules/ | ||
package-lock.json | ||
|
||
# Nix | ||
.direnv/ |
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
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
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
71 changes: 71 additions & 0 deletions
71
modules/core/js/src/main/scala/crystal/react/hooks/UseThrottlingStateView.scala
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,71 @@ | ||
// Copyright (c) 2016-2023 Association of Universities for Research in Astronomy, Inc. (AURA) | ||
// For license information see LICENSE or https://opensource.org/licenses/BSD-3-Clause | ||
|
||
package crystal.react.hooks | ||
|
||
import crystal.Pot | ||
import crystal.react.ThrottlingView | ||
import crystal.react.ViewThrottler | ||
import japgolly.scalajs.react.* | ||
import japgolly.scalajs.react.hooks.CustomHook | ||
|
||
import scala.concurrent.duration.FiniteDuration | ||
|
||
object UseThrottlingStateView { | ||
def hook[A]: CustomHook[(A, FiniteDuration), Pot[ThrottlingView[A]]] = | ||
CustomHook[(A, FiniteDuration)] | ||
.useStateViewBy(props => props._1) | ||
.useEffectResultOnMountBy((props, _) => ViewThrottler[A](props._2)) | ||
.buildReturning: (_, view, throttler) => | ||
throttler.map(_.throttle(view)) | ||
|
||
object HooksApiExt { | ||
sealed class Primary[Ctx, Step <: HooksApi.AbstractStep](api: HooksApi.Primary[Ctx, Step]) { | ||
|
||
/** Creates component state as a `ThrottlingView`. See `ViewThrottler[A]`. */ | ||
final def useThrottlingStateView[A](initialValue: => A, timeout: FiniteDuration)(using | ||
step: Step | ||
): step.Next[Pot[ThrottlingView[A]]] = | ||
useThrottlingStateViewBy(_ => (initialValue, timeout)) | ||
|
||
/** Creates component state as a `ThrottlingView`. See `ViewThrottler[A]`. */ | ||
final def useThrottlingStateViewBy[A](props: Ctx => (A, FiniteDuration))(using | ||
step: Step | ||
): step.Next[Pot[ThrottlingView[A]]] = | ||
api.customBy { ctx => | ||
val hookInstance = hook[A] | ||
hookInstance(props(ctx)) | ||
} | ||
} | ||
|
||
final class Secondary[Ctx, CtxFn[_], Step <: HooksApi.SubsequentStep[Ctx, CtxFn]]( | ||
api: HooksApi.Secondary[Ctx, CtxFn, Step] | ||
) extends Primary[Ctx, Step](api) { | ||
|
||
/** Creates component state as a `ThrottlingView`. See `ViewThrottler[A]`. */ | ||
def useThrottlingStateViewBy[A](props: CtxFn[(A, FiniteDuration)])(using | ||
step: Step | ||
): step.Next[Pot[ThrottlingView[A]]] = | ||
useThrottlingStateViewBy(step.squash(props)(_)) | ||
} | ||
} | ||
|
||
protected trait HooksApiExt { | ||
import HooksApiExt.* | ||
|
||
implicit def hooksExtThrottlingStateView1[Ctx, Step <: HooksApi.AbstractStep]( | ||
api: HooksApi.Primary[Ctx, Step] | ||
): Primary[Ctx, Step] = | ||
new Primary(api) | ||
|
||
implicit def hooksExtThrottlingStateView2[Ctx, CtxFn[_], Step <: HooksApi.SubsequentStep[ | ||
Ctx, | ||
CtxFn | ||
]]( | ||
api: HooksApi.Secondary[Ctx, CtxFn, Step] | ||
): Secondary[Ctx, CtxFn, Step] = | ||
new Secondary(api) | ||
} | ||
|
||
object syntax extends HooksApiExt | ||
} |
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
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
12 changes: 12 additions & 0 deletions
12
modules/core/shared/src/main/scala/crystal/ThrottlingViewF.scala
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,12 @@ | ||
// Copyright (c) 2016-2023 Association of Universities for Research in Astronomy, Inc. (AURA) | ||
// For license information see LICENSE or https://opensource.org/licenses/BSD-3-Clause | ||
|
||
package crystal | ||
|
||
/** | ||
* Result from `ViewThrottlerF`. | ||
*/ | ||
case class ThrottlingViewF[F[_], G[_], A]( | ||
throttlerView: ViewF[F, A], | ||
throttledView: ViewF[G, A] | ||
) |
82 changes: 82 additions & 0 deletions
82
modules/core/shared/src/main/scala/crystal/ViewThrottlerF.scala
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,82 @@ | ||
// Copyright (c) 2016-2023 Association of Universities for Research in Astronomy, Inc. (AURA) | ||
// For license information see LICENSE or https://opensource.org/licenses/BSD-3-Clause | ||
|
||
package crystal | ||
|
||
import cats.effect.Ref | ||
import cats.effect.Temporal | ||
import cats.effect.syntax.all.* | ||
import cats.syntax.all.* | ||
import cats.~> | ||
|
||
import scala.concurrent.duration.FiniteDuration | ||
|
||
/** | ||
* Given a `ViewF`, creates two `ViewF`s over the same value: | ||
* - A `throttledView`, which can be paused. While paused, it accumulates updates and applies them | ||
* all at once upon timeout. The callback is called only once and only to the function provided | ||
* in the last update during the pause. If unpaused, it acts as a normal `ViewF`. | ||
* - A `throttlerView`, which will pause the `throttledView` whenever it is modified. | ||
* This is particularly useful for values that can be both updated from a UI and from a server. The | ||
* `throttlerView` should be used in the UI, while the `throttledView` should be used for the server | ||
* updates. This way, the server updates will pause whenever the user changes a value. If the server | ||
* sends updates for every changed value, the throttling will avoid the UI from glitching between | ||
* old and new values when the UI is updated quickly. | ||
* @typeparam | ||
* F The sync effect. Your `ViewF` should be in this effect. | ||
* @typeparam | ||
* G The async effect. This will be used for concurrency. | ||
*/ | ||
final class ViewThrottlerF[F[_], G[_], A] private ( | ||
waitUntil: Ref[G, FiniteDuration], | ||
nextUpdate: Ref[G, Option[ViewThrottlerF.ModCBType[G, A]]], | ||
timeout: FiniteDuration, | ||
syncToAsync: F ~> G, | ||
dispatchAsync: G[Unit] => F[Unit] | ||
)(using G: Temporal[G]) { | ||
|
||
private def throttle: F[Unit] = | ||
dispatchAsync: | ||
G.monotonic.flatMap(now => waitUntil.set(now + timeout)) | ||
|
||
private def throttlerView(view: ViewF[F, A]): ViewF[F, A] = | ||
view.withOnMod(_ => throttle) | ||
|
||
private def attemptSet(modCB: (A => A, (A, A) => G[Unit]) => G[Unit]): G[Unit] = | ||
(waitUntil.get, G.monotonic).flatMapN: (waitUntil, now) => | ||
if (waitUntil > now) | ||
(G.sleep(waitUntil - now) >> attemptSet(modCB)).start.void | ||
else | ||
nextUpdate | ||
.flatModify: accumModCb => | ||
(none, accumModCb.map((mod, cb) => modCB(mod, cb)).getOrElse(G.unit)) | ||
.flatTap(_ => G.unit) | ||
|
||
private def throttledView(view: ViewF[F, A]): ViewF[G, A] = | ||
new ViewF[G, A]( | ||
get = view.get, | ||
modCB = (mod, cb) => | ||
nextUpdate.update: x => | ||
x match | ||
case None => (mod, cb).some | ||
case Some(oldMod, _) => (oldMod.andThen(mod), cb).some // We only keep last CB | ||
>> | ||
attemptSet: (newMod, newCB) => | ||
syncToAsync(view.modCB(newMod, (oldA, newA) => dispatchAsync(newCB(oldA, newA)))) | ||
) | ||
|
||
def throttle(view: ViewF[F, A]): ThrottlingViewF[F, G, A] = | ||
ThrottlingViewF(throttlerView(view), throttledView(view)) | ||
} | ||
|
||
object ViewThrottlerF { | ||
private type ModCBType[F[_], A] = (A => A, (A, A) => F[Unit]) | ||
|
||
def apply[F[_], G[_], A]( | ||
timeout: FiniteDuration, | ||
syncToAsync: F ~> G, | ||
dispatchAsync: G[Unit] => F[Unit] | ||
)(using G: Temporal[G]): G[ViewThrottlerF[F, G, A]] = | ||
(G.monotonic.flatMap(G.ref(_)), G.ref(none[ModCBType[G, A]])) | ||
.mapN(new ViewThrottlerF(_, _, timeout, syncToAsync, dispatchAsync)) | ||
} |
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
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
85 changes: 85 additions & 0 deletions
85
modules/tests/shared/src/test/scala/crystal/ViewThrottlerFSpec.scala
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,85 @@ | ||
// Copyright (c) 2016-2023 Association of Universities for Research in Astronomy, Inc. (AURA) | ||
// For license information see LICENSE or https://opensource.org/licenses/BSD-3-Clause | ||
|
||
package crystal | ||
|
||
import cats.arrow.FunctionK | ||
import cats.effect.IO | ||
import cats.effect.testkit.TestControl | ||
import fs2.Stream | ||
import munit.CatsEffectSuite | ||
|
||
import scala.concurrent.duration.* | ||
|
||
class ViewThrottlerFSpec extends munit.CatsEffectSuite: | ||
def server[A](values: (A => A, FiniteDuration)*): Stream[IO, A => A] = | ||
Stream | ||
.emits(values) | ||
.evalMap: (mod, t) => | ||
IO.sleep(t).as(mod) | ||
.prefetchN(Int.MaxValue) | ||
|
||
def plus(a: Int): Int => Int = _ + a | ||
|
||
def buildViews( | ||
timeout: FiniteDuration | ||
): IO[(ViewF[IO, Int], ViewF[IO, Int], IO[List[(Int, FiniteDuration)]])] = | ||
for | ||
view <- AccumulatingViewF.of[IO, Int](0) | ||
throttler <- ViewThrottlerF[IO, IO, Int](timeout, FunctionK.id[IO], identity) | ||
yield | ||
val throttlingView = throttler.throttle(view) | ||
(throttlingView.throttlerView, throttlingView.throttledView, view.accumulated.map(_.toList)) | ||
|
||
test("server modifies immediately if unthrottled"): | ||
TestControl | ||
.executeEmbed: | ||
for | ||
(_, throttledView, getAccum) <- buildViews(10.millis) | ||
_ <- | ||
server(plus(1) -> 1.millis, plus(1) -> 1.millis, plus(1) -> 1.millis) | ||
.evalMap(throttledView.mod) | ||
.compile | ||
.drain | ||
accum <- getAccum | ||
yield accum.toList | ||
.assertEquals(List(0 -> 0.millis, 1 -> 1.millis, 2 -> 2.millis, 3 -> 3.millis)) | ||
|
||
test("respects throttling") { | ||
TestControl | ||
.executeEmbed: | ||
for | ||
(throttlerView, throttledView, getAccum) <- buildViews(100.millis) | ||
_ <- | ||
server(plus(1) -> 10.millis, plus(1) -> 20.millis, plus(1) -> 200.millis) | ||
.evalMap(throttledView.mod) | ||
.compile | ||
.drain | ||
.background | ||
.use: result => | ||
IO.sleep(1.millis) >> throttlerView.mod(plus(1)) >> result.flatMap(_.embedError) | ||
accum <- getAccum | ||
yield accum.toList | ||
.assertEquals(List(0 -> 0.millis, 1 -> 1.millis, 3 -> 101.millis, 4 -> 230.millis)) | ||
} | ||
|
||
test("respects multiple throttles") { | ||
TestControl | ||
.executeEmbed: | ||
for | ||
(throttlerView, throttledView, getAccum) <- buildViews(100.millis) | ||
_ <- | ||
server(plus(1) -> 20.millis) | ||
.evalMap(throttledView.mod) | ||
.compile | ||
.drain | ||
.background | ||
.use: result => | ||
IO.sleep(10.millis) >> throttlerView.mod(plus(1)) >> | ||
IO.sleep(40.millis) >> throttlerView.mod(plus(1)) >> | ||
result.flatMap(_.embedError) | ||
_ <- IO.sleep(101.millis) // Wait for background threads | ||
accum <- getAccum | ||
yield accum.toList | ||
.assertEquals(List(0 -> 0.millis, 1 -> 10.millis, 2 -> 50.millis, 3 -> 150.millis)) | ||
} |
Oops, something went wrong.