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

Returns a new instance of 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
32
33
# File 'lib/core/async/future.rb', line 25

def cancel
  if pending?
    @task.stop
    wait
    true
  else
    false
  end
end

#canceled?Boolean

public

Return true if canceled.

Returns:

  • (Boolean)


110
111
112
# File 'lib/core/async/future.rb', line 110

def canceled?
  status == :canceled
end

#complete?Boolean

public

Return true if complete.

Returns:

  • (Boolean)


116
117
118
# File 'lib/core/async/future.rb', line 116

def complete?
  status == :complete
end

#errorObject

public

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



53
54
55
# File 'lib/core/async/future.rb', line 53

def error
  @error ||= get_error
end

#failed?Boolean

public

Return true if failed.

Returns:

  • (Boolean)


104
105
106
# File 'lib/core/async/future.rb', line 104

def failed?
  status == :failed
end

#pending?Boolean

public

Return true if pending.

Returns:

  • (Boolean)


98
99
100
# File 'lib/core/async/future.rb', line 98

def pending?
  status == :pending
end

#resolved?Boolean

public

Return true if failed or complete.

Returns:

  • (Boolean)


122
123
124
# File 'lib/core/async/future.rb', line 122

def resolved?
  failed? || complete?
end

#resultObject

public

Return the result, blocking until available.



37
38
39
# File 'lib/core/async/future.rb', line 37

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

#statusObject

public

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



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

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