Method: Concurrent::Delay#value

Defined in:
lib/concurrent-ruby/concurrent/delay.rb

#value(timeout = nil) ⇒ Object

Note:

The default behavior of Delay is to block indefinitely when calling either value or wait, executing the delayed operation on the current thread. This makes the timeout value completely irrelevant. To enable non-blocking behavior, use the executor constructor option. This will cause the delayed operation to be execute on the given executor, allowing the call to timeout.

Return the value this object represents after applying the options specified by the #set_deref_options method. If the delayed operation raised an exception this method will return nil. The exception object can be accessed via the #reason method.

Parameters:

  • timeout (Numeric) (defaults to: nil)

    the maximum number of seconds to wait

Returns:

  • (Object)

    the current value of the object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/concurrent-ruby/concurrent/delay.rb', line 77

def value(timeout = nil)
  if @executor # TODO (pitr 12-Sep-2015): broken unsafe read?
    super
  else
    # this function has been optimized for performance and
    # should not be modified without running new benchmarks
    synchronize do
      execute = @evaluation_started = true unless @evaluation_started
      if execute
        begin
          set_state(true, @task.call, nil)
        rescue => ex
          set_state(false, nil, ex)
        end
      elsif incomplete?
        raise IllegalOperationError, 'Recursive call to #value during evaluation of the Delay'
      end
    end
    if @do_nothing_on_deref
      @value
    else
      apply_deref_options(@value)
    end
  end
end