Sha256: 2ec50037cac25b927349f3cc502d5ea895b7ef7b9b6538689ed9dd7eab237ab9

Contents?: true

Size: 1008 Bytes

Versions: 3

Compression:

Stored size: 1008 Bytes

Contents

# frozen_string_literal: true
class Fiber
  module Collector
    class Race
      def initialize(procs)
        @tasks = procs.map { Task.new(_1) }
      end

      def wait(timeout)
        raise "must run on non-blocking fiber" if Fiber.current.blocking?
        @tasks.each do |task|
          Fiber.schedule { task.run }
        end
        if timeout.nil?
          loop do
            task = @tasks.find { |t| t.done? }
            unless task.nil?
              raise task.error unless task.error.nil?
              return task.result
            end
            sleep 0.001
          end
        else
          elapsed = 0
          loop do
            task = @tasks.find { |t| t.done? }
            unless task.nil?
              raise task.error unless task.error.nil?
              return task.result
            end
            t = 0.001
            sleep t
            elapsed += t
            raise "timeout" if elapsed > timeout
          end          
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fiber-collector-1.0.0 lib/fiber/collector/builder/race.rb
fiber-collector-0.1.1 lib/fiber/collector/builder/race.rb
fiber-collector-0.1.0 lib/fiber/collector/builder/race.rb