module Sinatra::Promises

Constants

VERSION

Public Class Methods

included(klass) click to toggle source
# File lib/sinatra/promises/promises.rb, line 5
def self.included(klass)
  klass.send(:alias_method, :old_invoke, :invoke)
  klass.send(:alias_method, :invoke, :async_handler)
end

Public Instance Methods

async_handler() { || ... } click to toggle source
# File lib/sinatra/promises/promises.rb, line 10
def async_handler
  res = catch(:halt) {yield}
  if res.is_a?(EventMachine::Q::Promise)
    res.then do |result|
      body result
      request.env['async.callback'].call(@response.finish)
    end
    throw :async
  else
    old_invoke do
      res
    end
  end
end