Module: Rbgo::Channel::Chan

Includes:
Enumerable
Included in:
BufferChan, NonBufferChan
Defined in:
lib/rbgo/select_chan.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.after(seconds) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/rbgo/select_chan.rb', line 32

def self.after(seconds)
  ch = new
  CoRun::Routine.new(new_thread: true, queue_tag: :none) do
    sleep seconds
    ch << Time.now rescue nil
    ch.close
  end
  ch
end

.new(max = 0) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/rbgo/select_chan.rb', line 23

def self.new(max = 0)
  max = max.to_i
  if max <= 0
    NonBufferChan.new
  else
    BufferChan.new(max)
  end
end

.perform(timeout: nil, &blk) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rbgo/select_chan.rb', line 56

def self.perform(timeout: nil, &blk)
  ch = new
  CoRun::Routine.new(new_thread: false, queue_tag: :default) do
    begin
      Timeout::timeout(timeout) do
        blk.call
      end
    rescue Timeout::Error
    ensure
      ch.close
    end
  end
  ch
end

.tick(every_seconds) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rbgo/select_chan.rb', line 42

def self.tick(every_seconds)
  ch = new
  CoRun::Routine.new(new_thread: true, queue_tag: :none) do
    loop do
      sleep every_seconds
      begin
        ch.enq(Time.now, true)
      rescue ThreadError
      end
    end
  end
  ch
end

Instance Method Details

#eachObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rbgo/select_chan.rb', line 8

def each
  if block_given?
    loop do
      obj, ok = pop
      if ok
        yield obj
      else
        break
      end
    end
  else
    enum_for(:each)
  end
end