Class: Dispatch::Proxy

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/dispatch/proxy.rb

Overview

Serialize or asynchronize access to a delegate object. Forwards method invocations to the passed object via a private serial queue, and can call back asynchronously if given a block

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(delegate, group = Group.new, queue = Dispatch::Queue.concurrent) ⇒ Proxy

Create Proxy to wrap the given delegate, optionally specify group and queue for asynchronous callbacks



14
15
16
17
18
19
20
# File 'lib/dispatch/proxy.rb', line 14

def initialize(delegate, group=Group.new, queue=Dispatch::Queue.concurrent)
  super(delegate)
  @__serial__ = Dispatch::Queue.for(self)
  @__group__ = group
  @__queue__ = queue
  @__retval__ = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args, &block) ⇒ Object

Call methods on the delegate object via a private serial queue Returns asychronously if given a block; else synchronously



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/dispatch/proxy.rb', line 25

def method_missing(symbol, *args, &block)
  if block.nil? then
    @__serial__.sync { @__retval__ = __getobj__.__send__(symbol,*args) }
    return @__retval__
  end
  queue = @__queue__ # copy in case it changes while in flight
  @__serial__.async(@__group__) do
    retval = __getobj__.__send__(symbol, *args)
    queue.async(@__group__) { block.call(retval) }
  end
end

Instance Attribute Details

#__group__Object

Returns the value of attribute __group__.



10
11
12
# File 'lib/dispatch/proxy.rb', line 10

def __group__
  @__group__
end

#__queue__Object

Returns the value of attribute __queue__.



10
11
12
# File 'lib/dispatch/proxy.rb', line 10

def __queue__
  @__queue__
end

#__sync__Object

Returns the value of attribute __sync__.



10
11
12
# File 'lib/dispatch/proxy.rb', line 10

def __sync__
  @__sync__
end

Instance Method Details

#__value__Object

Return the delegate object after waiting



43
44
45
46
# File 'lib/dispatch/proxy.rb', line 43

def __value__
  __wait__
  __getobj__
end

#__wait__Object

Wait until the internal private queue has completed pending executions



38
39
40
# File 'lib/dispatch/proxy.rb', line 38

def __wait__
  @__serial__.sync { }
end