scala - Throttle or debounce method calls -


let's have method permits update date in db:

def updatelastconsultationdate(userid: string): unit = ??? 

how can throttle/debounce method won't run more once hour per user.

i'd simplest possible solution, not based on event-bus, actor lib or persistence layer. i'd in-memory solution (and aware of risks).

i've seen solutions throttling in scala, based on akka throttler, looks me overkill start using actors throttling method calls. isn't there simple way that?

edit: seems not clear enough, here's visual representation of want, implemented in js. can see, throttling may not filtering subsequent calls, postponing calls (also called trailing events in js/lodash/underscore). solution i'm looking can't based on pure-synchronous code only.

this sounds great job reactivex-based solution. on scala, monix favorite one. here's ammonite repl session illustrating it:

import $ivy.`io.monix::monix:2.1.0` // i'm using ammonite's magic imports, it's equivalent adding "io.monix" %% "monix" % "2.1.0" libraryimports in sbt  import scala.concurrent.duration.durationint import monix.reactive.subjects.concurrentsubject import monix.reactive.consumer import monix.execution.scheduler.implicits.global import monix.eval.task  class dbupdater {   val publish = concurrentsubject.publish[string]   val throttled = publish.throttlefirst(1 hour)   val cancelhandle = throttled.consumewith(     consumer.foreach(userid =>       println(s"update database $userid here")))     .runasync    def updatelastconsultationdate(userid: string): unit = {     publish.onnext(userid)   }    def stop(): unit = cancelhandle.cancel() } 

yes, , scala.js code work in browser, too, if it's important you.


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -