Class: Hermann::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/hermann/result.rb

Constant Summary collapse

STATES =
[:pending,
:rejected,
:fulfilled,
:unfulfilled,
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(producer) ⇒ Result

Returns a new instance of Result.



12
13
14
15
16
17
# File 'lib/hermann/result.rb', line 12

def initialize(producer)
  @producer = producer
  @reason = nil
  @value = nil
  @state = :unfulfilled
end

Instance Attribute Details

#reasonObject (readonly)

Returns the value of attribute reason.



4
5
6
# File 'lib/hermann/result.rb', line 4

def reason
  @reason
end

#stateObject (readonly)

Returns the value of attribute state.



4
5
6
# File 'lib/hermann/result.rb', line 4

def state
  @state
end

Instance Method Details

#completed?Boolean

Returns True if this child can be reaped.

Returns:

  • (Boolean)

    True if this child can be reaped



26
27
28
29
# File 'lib/hermann/result.rb', line 26

def completed?
  return true if rejected? || fulfilled?
  return false
end

#internal_set_error(exception) ⇒ Object

INTERNAL METHOD ONLY. Do not use

This method will set our internal #reason with the details from the exception

Parameters:

  • exception (Exception)


67
68
69
70
71
72
# File 'lib/hermann/result.rb', line 67

def internal_set_error(exception)
  return if exception.nil?

  @reason = exception
  @state = :rejected
end

#internal_set_value(value, is_error) ⇒ Object

INTERNAL METHOD ONLY. Do not use

This method will be invoked by the underlying extension to indicate set the actual value after a callback has completed

Parameters:

  • value (Object)

    The actual resulting value

  • is_error (Boolean)

    True if the result was errored for whatever reason



50
51
52
53
54
55
56
57
58
59
# File 'lib/hermann/result.rb', line 50

def internal_set_value(value, is_error)
  @value = value

  if is_error
    puts "Hermann::Result#set_internal_value(#{value.class}:\"#{value}\", error?:#{is_error})"
    @state = :rejected
  else
    @state = :fulfilled
  end
end

#value(timeout = 0) ⇒ NilClass, Object

Access the value of the future

Parameters:

  • timeout (FixNum) (defaults to: 0)

    Seconds to wait on the underlying machinery for a result

Returns:

  • (NilClass)

    nil if no value could be received in the time alotted

  • (Object)


37
38
39
40
# File 'lib/hermann/result.rb', line 37

def value(timeout=0)
  @producer.tick_reactor(timeout)
  return @value
end