Class: IOPromise::Deferred::DeferredPromise

Inherits:
Base
  • Object
show all
Defined in:
lib/iopromise/deferred/promise.rb

Instance Method Summary collapse

Methods inherited from Base

#beginning, #cancel, #cancelled?, #fulfill, #instrument, #notify_completion, #reject, #started_executing?

Constructor Details

#initialize(delay: nil, &block) ⇒ DeferredPromise

Returns a new instance of DeferredPromise.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/iopromise/deferred/promise.rb', line 8

def initialize(delay: nil, &block)
  super()
    
  @block = block
  
  unless delay.nil?
    @defer_until = Process.clock_gettime(Process::CLOCK_MONOTONIC) + delay
  end
    
  ::IOPromise::ExecutorContext.current.register(self) unless @block.nil?
end

Instance Method Details

#execute_poolObject



28
29
30
# File 'lib/iopromise/deferred/promise.rb', line 28

def execute_pool
  DeferredExecutorPool.for(Thread.current)
end

#run_deferredObject



32
33
34
35
36
37
38
39
# File 'lib/iopromise/deferred/promise.rb', line 32

def run_deferred
  return if @block.nil? || !pending?
  begin
    fulfill(@block.call)
  rescue => exception
    reject(exception)
  end
end

#time_until_executionObject



41
42
43
44
45
46
47
48
# File 'lib/iopromise/deferred/promise.rb', line 41

def time_until_execution
  return 0 unless defined?(@defer_until)

  now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  return 0 if now > @defer_until

  @defer_until - now
end

#waitObject



20
21
22
23
24
25
26
# File 'lib/iopromise/deferred/promise.rb', line 20

def wait
  if @block.nil?
    super
  else
    ::IOPromise::ExecutorContext.current.wait_for_all_data(end_when_complete: self)
  end
end