Sha256: 47e3204dd716a09ab1198262c513b953213f0e8f80ca4c713fb675ea0426d40a

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

require 'fiber'

begin
  require 'eventmachine/sync-defer'
rescue LoadError
end

begin
  require 'cool.io/sync-defer'
rescue LoadError
end

module SyncDefer
  # assume we would always require 'rest-core' in root fiber
  RootFiber = Fiber.current

  module_function
  def defer *args, &block
    if root_fiber?
      fallback("Not called inside a fiber.", *args, &block)

    elsif Object.const_defined?(:EventMachine) &&
          EventMachine.reactor_running?
      EventMachine::SyncDefer.defer(*args, &block)

    elsif Object.const_defined?(:Coolio) &&
          Coolio::Loop.default.has_active_watchers?
      Coolio::SyncDefer.defer(*args, &block)

    else
      fallback("No reactor found.", *args, block)
    end
  end

  def root_fiber?
    RootFiber == Fiber.current
  end

  def fallback message, *args, &block
    $stderr.puts("SyncDefer: WARN: #{message}")
    $stderr.puts("           Falling back to run the computation directly.")
    $stderr.puts("           Called from: #{caller.last(5).inspect}")
    args << block if block_given?
    if args.size == 1
      args.first.call
    else
      args.map(&:call)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sync-defer-0.9.5 lib/sync-defer.rb