class Object

Public Instance Methods

asleep(sec) click to toggle source
# File lib/asleep.rb, line 1
def asleep(sec)
  $eloop.call_later(Fiber.current, sec, $eloop.item[:callback])
  Fiber.yield
end
async(obj) click to toggle source
# File lib/async.rb, line 1
def async(obj)

  def wrapper(func, *args)
    local_fiber = Fiber.new do
      if func.instance_of?UnboundMethod
        # TODO: write unit tests with corner cases for this line
        func = func.bind(self)
      end
      func.call(*args)
    end
    local_fiber
  end

  if instance_of?Object
    original_fn = method(obj)
    define_method(obj) { |*args| wrapper(original_fn, *args) }
  elsif instance_of?Class

    # handling class methods
    if self.methods.include?obj
      original_fn = self.method(obj)
      class_eval {define_method(:a_class_method) { |*args| wrapper(original_fn, *args) }}

    # handling instance methods
    elsif self.instance_methods.include?obj
      original_fn = instance_method(obj)
      define_method(obj) { |*args| wrapper(original_fn, *args) }

    else
      raise 'Unhandled case'
    end

   end
end
await(wait_for_obj) click to toggle source
# File lib/await.rb, line 1
def await(wait_for_obj)
  $eloop.call_later(wait_for_obj, 0, Fiber.current)
  Fiber.yield
end
wrapper(func, *args) click to toggle source
# File lib/async.rb, line 3
def wrapper(func, *args)
  local_fiber = Fiber.new do
    if func.instance_of?UnboundMethod
      # TODO: write unit tests with corner cases for this line
      func = func.bind(self)
    end
    func.call(*args)
  end
  local_fiber
end