Skip to content

Instantly share code, notes, and snippets.

@drernie
Created January 28, 2010 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drernie/289016 to your computer and use it in GitHub Desktop.
Save drernie/289016 to your computer and use it in GitHub Desktop.
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