Created
January 28, 2010 18:52
-
-
Save drernie/289016 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
module Dispatch | |
class Actor | |
# Create an Actor that serializes or asynchronizes the given object | |
# Will invoke and call-back asynchronously if provide a block | |
# Note that this will NOT work for methods that themselves expect a block | |
def initialize(actee, callback=nil) | |
@actee = actee | |
@callback = callback || Dispatch::Queue.concurrent | |
@q = Dispatch::Queue.new("dispatch.actor.#{actee}.#{object_id}") | |
end | |
def _on(callback) | |
@callback = callback | |
end | |
def method_missing(symbol, *args, &block) | |
if block_given? | |
@q.async do | |
retval = @actee.send(symbol, *args) | |
@callback.async { block.call(retval) } | |
end | |
return nil | |
else | |
@retval = nil | |
@q.sync { @retval = @actee.send(symbol, *args) } | |
return @retval | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment