Module: Deferred::InstanceMethods

Includes:
EM::Deferrable
Included in:
Deferred, Default, ThreadpoolJob
Defined in:
lib/deferred/instance_methods.rb

Instance Method Summary collapse

Instance Method Details

#callback(*a, &b) ⇒ Object



5
6
7
8
# File 'lib/deferred/instance_methods.rb', line 5

def callback(*a, &b)
  super(*a, &b)
  self
end

#chain_err(other_dfr) ⇒ Object

syntactic sugar equivalent to other_dfr.errback { |*a| self.fail(*a) }



51
52
53
# File 'lib/deferred/instance_methods.rb', line 51

def chain_err(other_dfr)
  chain_to(other_dfr, :only_errors => true)
end

#chain_to(other_dfr, opts = {}) ⇒ Object

call this deferred with the result (callback or errback) of the other_dfr

Examples:

deferred chaining


# this:

d.chain_to(other_dfr)

# is the equivalent of:

other_dfr.callback { |*a| d.succeed(*a) }
other_dfr.errback  { |*a| d.fail(*a) }

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :ignore_errors (true, false) — default: false

    if true, then don’t hook up the errback.

  • :only_errors (true, false) — default: false

    if true, then don’t hook up the callback.

Returns:

  • self



44
45
46
47
48
# File 'lib/deferred/instance_methods.rb', line 44

def chain_to(other_dfr, opts={})
  other_dfr.callback { |*a| self.succeed(*a) }  unless opts[:only_errors]
  other_dfr.errback { |*a| self.fail(*a) }      unless opts[:ignore_errors]
  self
end

#ensure_that(&b) ⇒ Object

add block to both callback and errback



16
17
18
19
20
# File 'lib/deferred/instance_methods.rb', line 16

def ensure_that(&b)
  callback(&b)
  errback(&b)
  self
end

#errback(*a, &b) ⇒ Object



10
11
12
13
# File 'lib/deferred/instance_methods.rb', line 10

def errback(*a, &b)
  super(*a, &b)
  self
end

#errback_on_exceptionObject

takes a block, if an exception is raised inside of the block we do self.fail(exc)



75
76
77
78
79
# File 'lib/deferred/instance_methods.rb', line 75

def errback_on_exception
  yield
rescue Exception => e
  self.fail(*e)
end

#timeout(seconds, exception_klass = nil) ⇒ Object

Like EM::Deferrable’s timeout method, but optionally allows you to specify an exception class. An instance of that exception class will be created and used as the argument to #fail. If an exception_klass is not given, then #fail is called without arguments.



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/deferred/instance_methods.rb', line 60

def timeout(seconds, exception_klass=nil)
  cancel_timeout
  me = self
  @deferred_timeout = EventMachine::Timer.new(seconds) do
    if exception_klass
      e = exception_klass.new("timeout after %0.3f seconds" % [seconds.to_f]).tap { |e| e.set_backtrace([]) }
      me.fail(*e)
    else
      me.fail
    end
  end
end