Sha256: 5c99bfd581d39124c5d7673b0a4296f09b4eb803cebaf41dfb979f0566c8f13f

Contents?: true

Size: 1.6 KB

Versions: 6

Compression:

Stored size: 1.6 KB

Contents

##
# Meta-programming String for Syntactic Sugars
# Referenced from {Qiita}[http://qiita.com/south37/items/99a60345b22ef395d424]
class Promise
  # Init a Promise
  # @param [Proc] callback an async method
  def initialize(&callback)
    @callback = callback
  end

  # Define what to do after a method callbacks
  # @param [Proc] resolve what on callback
  # @return [nil] nil
  def then(&resolve)
    @callback.call(resolve)
  end
end

module Kernel
  # Make fiber as async chain
  # @param [Fiber] fiber root of async chain
  def async_fiber(fiber)
    chain = proc do |result|
      next unless result.is_a? Promise
      result.then do |val|
        chain.call(fiber.resume(val))
      end
    end
    chain.call(fiber.resume)
  end

  # Define an async method
  # @param [Symbol] method method name
  # @yield async method
  # @example
  #   async :hello do 
  #     puts 'Hello'
  #   end
  def async(method)
    define_singleton_method method do |*args|
      async_fiber(Fiber.new {yield(*args)})
    end
  end

  # Block the I/O to wait for async method response
  # @param [Promise] promise promise method
  # @example
  #   result = await SQL.query('SELECT * FROM hello')
  def await(promise)
    result = Fiber.yield promise
    if result.is_a? PromiseException
      raise result.payload
    end
    result
  end
end


##
# Exceptions for Promises
# @!attribute [r] payload
#   @return [Exception] raw execption
class PromiseException < Exception
  attr_reader :payload
  # Init PromiseException with existed Exception
  # @param [Exception] payload raw execption
  def initialize(payload)
    @payload = payload
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
murasaki-0.1.0 lib/murasaki/promise.rb
em-midori-0.3.0 lib/midori/core_ext/promise.rb
em-midori-0.2.4 lib/midori/core_ext/promise.rb
em-midori-0.2.3 lib/midori/core_ext/promise.rb
em-midori-0.2.2 lib/midori/core_ext/promise.rb
em-midori-0.2.1 lib/midori/core_ext/promise.rb