Sha256: 1ce277e2361b661df09af32b5adf2da331f8ba7de57c1a12ab727441236e1896

Contents?: true

Size: 1.13 KB

Versions: 11

Compression:

Stored size: 1.13 KB

Contents

require "em/iterator"

module EventMachine
  module Synchrony

    class Iterator < EM::Iterator

      # synchronous iterator which will wait until all the
      # jobs are done before returning. Unfortunately this
      # means that you loose ability to choose concurrency
      # on the fly (see iterator documentation in EM)
      def each(foreach=nil, after=nil, &blk)
        fiber = Fiber.current

        fe = (foreach || blk)
        cb = Proc.new do
          after.call if after
          fiber.resume
        end

        Fiber.yield super(fe, cb)
      end

      def map(&block)
        fiber = Fiber.current
        result = nil

        after = Proc.new {|res| result = res; fiber.resume }
        super(block, after)

        Fiber.yield
        result
      end

      def inject(obj, foreach = nil, after = nil, &block)
        if foreach and after
          super(obj, foreach, after)
        else
          fiber = Fiber.current
          result = nil

          after = Proc.new {|res| result = res; fiber.resume}
          super(obj, block, after)

          Fiber.yield
          result
        end
      end

    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
em-synchrony-1.0.6 lib/em-synchrony/iterator.rb
em-synchrony-1.0.5 lib/em-synchrony/iterator.rb
em-synchrony-1.0.4 lib/em-synchrony/iterator.rb
em-synchrony-1.0.3 lib/em-synchrony/iterator.rb
em-synchrony-1.0.2 lib/em-synchrony/iterator.rb
em-synchrony-1.0.1 lib/em-synchrony/iterator.rb
em-synchrony-1.0.0 lib/em-synchrony/iterator.rb
em-synchrony-0.3.0.beta.1 lib/em-synchrony/iterator.rb
em-synchrony-0.2.0 lib/em-synchrony/iterator.rb
em-synchrony-0.1.5 lib/em-synchrony/iterator.rb
em-synchrony-0.1.4 lib/em-synchrony/iterator.rb