class Fibril::AsyncProxy

Attributes

target[RW]

Public Class Methods

new(target) click to toggle source
# File lib/fibril/async_proxy.rb, line 4
def initialize(target)
  self.target = target
end

Public Instance Methods

method_missing(name, *_args, &_block) click to toggle source

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

# 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