基於紙張Deprecating the Observer Pattern with Scala.React我試圖從紙建立一個簡單的例子,但它扔一個異常Exception in thread "main" java.lang.AssertionError: assertion failed: This method must be run on its domain [email protected]
斯卡拉簡單示例陣營
一個相關的問題是Running a simple Scala.React expression。
如何設置所有使用scala反應庫的強大功能?
除了庫scala-react,我用下面的例子:
object MyFirstReact extends App {
object MyDomain extends scala.react.Domain {
protected val scheduler: Scheduler = new ManualScheduler
protected val engine: Engine = new Engine
}
import MyDomain._
case class MouseEvent(position: (Int, Int))
class Path(var positions: Seq[(Int, Int)]) {
def this(pos: (Int, Int)) = this(Seq(pos))
def lineTo(pos: (Int, Int)) { positions = positions :+ pos }
def close { positions = positions :+ positions.head }
}
val mouseDown: Events[MouseEvent] = Events.once(MouseEvent((0, 0)))
val mouseMove: Events[MouseEvent] = Events.once(MouseEvent((1, 1)))
val mouseUp: Events[MouseEvent] = Events.once(MouseEvent((2, 2)))
def draw(path: Path) { /* ... */ }
Reactor.loop { self =>
// step 1
val path = new Path((self await mouseDown).position)
self.loopUntil(mouseUp) { // step 2
val m = self awaitNext mouseMove
path.lineTo(m.position)
draw(path)
}
path.close // step 3
draw(path)
}
}