Permalink
Browse files

ditch capturing

  • Loading branch information...
1 parent 492db62 commit 37a9067c2b0b15608a9f995c437bd75e1b762fbc @mental committed Jan 4, 2011
Showing with 97 additions and 112 deletions.
  1. +49 −35 README.md
  2. +19 −19 spec/ActorSpec.coffee
  3. +14 −28 spec/MatchSpec.coffee
  4. +7 −4 spec/WorkerSpec.coffee
  5. +1 −1 spec/WorkerSpecStub.coffee
  6. +1 −1 src/Actor.coffee
  7. +6 −24 src/Match.coffee
View
@@ -98,8 +98,8 @@ function. It takes a pattern and a callback to be invoked
when a matching message is received.
function aCallback() {
- // $ARG matches anything
- WebActors.receive(WebActors.$ARG, function (message) {
+ // ANY matches anything
+ WebActors.receive(WebActors.ANY, function (message) {
alert(message);
});
}
@@ -122,31 +122,57 @@ for them (by calling `receive` with a matching pattern).
### Patterns
The first argument to `receive` is a pattern which is used
-to match an incoming message and extract values from it.
-In case of a match, values are passed as arguments to the
-callback.
+to match an incoming message. Patterns are ordinary
+JavaScript values.
-The simplest possible pattern is `WebActors.ANY`, which
-matches anything without extracting any values from it.
+#### Primitive Values
-The second simplest pattern is `WebActors.$ARG`, which
-also matches anything, but additionally extracts the value
-as a parameter for the callback. Most of our examples
-use this.
+Used as a pattern, any primitive value matches an
+equivalent primitive value (as determined by `===`).
-Patterns can also be ordinary JavaScript values. When a
-message is an array, pattern matching can also be performed
-on individual array elements; for example:
+ 3 matches 3
+ "foo" matches "foo"
- WebActors.receive ["foo", WebActors.$ARG, WebActors.$ARG], (a, b) ->
+ 3 does not match 4
+ "3" does not match 3
+ 3 does not match "3"
+ "foo" does not match "bar"
-Will match three-element arrays whose first element is "foo",
-and pass the remaining two elements as arguments to the callback.
+#### Arrays
+
+When used as a pattern, an Array matches an array with
+the same length and matching elements (as determined by the
+pattern-matching rules)
+
+ [1, 2, 3] matches [1, 2, 3]
+
+ [1, 2, 3] does not match [4, 5, 6]
+ [1, 2, 3] does not match [1, 2, 3, 4]
+
+#### Objects
+
+When used as a pattern, an object matches any object with
+the same fields and matching values. (The matched object
+may have other fields in addition.)
+
+ {a: 1} matches {a: 1}
+ {a: 1} matches {a: 1, b: 2}
+
+ {a: 1} does not match {}
+ {a: 1} does not match {a: 3}
+ {a: 1} does not match {b: 1}
+
+#### Wildcards
+
+`WebActors.ANY` matches any JavaScript value.
+
+#### WebActors.match
If you want to, you can use WebActors pattern matching
-directly in your own non-actor code by using `WebActors.match`.
-`match` takes a pattern and a value, and returns a list of
-captured values in case of a match, `null` otherwise.
+directly in your own non-actor code by using
+`WebActors.match`. `match` takes a pattern and a value,
+and returns a truthy value in case of a match, or a falsy
+one otherwise.
### Saving Some Typing
@@ -158,11 +184,10 @@ aliases for functions defined on library objects.
var spawn = WebActors.spawn;
var receive = WebActors.receive;
var send = WebActors.send;
- var $ARG = WebActors.$ARG;
+ var ANY = WebActors.ANY;
function aCallback() {
- // $ARG matches anything
- receive($ARG, function (message) {
+ receive(ANY, function (message) {
alert(message);
});
}
@@ -344,15 +369,4 @@ successful, or `null` otherwise.
### WebActors.ANY
-When used in a pattern, `ANY` matches any value
-without capturing it.
-
-### WebActors.$ARG
-
-When used in a pattern, `$ARG` matches any value and
-includes it in the list of captured values returned
-by `WebActors.match`. The match may be further
-constrained by passing an argument to `$ARG` as a
-function. That is, `$ARG` will match anything, whereas
-`$ARG(foo)` will only match `foo` (where `foo`
-is a pattern).
+When used in a pattern, `ANY` matches any value.
View
@@ -1,6 +1,6 @@
+ANY = WebActors.ANY
+
describe "A WebActors Actor", ->
- $ARG = WebActors.$ARG
- ANY = WebActors.ANY
it "should have a unique ID", ->
actor_id_a = WebActors.spawn ->
@@ -20,7 +20,7 @@ describe "A WebActors Actor", ->
received = []
actor_id = WebActors.spawn ->
- WebActors.receive $ARG, (m) -> received.push(m)
+ WebActors.receive ANY, (m) -> received.push(m)
WebActors.send actor_id, expected
@@ -32,13 +32,13 @@ describe "A WebActors Actor", ->
received = []
actor_id = WebActors.spawn ->
- WebActors.receive $ARG("foo"), (m) ->
+ WebActors.receive "foo", (m) ->
received.push(m)
- WebActors.receive $ARG("baz"), (m) ->
+ WebActors.receive "baz", (m) ->
received.push(m)
- WebActors.receive $ARG("bar"), (m) ->
+ WebActors.receive "bar", (m) ->
received.push(m)
- WebActors.receive $ARG("baz"), (m) ->
+ WebActors.receive "baz", (m) ->
received.push(m)
WebActors.send actor_id, "bar"
@@ -64,7 +64,7 @@ describe "A WebActors Actor", ->
actor_a_id = WebActors.spawn ->
WebActors.trapKill (killer_id, reason) -> [killer_id, reason]
- WebActors.receive $ARG, (m) ->
+ WebActors.receive ANY, (m) ->
received.push m
actor_b_id = WebActors.spawn ->
@@ -80,7 +80,7 @@ describe "A WebActors Actor", ->
actor_a_id = WebActors.spawn ->
WebActors.trapKill (killer_id, reason) -> [killer_id, reason]
- WebActors.receive $ARG, (m) ->
+ WebActors.receive ANY, (m) ->
received.push m
actor_b_id = WebActors.spawn ->
@@ -95,7 +95,7 @@ describe "A WebActors Actor", ->
actor_a_id = WebActors.spawn ->
WebActors.trapKill (killer_id, reason) -> [killer_id, reason]
- WebActors.receive $ARG, (m) -> received.push m
+ WebActors.receive ANY, (m) -> received.push m
actor_b_id = WebActors.spawn ->
WebActors.link actor_a_id
@@ -112,7 +112,7 @@ describe "A WebActors Actor", ->
received = []
actor_a_id = WebActors.spawn ->
- WebActors.receive $ARG, (m) -> received.push m
+ WebActors.receive ANY, (m) -> received.push m
actor_b_id = WebActors.spawn ->
WebActors.kill actor_a_id, "foobar"
@@ -131,7 +131,7 @@ describe "A WebActors Actor", ->
WebActors.trapKill (killer_id, reason) -> [killer_id, reason]
WebActors.receive "go", ->
WebActors.kill actor_b_id, "testing"
- WebActors.receive $ARG, (m) -> received.push m
+ WebActors.receive ANY, (m) -> received.push m
actor_b_id = WebActors.spawn ->
WebActors.trapKill (killer_id, reason) -> throw "error"
@@ -195,7 +195,7 @@ describe "A WebActors Actor", ->
WebActors.receive "go", ->
WebActors.unlink actor_b_id
WebActors.send actor_b_id, "go"
- WebActors.receive $ARG, ->
+ WebActors.receive ANY, ->
actor_b_id = WebActors.spawn ->
WebActors.link actor_a_id
@@ -204,7 +204,7 @@ describe "A WebActors Actor", ->
setTimeout((-> WebActors.send root_id, "watchdog"), 100)
WebActors.receive "watchdog", -> passed = true
- WebActors.receive $ARG, ->
+ WebActors.receive ANY, ->
waitsFor -> passed
@@ -219,7 +219,7 @@ describe "A WebActors Actor", ->
WebActors.receive "go", ->
WebActors.receive [actor_a_id, null], -> passed = true
- WebActors.receive $ARG, ->
+ WebActors.receive ANY, ->
waitsFor -> passed
@@ -301,7 +301,7 @@ describe "WebActors.injectEvent", ->
received = []
actor_id = WebActors.spawn ->
- WebActors.receive WebActors.$ARG, (message) ->
+ WebActors.receive ANY, (message) ->
received.push(message)
WebActors.injectEvent {target_id:actor_id, event_name:"send", message:"foobar"}
@@ -315,7 +315,7 @@ describe "WebActors.injectEvent", ->
actor_a_id = WebActors.spawn ->
WebActors.trapKill (killer_id, reason) -> ["blah", killer_id, reason]
- WebActors.receive WebActors.$ARG, (message) ->
+ WebActors.receive ANY, (message) ->
received.push(message)
actor_b_id = WebActors.spawn ->
@@ -330,7 +330,7 @@ describe "WebActors.injectEvent", ->
actor_a_id = WebActors.spawn ->
WebActors.trapKill (killer_id, reason) -> ["blah", killer_id, reason]
- WebActors.receive WebActors.$ARG, (message) ->
+ WebActors.receive ANY, (message) ->
received.push(message)
actor_b_id = WebActors.spawn ->
@@ -350,7 +350,7 @@ describe "WebActors.injectEvent", ->
actor_id = WebActors.spawn ->
WebActors.trapKill (killer_id, reason) -> ["blah", killer_id, reason]
ready = true
- WebActors.receive WebActors.$ARG, (message) ->
+ WebActors.receive ANY, (message) ->
received.push(message)
waitsFor -> ready
View
@@ -1,45 +1,31 @@
describe "WebActors.match", ->
it "should exactly match numbers", ->
- expect(WebActors.match(3, 2)).toEqual null
- expect(WebActors.match(3, 3)).toEqual []
+ expect(WebActors.match(3, 2)).toBeFalsy()
+ expect(WebActors.match(3, 3)).toBeTruthy()
it "should exactly match strings", ->
- expect(WebActors.match("foobar", "barfoo")).toEqual null
- expect(WebActors.match("foobar", "foobar")).toEqual []
+ expect(WebActors.match("foobar", "barfoo")).toBeFalsy()
+ expect(WebActors.match("foobar", "foobar")).toBeTruthy()
it "should exactly match booleans", ->
for b in [true, false]
- expect(WebActors.match(b, not b)).toEqual null
- expect(WebActors.match(b, b)).toEqual []
+ expect(WebActors.match(b, not b)).toBeFalsy()
+ expect(WebActors.match(b, b)).toBeTruthy()
it "should not match dissimilar arrays", ->
- expect(WebActors.match(["a", "b"], ["a", "a"])).toEqual null
- expect(WebActors.match(["a", "b"], ["a", "b", "c"])).toEqual null
- expect(WebActors.match(["a", "b"], ["a"])).toEqual null
+ expect(WebActors.match(["a", "b"], ["a", "a"])).toBeFalsy()
+ expect(WebActors.match(["a", "b"], ["a", "b", "c"])).toBeFalsy()
+ expect(WebActors.match(["a", "b"], ["a"])).toBeFalsy()
it "should match identical arrays", ->
- expect(WebActors.match(["a", "b"], ["a", "b"])).toEqual []
-
- it "should support simple $ARG", ->
- expect(WebActors.match(WebActors.$ARG, 42)).toEqual [42]
+ expect(WebActors.match(["a", "b"], ["a", "b"])).toBeTruthy()
it "should match subsets of object fields", ->
- expect(WebActors.match({a: 3}, {a: 3, b: 4})).toEqual []
+ expect(WebActors.match({a: 3}, {a: 3, b: 4})).toBeTruthy()
it "should not match objects with missing fields", ->
- expect(WebActors.match({a: 3, b: 4}, {a: 3})).toEqual null
-
- it "should support restricted $ARG", ->
- expect(WebActors.match(WebActors.$ARG(42), 38)).toEqual null
- expect(WebActors.match(WebActors.$ARG(42), 42)).toEqual [42]
-
- it "should support restricted $ARG for strings", ->
- expect(WebActors.match(WebActors.$ARG("foo"), "bar")).toEqual null
- expect(WebActors.match(WebActors.$ARG("foo"), "foo")).toEqual ["foo"]
-
- it "should support destructuring $ARG", ->
- expect(WebActors.match([WebActors.$ARG, "b", WebActors.$ARG], ["a", "b", "c"])).toEqual ["a", "c"]
+ expect(WebActors.match({a: 3, b: 4}, {a: 3})).toBeFalsy()
it "should support wildcards", ->
- expect(WebActors.match(WebActors.ANY, 42)).toEqual []
- expect(WebActors.match(WebActors.ANY, "testing")).toEqual []
+ expect(WebActors.match(WebActors.ANY, 42)).toBeTruthy()
+ expect(WebActors.match(WebActors.ANY, "testing")).toBeTruthy()
@@ -1,3 +1,5 @@
+ANY = WebActors.ANY
+
spawn_helper = (method, body) ->
actor_id = WebActors[method]("spec/WorkerSpecStub.js")
body_string = "(#{body})"
@@ -10,12 +12,12 @@ describe "WebActors.spawnWorker", ->
WebActors.spawn ->
worker_id = spawn_helper 'spawnWorker', ->
- WebActors.receive WebActors.$ARG, (reply_id) ->
+ WebActors.receive WebActors.ANY, (reply_id) ->
WebActors.send reply_id, "it works!"
WebActors.send worker_id, WebActors.self()
- WebActors.receive WebActors.$ARG, (result) ->
+ WebActors.receive ANY, (result) ->
expect(result).toEqual("it works!")
done = true
@@ -27,12 +29,13 @@ describe "Workers", ->
WebActors.spawn ->
worker_id = spawn_helper 'spawnWorker', ->
- WebActors.receive [WebActors.$ARG], (reply_id) ->
+ WebActors.receive [WebActors.ANY], (m) ->
+ reply_id = m[0]
WebActors.send reply_id, "it works!"
WebActors.send worker_id, [WebActors.self()]
- WebActors.receive WebActors.$ARG, (result) ->
+ WebActors.receive ANY, (result) ->
expect(result).toEqual("it works!")
done = true
@@ -1,6 +1,6 @@
importScripts("../lib/webactors.js")
WebActors.initWorker ->
- WebActors.receive WebActors.$ARG, (body_source) ->
+ WebActors.receive WebActors.ANY, (body_source) ->
body = eval(body_source)
body.apply(this)
View
@@ -90,7 +90,7 @@ class LocalActor
receiver = (message) ->
captured = WebActors.match(pattern, message)
if captured
- -> cont.apply(this, captured)
+ -> cont.call(this, message)
else
null
@receivers.push receiver
Oops, something went wrong.

0 comments on commit 37a9067

Please sign in to comment.