Sha256: 503a48df1ea899bee5e84395771ec4572509d20c362c66051043dd12ec8dee04

Contents?: true

Size: 1.29 KB

Versions: 20

Compression:

Stored size: 1.29 KB

Contents

require 'fiber'

class ::Libuv::Loop
    @@use_fibers = true
end

class Object
    private


    # Takes a Promise response and turns it into a co-routine
    # for code execution without using callbacks
    #
    # @param *promises [::Libuv::Q::Promise] a number of promises that will be combined into a single promise
    # @return [Object] Returns the result of a single promise or an array of results if provided multiple promises
    # @raise [Exception] if the promise is rejected
    def co(*yieldable, &block)
        f = Fiber.current
        wasError = false

        # Convert the input into a promise
        if yieldable.length == 1
            promise = yieldable[0]
        else
            promise = ::Libuv::Loop.current.all(*yieldable)
        end

        # Use the promise to resume the Fiber
        promise.then(proc { |res|
            f.resume res
        }, proc { |err|
            wasError = true
            f.resume err
        })

        # Passed independently as this is often overwritten for performance
        promise.progress(block) if block_given?

        # Assign the result from the resume
        result = Fiber.yield

        # Either return the result or raise an error
        raise result if wasError
        result
    end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
libuv-2.0.12 lib/libuv/coroutines.rb
libuv-2.0.11 lib/libuv/coroutines.rb
libuv-2.0.10 lib/libuv/coroutines.rb
libuv-2.0.9 lib/libuv/coroutines.rb
libuv-2.0.8 lib/libuv/coroutines.rb
libuv-2.0.6 lib/libuv/coroutines.rb
libuv-2.0.5 lib/libuv/coroutines.rb
libuv-2.0.4 lib/libuv/coroutines.rb
libuv-2.0.3 lib/libuv/coroutines.rb
libuv-2.0.2 lib/libuv/coroutines.rb
libuv-2.0.1 lib/libuv/coroutines.rb
libuv-2.0.0 lib/libuv/coroutines.rb
libuv-1.3.0 lib/libuv/coroutines.rb
libuv-1.2.0 lib/libuv/coroutines.rb
libuv-1.1.3 lib/libuv/coroutines.rb
libuv-1.1.2 lib/libuv/coroutines.rb
libuv-1.1.1 lib/libuv/coroutines.rb
libuv-1.1.0 lib/libuv/coroutines.rb
libuv-1.0.3 lib/libuv/coroutines.rb
libuv-1.0.2 lib/libuv/coroutines.rb