Class: Chan::Yielder

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

Overview

Channel handler inside block.

Instance Method Summary collapse

Constructor Details

#initialize(parent_to_child, child_to_parent) ⇒ Yielder

Returns a new instance of Yielder.



15
16
17
18
# File 'lib/chan.rb', line 15

def initialize(parent_to_child,child_to_parent)
  @parent_to_child=parent_to_child
  @child_to_parent=child_to_parent
end

Instance Method Details

#closeObject

Stops writing to parent queue.



26
27
28
29
30
31
32
# File 'lib/chan.rb', line 26

def close
  @parent_to_child.instance_eval{
    def push(v)
      raise RuntimeError.new('Pushing to closed Chan')
    end
  }
end

#peekObject

Receives an object from parent without modifying the queue.



34
35
36
37
# File 'lib/chan.rb', line 34

def peek
  sync
  @parent_to_child.first
end

#receiveObject Also known as: next, succ

Receives an object from parent. aliased to next/succ.



40
41
42
43
# File 'lib/chan.rb', line 40

def receive
  sync
  @parent_to_child.shift
end

#send(v) ⇒ Object Also known as: <<

Sends an object to parent. aliased to <<.



48
49
50
51
# File 'lib/chan.rb', line 48

def send(v)
  @child_to_parent.push(v)
  Fiber.yield(false)
end

#syncObject

Loops indefinitely while parent channel is empty.



20
21
22
23
24
# File 'lib/chan.rb', line 20

def sync
  while @parent_to_child.empty?
    Fiber.yield(true)
  end
end