Class: Bolt::PlanFuture

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt/plan_future.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fiber, id, name = nil) ⇒ PlanFuture

Returns a new instance of PlanFuture.



10
11
12
13
14
15
# File 'lib/bolt/plan_future.rb', line 10

def initialize(fiber, id, name = nil)
  @fiber = fiber
  @id    = id
  @name  = name
  @value = nil
end

Instance Attribute Details

#fiberObject (readonly)

Returns the value of attribute fiber.



7
8
9
# File 'lib/bolt/plan_future.rb', line 7

def fiber
  @fiber
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/bolt/plan_future.rb', line 7

def id
  @id
end

#valueObject

Returns the value of attribute value.



8
9
10
# File 'lib/bolt/plan_future.rb', line 8

def value
  @value
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/bolt/plan_future.rb', line 25

def alive?
  fiber.alive?
end

#nameObject



17
18
19
# File 'lib/bolt/plan_future.rb', line 17

def name
  @name || @id
end

#raise(exception) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/bolt/plan_future.rb', line 29

def raise(exception)
  # Make sure the value gets set
  @value = exception
  # This was introduced in Ruby 2.7
  begin
    # Raise an exception to kill the Fiber. If the Fiber has not been
    # resumed yet, or is already terminated this will raise a FiberError.
    # We don't especially care about the FiberError, as long as the Fiber
    # doesn't report itself as alive.
    fiber.raise(exception)
  rescue FiberError
    # If the Fiber is still alive, resume it with a block to raise the
    # exception which will terminate it.
    if fiber.alive?
      fiber.resume { raise(exception) }
    end
  end
end

#resumeObject



48
49
50
51
52
53
54
# File 'lib/bolt/plan_future.rb', line 48

def resume
  if fiber.alive?
    @value = fiber.resume
  else
    @value
  end
end

#stateObject



56
57
58
59
60
61
62
63
64
# File 'lib/bolt/plan_future.rb', line 56

def state
  if fiber.alive?
    "running"
  elsif value.is_a?(Exception)
    "error"
  else
    "done"
  end
end

#to_sObject



21
22
23
# File 'lib/bolt/plan_future.rb', line 21

def to_s
  "Future '#{name}'"
end