title: Simple Example for Scala React
body: <p>Based on the paper <a href="http://infoscience.epfl.ch/record/176887/files/DeprecatingObservers2012.pdf" rel="nofollow noreferrer">Deprecating the Observer Pattern with Scala.React</a> I tried to set up a simple example from the paper, but it throwed an exception <code>Exception in thread "main" java.lang.AssertionError: assertion failed: This method must be run on its domain scala.react.NilDebug@1502c065</code></p>
  
  <p>A related question is <a href="https://stackoverflow.com/questions/17217377/running-a-simple-scala-react-expression">Running a simple Scala.React expression</a>.</p>
  
  <p>How do I set up everything to use the great power of the scala react library?</p>
  
  <p>Besides the library <a href="https://github.com/ingoem/scala-react" rel="nofollow noreferrer">scala-react</a>, I used the following example: </p>
  
  <pre><code>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 =&gt;
      // 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)
    }
  
  }
  </code></pre>