Class: Reacto::Operations::Take
- Inherits:
-
Object
- Object
- Reacto::Operations::Take
- Defined in:
- lib/reacto/operations/take.rb
Instance Method Summary collapse
- #call(tracker) ⇒ Object
-
#initialize(how_many_to_take) ⇒ Take
constructor
A new instance of Take.
Constructor Details
#initialize(how_many_to_take) ⇒ Take
Returns a new instance of Take.
6 7 8 9 10 11 12 13 14 |
# File 'lib/reacto/operations/take.rb', line 6 def initialize(how_many_to_take) if how_many_to_take < 0 raise ArgumentError.new('Attempt to take negative size!') end @how_many_to_take = how_many_to_take @taken = 0 @closed = false end |
Instance Method Details
#call(tracker) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/reacto/operations/take.rb', line 16 def call(tracker) behaviour = lambda do |value| return if @closed if @taken < @how_many_to_take tracker.on_value(value) @taken += 1 else @closed = true tracker.on_close end end Subscriptions::OperationSubscription.new( tracker, value: behaviour ) end |