Class: Fibril::AsyncProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/fibril/async_proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ AsyncProxy

Returns a new instance of AsyncProxy.



4
5
6
# File 'lib/fibril/async_proxy.rb', line 4

def initialize(target)
  self.target = target
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *_args, &_block) ⇒ Object

Execute target method on proxied target. Enqueue the current fibril to be resumed as soon as async task is finished



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fibril/async_proxy.rb', line 12

def method_missing(name, *_args, &_block)
  define_singleton_method(name) do |*args, &block|
    waiting = Fibril.current
    Thread.new do
      begin
        target.send(name, *args, &block).tap{ Fibril.enqueue waiting }
      rescue Exception => e
        puts "Exception! #{e}"
        Fibril.enqueue waiting
      end
    end.tap do
      Fibril.current.yield
    end.value
  end
  send(name, *_args, &_block)
end

Instance Attribute Details

#targetObject

Returns the value of attribute target.



2
3
4
# File 'lib/fibril/async_proxy.rb', line 2

def target
  @target
end