Class: Reacto::Operations::Drop

Inherits:
Object
  • Object
show all
Defined in:
lib/reacto/operations/drop.rb

Instance Method Summary collapse

Constructor Details

#initialize(how_many_to_drop, offset = NO_VALUE) ⇒ Drop

Returns a new instance of Drop.



7
8
9
10
11
12
13
14
# File 'lib/reacto/operations/drop.rb', line 7

def initialize(how_many_to_drop, offset = NO_VALUE)
  if how_many_to_drop < 0
    raise ArgumentError.new('Attempt to drop negative size!')
  end

  @how_many_to_drop = how_many_to_drop
  @offset = offset
end

Instance Method Details

#call(tracker) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/reacto/operations/drop.rb', line 16

def call(tracker)
  dropped = 0

  behaviour = -> (value) do
    dropped += 1

    if dropped > @how_many_to_drop
      if @offset != NO_VALUE
        if @offset <= 0
          tracker.on_close
          return
        else
          @offset -= 1
        end
      end
      tracker.on_value(value)
    end
  end

  Subscriptions::OperationSubscription.new(tracker, value: behaviour)
end