Module: MultiPrompt

Included in:
Cont
Defined in:
lib/cont/multi_prompt.rb

Defined Under Namespace

Classes: DeadContinuationError, UnexpectedStatusError

Class Method Summary collapse

Class Method Details

.control0_at(tag, &block) ⇒ Object



57
58
59
60
61
62
# File 'lib/cont/multi_prompt.rb', line 57

def control0_at(tag, &block)
  status, f = Fiber.yield(:capture, tag, block)
  raise UnexpectedStatusError.new("unexpected status: #{status}") \
    unless status == :resume
  f.call()
end

.prompt0_at(tag, &block) ⇒ Object



50
51
52
53
54
55
# File 'lib/cont/multi_prompt.rb', line 50

def prompt0_at(tag, &block)
  fiber = Fiber.new do
    Fiber.yield(:return, block.call())
  end
  run_at(tag, fiber)
end

.reset_at(tag) {|block| ... } ⇒ Object

Limit the continuation to the current block.

Yields:

  • (block)

    The block of code to be run

Returns:

  • (Object)

    The result of the block.



16
17
18
# File 'lib/cont/multi_prompt.rb', line 16

def reset_at(tag, &block)
  prompt0_at(tag, &block)
end

.run_at(tag, fiber, *args) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cont/multi_prompt.rb', line 37

def run_at(tag, fiber, *args)
  case fiber.resume(*args)
  in :return, value
    value
  in :capture, ^tag, value
    value.call(fiber)
  in :capture, other_tag, value
      run_at(tag, fiber, Fiber.yield(:capture, other_tag, value))
  else
    raise UnexpectedStatusError.new("unexpected status")
  end
end

.shift_at(tag) {|block| ... } ⇒ Object

Capture the current continuation.

Yields:

  • (block)

    The block of code to be run

Returns:

  • (Object)

    The result of the block.



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cont/multi_prompt.rb', line 24

def shift_at(tag, &block)
  control0_at(tag) do |fiber|
    prompt0_at(tag) do
      block.call lambda { |value|
        prompt0_at(tag) do
          raise DeadContinuationError.new unless fiber.alive?
          run_at(nil, fiber, :resume, lambda { value })
        end
      }
    end
  end
end