Reputation: 8392
I am trying to combine the use of SWT with Akka. One of the SWT widgets is a Browser
, which embeds a web browser, and allows JS code to call JVM code via callback functions in BrowserFunction
objects.
I have the following code:
import org.eclipse.swt.widgets.Composite
import org.eclipse.swt.browser._
import akka.actor.Actor
class MyActor(parentComposite: Composite) extends Actor {
private var mutableContent: Any = ???
val browser = new Browser(parentComposite, SWT.BORDER)
val browserFunction = new BrowserFunction(browser, "JS_CallableFunctionName") {
val ref = context.self
override def function(arguments: Array[Object]): Object = {
ref ! "Is it safe to send a message to myself?"
null
}
}
def receive = {
case _ => ???
}
}
Are there any risks with an actor sending a message to itself inside one of these callbacks?
Upvotes: 3
Views: 954
Reputation: 473
There shouldn't be a problem when an actor sends a message to self. In essence, that would just add the message to the actor's mailbox which the actor would later process. The problems arise when you expose the actor's internal variables or state to the outside world. That would happen for example if your browserFunction would return the 'mutableContent' which is not the case.
That being said, it seems to me that a better approach would be to create the Actor separately from the Browser and then safely pass its ActorRef to the BrowserFunction (also created outside the Actor). By doing this, you can be sure you will never share the actor's internal state with any callback that is executed outside the actor loop. Any communication would happen through the Actor's messaging.
Upvotes: 5