class Concurrent::Delay
@see aphyr.com/posts/306-clojure-from-the-ground-up-state<br>@see clojuredocs.org/clojure_core/clojure.core/delay<br><br>@see Concurrent::Dereferenceable
@since 0.6.0
safety of the reference returned by ‘#value`.
`Delay` includes the `Concurrent::Dereferenceable` mixin to support thread
any side effects created by the operation will only happen once as well.
return the cached value. The operation will only be run once. This means that
blocked on `#value` will return. Subsequent calls to `#value` will immediately
reason will be set to the raised exception, as appropriate. All threads
is complete the value will be set to the result of the operation or the
threads attempting to call `#value` will block as well. Once the operation
enclosed opration will be run and the calling thread will block. Other
reason are both `nil`. The first time the `#value` method is called the
When a `Delay` is created its state is set to `pending`. The value and
delays execution of the operation until the result is actually needed.
performs the operation asynchronously, a `Delay` (as the name implies)
Where a `Future` schedules an operation for immediate execution and
A `Delay` is similar to `Future` but solves a different problem.
operations that may never be needed.
Lazy evaluation of a block yielding an immutable result. Useful for expensive
def execute_task_once
def execute_task_once if @state == :pending begin @value = @task.call @state = :fulfilled rescue => ex @reason = ex @state = :rejected end end end
def initialize(opts = {}, &block)
-
(ArgumentError)- if no block is given
Options Hash:
(**opts)-
:copy_on_deref(String) -- call the given `Proc` passing the internal value and -
:freeze_on_deref(String) -- call `#freeze` before returning the data -
:dup_on_deref(String) -- call `#dup` before returning the data
Parameters:
-
opts(Hash) -- the options to create a message with
Other tags:
- Yield: - the delayed operation to perform
def initialize(opts = {}, &block) raise ArgumentError.new('no block given') unless block_given? init_obligation @state = :pending @task = block set_deref_options(opts) end
def value
- See: Concurrent::Dereferenceable -
Returns:
-
(Object)- the (possibly memoized) result of the block operation
def value mutex.lock execute_task_once result = apply_deref_options(@value) mutex.unlock result end