I am trying to write a simple scala.js application. The problem I now have is that I can't figure out why I fail to save data which I get from the server / user.
To be more concrete, here are code snippets:
<!-- in my html -->
<script>
example.RoomFrontend().setName("@name");
</script>
// in my scala.js src
@JSExport
object RoomFrontend extends js.JSApp {
var username: Option[String] = None
@JSExport
def setName(name: String): Unit = {
username = Some(name)
g.console.debug(s"Got user name: $name")
}
case object TestUserMessage
class Render(ctx: dom.CanvasRenderingContext2D) extends Actor {
override def receive: Receive = {
case TestUserMessage => g.console.debug(s"user name is $username")
}
}
@JSExport
def main(): Unit = {
//...
// renderer is akka actorRef
dom.window.setInterval(() => render ! TestUserMessage, 50)
}
So, what happens is the following. I succesfully enter user name, e.g. Blah, I see nice message in the js console saying "Got user name: Blah", but the problem is, each time renderer is called, it says username is None.
I imagine it can be caused by my faulty assigning to a variable in one thread (in js main thread, I don't know is it correct definition), and reading this variable from actor thread... But the same story happends with variables stored in actor itself.
I think I don't understand something basic about how one should use scala.js, for I'm new both to scala and js. Please, can somebody explain this odd behavior?