Last active
September 10, 2016 05:33
-
-
Save jemc/606710761a52e1a3ad11b040c0bf5e1c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use "collections" | |
use "debug" | |
interface tag InboxTarget[M: Any val] | |
be process(m: M) | |
actor Inbox[M: Any val] | |
""" | |
An inbox that holds the latest posted message of type M, | |
forwarding the message to the current target if and when the target is ready. | |
""" | |
var target: (InboxTarget[M] | None) = None | |
var latest: (M | None) = None | |
var waiting: Bool = false | |
be set_target(t: InboxTarget[M]) => | |
target = t | |
waiting = true | |
be post(m: M) => | |
latest = m | |
if waiting then next() end | |
be next() => | |
""" | |
Forward the latest message to the current target if one is available, | |
otherwise move into the waiting state where we will want to send the next | |
message that arrives immediately to the current target. | |
""" | |
match (latest = None) | let m: M => | |
try | |
(target as InboxTarget[M]).process(m) | |
waiting = false | |
end | |
else | |
waiting = true | |
end | |
class val Transaction | |
let name: String | |
// ... more fields would go here | |
new val create(n: String) => | |
name = n | |
actor TransactionWatcher | |
let inbox: Inbox[Transaction] | |
new create(i: Inbox[Transaction]) => | |
inbox = i | |
inbox.set_target(this) | |
be process(t: Transaction) => | |
let busywork: String ref = String | |
for i in Range(0, 1000) do | |
busywork.append("pushing paper...") | |
end | |
Debug("saw transaction " + t.name) | |
inbox.next() | |
actor Main | |
new create(env: Env) => | |
let inbox = Inbox[Transaction] | |
TransactionWatcher(inbox) | |
for i in Range(0, 10000) do | |
inbox.post(Transaction("foo " + i.string())) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
saw transaction foo 8 | |
saw transaction foo 9 | |
saw transaction foo 10 | |
saw transaction foo 11 | |
saw transaction foo 133 | |
saw transaction foo 217 | |
saw transaction foo 259 | |
saw transaction foo 318 | |
saw transaction foo 376 | |
saw transaction foo 436 | |
saw transaction foo 494 | |
saw transaction foo 555 | |
saw transaction foo 615 | |
saw transaction foo 669 | |
saw transaction foo 728 | |
saw transaction foo 858 | |
saw transaction foo 925 | |
saw transaction foo 998 | |
saw transaction foo 1094 | |
saw transaction foo 1296 | |
saw transaction foo 1404 | |
saw transaction foo 1493 | |
saw transaction foo 1603 | |
saw transaction foo 1868 | |
saw transaction foo 2040 | |
saw transaction foo 2046 | |
saw transaction foo 2086 | |
saw transaction foo 2508 | |
saw transaction foo 2783 | |
saw transaction foo 2859 | |
saw transaction foo 2928 | |
saw transaction foo 3543 | |
saw transaction foo 3984 | |
saw transaction foo 4107 | |
saw transaction foo 4225 | |
saw transaction foo 5234 | |
saw transaction foo 5933 | |
saw transaction foo 6136 | |
saw transaction foo 6328 | |
saw transaction foo 7974 | |
saw transaction foo 9097 | |
saw transaction foo 9416 | |
saw transaction foo 9728 | |
saw transaction foo 9999 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment