【问题标题】:Using Timer from scala.rx使用来自 scala.rx 的定时器
【发布时间】:2014-08-30 21:34:27
【问题描述】:

谁能给我一个简单的例子,使用李浩毅的 scala.rx 中的 Timer,它不依赖于 Akka 或除 scalajs、dom 和 rx 之外的任何其他库?

好易的 GitHub 上的一个 Timer 的例子是:

import scala.concurrent.duration._
implicit val scheduler = new AkkaScheduler(akka.actor.ActorSystem())

val t = Timer(100 millis)
var count = 0  
val o = Obs(t){
  count = count + 1
}

println(count) // 3
println(count) // 8
println(count) // 13

但是,这使用了 Akka。

查看scala.rx api,创建rx.ops.Timer的方式是:

new Timer(interval: FiniteDuration, delay: FiniteDuration)(implicit scheduler: Scheduler, p: Propagator[P], ec: ExecutionContext)

其中 Scheduler 是一个 trait 定义为:

abstract def scheduleOnce[T](interval: FiniteDuration)(thunk: ⇒ T)(implicit executor: ExecutionContext): Unit

Scheduler 是 JVM 上的 Akka ActorSystem 和 JavaScript 中的 setTimeout 函数。”

虽然 api 中的所有信息都很有用,但我仍然无法获得简单计时器的正确语法。

【问题讨论】:

    标签: scala timer scala.js scala.rx


    【解决方案1】:

    如果我从文档中正确理解,您只需在范围内提供隐式 DomScheduler 而不是 AkkaScheduler

    import rx.ops._
    
    implicit val scheduler = new DomScheduler
    

    【讨论】:

      猜你喜欢
      • 2016-01-30
      • 2018-08-10
      • 2019-05-20
      • 1970-01-01
      • 2014-03-20
      • 2011-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多