Browse files

re-introduce sendback

  • Loading branch information...
1 parent 38f7c3c commit 5f8ce6568ecba98ce7c484939d97c4934c5b624b @mental committed Jan 17, 2011
Showing with 62 additions and 0 deletions.
  1. +23 −0 README.md
  2. +29 −0 spec/ActorSpec.coffee
  3. +10 −0 src/Actor.coffee
View
23 README.md
@@ -453,6 +453,7 @@ rule are:
* `WebActors.spawn`
* `WebActors.send`
* `WebActors.kill`
+ * `WebActors.sendbackTo`
### WebActors.spawn(body) -> actorId
@@ -554,6 +555,28 @@ actor is sent a kill message by an actor it is linked to,
the link will remain in place until the actor exits or
it calls `unlink`.
+## Callbacks and "Sendbacks"
+
+Most Javascript APIs are heavily callback-based, but it
+isn't possible to run a callback in the context of an
+actor. However, WebActors includes utilities for bridging
+the gap between callbacks and actors.
+
+### WebActors.sendback(function (args...) { ... })
+
+Generates a callback function which sends a message to the
+current actor (at the time `sendback` is called). The
+content of the message is determined by what the function
+returns passed to `sendback`; it receives the callback
+arguments.
+
+### WebActors.sendbackTo(actorId, function (args...) { ... })
+
+Like `sendback`, but `sendbackTo` allows you to set up
+the callback to send a message to a different actor.
+
+`sendbackTo` may be called outside an actor.
+
## Pattern Matching
WebActors also provides a utility function for performing
View
29 spec/ActorSpec.coffee
@@ -296,6 +296,35 @@ describe "A WebActors Actor", ->
waitsFor -> actor_id is null
+describe "WebActors.sendback", ->
+ it "should send messages to creating actor based on callback arguments", ->
+ callback = null
+ passed = false
+
+ WebActors.spawn ->
+ callback = WebActors.sendback (args...) -> args
+ WebActors.receive ["foo", "bar"], ->
+ passed = true
+
+ waitsFor -> callback
+
+ runs -> callback("foo", "bar")
+
+ waitsFor -> passed
+
+describe "WebActors.sendbackTo", ->
+ it "should send messages to designated actor based on callback arguments", ->
+ passed = false
+
+ actor_id = WebActors.spawn ->
+ WebActors.receive ["foo", "bar"], ->
+ passed = true
+
+ callback = WebActors.sendbackTo actor_id, (args...) -> args
+ callback("foo", "bar")
+
+ waitsFor -> passed
+
describe "WebActors._injectEvent", ->
it "should inject message events", ->
received = []
View
10 src/Actor.coffee
@@ -298,6 +298,14 @@ reportError = (message) ->
setErrorHandler = (callback) ->
error_handler = callback
+sendback = (f) ->
+ actor_id = self()
+ sendbackTo actor_id, f
+
+sendbackTo = (actor_id, f) ->
+ (args...) ->
+ WebActors.send actor_id, f.apply(this, args)
+
WebActors.spawn = spawn
WebActors.spawnLinked = spawnLinked
WebActors.send = send
@@ -308,6 +316,8 @@ WebActors.trapKill = trapKill
WebActors.kill = kill
WebActors.link = link
WebActors.unlink = unlink
+WebActors.sendback = sendback
+WebActors.sendbackTo = sendbackTo
WebActors._injectEvent = injectEvent
WebActors._registerGateway = registerGateway

0 comments on commit 5f8ce65

Please sign in to comment.