Class: Pluggaloid::Stream

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/pluggaloid/stream.rb

Defined Under Namespace

Classes: Merge

Instance Method Summary collapse

Constructor Details

#initialize(enumerator) ⇒ Stream

Returns a new instance of Stream.



7
8
9
# File 'lib/pluggaloid/stream.rb', line 7

def initialize(enumerator)
  @enumerator = enumerator
end

Instance Method Details

#buffer(sec) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/pluggaloid/stream.rb', line 35

def buffer(sec)
  throttling_promise = nil
  buffer = []
  Stream.new(
    Enumerator.new do |yielder|
      @enumerator.each do |item|
        buffer << item
        throttling_promise ||= Delayer.new(delay: sec) do
          yielder << buffer.freeze
          buffer = []
          throttling_promise = nil
        end
      end
    end.lazy
  )
end

#debounce(sec) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/pluggaloid/stream.rb', line 21

def debounce(sec)
  throttling_promise = nil
  Stream.new(
    Enumerator.new do |yielder|
      @enumerator.each do |item|
        throttling_promise&.cancel
        throttling_promise = Delayer.new(delay: sec) do
          yielder << item
        end
      end
    end.lazy
  )
end

#merge(*streams) ⇒ Object



52
53
54
# File 'lib/pluggaloid/stream.rb', line 52

def merge(*streams)
  Stream.new(Merge.new(self, *streams).lazy)
end

#throttle(sec) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/pluggaloid/stream.rb', line 11

def throttle(sec)
  throttling = 0
  @enumerator.select do |item|
    r0 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
    if throttling <= r0
      throttling = r0 + sec
    end
  end
end