Class: Core::Async::Future

Inherits:
Object
  • Object
show all
Defined in:
lib/core/async/future.rb

Overview

public

Represents a future result.

Instance Method Summary collapse

Constructor Details

#initialize(task) ⇒ Future



8
9
10
11
# File 'lib/core/async/future.rb', line 8

def initialize(task)
  @task = task
  @error = nil
end

Instance Method Details

#cancelObject

public

Attempt to cancel the future, returns true if successful.



25
26
27
28
29
30
31
# File 'lib/core/async/future.rb', line 25

def cancel
  if pending?
    @task.stop
  end

  self
end

#canceled?Boolean

public

Return true if canceled.



108
109
110
# File 'lib/core/async/future.rb', line 108

def canceled?
  status == :canceled
end

#complete?Boolean

public

Return true if complete.



114
115
116
# File 'lib/core/async/future.rb', line 114

def complete?
  status == :complete
end

#errorObject

public

Return any error that occurred without re-raising, blocking until available.



51
52
53
# File 'lib/core/async/future.rb', line 51

def error
  @error ||= get_error
end

#failed?Boolean

public

Return true if failed.



102
103
104
# File 'lib/core/async/future.rb', line 102

def failed?
  status == :failed
end

#pending?Boolean

public

Return true if pending.



96
97
98
# File 'lib/core/async/future.rb', line 96

def pending?
  status == :pending
end

#resolved?Boolean

public

Return true if failed or complete.



120
121
122
# File 'lib/core/async/future.rb', line 120

def resolved?
  failed? || complete?
end

#resultObject

public

Return the result, blocking until available.



35
36
37
# File 'lib/core/async/future.rb', line 35

def result
  @error || @result ||= resolve
end

#statusObject

public

Return the status; one of :pending, :failed, or :complete.



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/core/async/future.rb', line 64

def status
  if defined?(@status)
    @status
  else
    status = find_status
    if terminal_status?(status)
      @status = status
    end

    status
  end
end

#waitObject

public

Wait on the future to resolve, returning self.



15
16
17
18
19
20
21
# File 'lib/core/async/future.rb', line 15

def wait
  unless @error
    resolve
  end

  self
end