Sha256: dc53fb1921644c6412120c3c67ee9a43828a4aae550c6ad669bbec8e8b62cca6

Contents?: true

Size: 1 KB

Versions: 3

Compression:

Stored size: 1 KB

Contents

# frozen_string_literal: true
class Fiber
  module Collector
    class Any
      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? && t.error.nil? }
            return task.result unless task.nil?
            raise @tasks.first.error if @tasks.all? { |t| t.done? && t.error }
            sleep 0.001
          end
        else
          elapsed = 0
          loop do
            task = @tasks.find { |t| t.done? && t.error.nil? }
            return task.result unless task.nil?
            raise @tasks.first.error if @tasks.all? { |t| t.done? && t.error }
            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/any.rb
fiber-collector-0.1.1 lib/fiber/collector/builder/any.rb
fiber-collector-0.1.0 lib/fiber/collector/builder/any.rb