Skip to content

Instantly share code, notes, and snippets.

@fnumatic
Created November 23, 2012 11:28
Show Gist options
  • Save fnumatic/4135213 to your computer and use it in GitHub Desktop.
Save fnumatic/4135213 to your computer and use it in GitHub Desktop.
Simple Swing Application with scala.react * github.com/ingoem/scala-react
This is based on the scala.react library.
There should be an output on the console on every button click.
But nothing happens. Any clue?
import MySwingDomain._
object Gui extends ReactiveSwingApp {
val button = new MyButton("test")
def top = new MainFrame {
contents = new FlowPanel {
contents += button
}
}
schedule { ConsoleObserver().obs }
}
case class ConsoleObserver() extends Observing {
def obs {
observe(Gui.button.actionPerformed)(_ => println("click"))
}
}
import MySwingDomain._
class MyButton(text: String) extends Button(text) with HasAction
trait HasAction extends Observing {
this: AbstractButton =>
def getAction() = peer.getAction
def addActionListener(a: ActionListener) = peer.addActionListener(a)
def removeActionListener(a: ActionListener) = peer.removeActionListener(a)
val actionPerformed: Events[Action] =
new EventSource[Action](MySwingDomain.owner) {
source =>
addActionListener(new ActionListener {
def actionPerformed(e: ActionEvent) = { source emit getAction }
})
}
}
object MySwingDomain extends SwingDomain {
self:Domain =>
val scheduler = new SwingScheduler()
val engine = new Engine
}
abstract class SwingDomain extends Domain {
trait ReactiveSwingApp extends SimpleSwingApplication {
override def main(args: Array[String]) {
schedule { startup(args) }
start()
}
}
}
@nightscape
Copy link

Hi fnumatic, I am currently also experimenting with Scala.React (see https://github.com/nightscape/scala-react).
I haven't gotten Scala.React to work in my Android project, but I ran the benchmarks to see how a correct call stack looks like:

> java.lang.Thread.getStackTrace(Thread.java:1503)
> react.bench.chain.FlowEventChain$$anonfun$2.apply$mcVD$sp(FlowEventChain.scala:28)
> react.bench.chain.FlowEventChain$$anonfun$2.apply(FlowEventChain.scala:25)
> react.bench.chain.FlowEventChain$$anonfun$2.apply(FlowEventChain.scala:25)
> scala.react.ReactiveModule$Reactive.ifEmitting(ReactiveModule.scala:501)
> scala.react.ReactiveModule$Observing$Observer1.react(ReactiveModule.scala:678)
> scala.react.ReactiveModule$LeafNode$class.doValidatePulse(ReactiveModule.scala:236)
> scala.react.ReactiveModule$Observing$Observer.doValidatePulse(ReactiveModule.scala:646)
> scala.react.ReactiveModule$Node.validatePulse(ReactiveModule.scala:81)
> scala.react.ReactiveModule$StrictNode$class.tock(ReactiveModule.scala:212)
> scala.react.ReactiveModule$Observing$Observer.tock(ReactiveModule.scala:646)
> scala.react.EngineModule$Propagator.tryTock(EngineModule.scala:145)
> scala.react.EngineModule$Propagator.propagate(EngineModule.scala:112)
> scala.react.EngineModule$Propagator.runTurn(EngineModule.scala:77)
> react.bench.ReactiveBenchmark.innerLoop(ReactiveBenchmark.scala:19)
> react.bench.Benchmark.measure(Benchmark.scala:46)
> react.bench.Benchmark.runBenchmark(Benchmark.scala:79)
> react.bench.ReactiveBenchmark.main(ReactiveBenchmark.scala:25)
> scala.react.Domain$ReactiveApp$class.main(Domain.scala:40)
> react.bench.ReactiveBenchmark.main(ReactiveBenchmark.scala:11)
> react.bench.chain.FlowEventChain.main(FlowEventChain.scala)

Hope this helps a little. I try to keep you up-to-date with my findings.

@nightscape
Copy link

Hi again,

after digging and debugging through the code I found the culprit (see my fork of your gist)

source emit getAction

needs to be a

source << getAction

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment