Module: Reacto::Behaviours

Defined in:
lib/reacto/behaviours.rb

Class Method Summary collapse

Class Method Details

.array_repeat_enumerator(array) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/reacto/behaviours.rb', line 53

def array_repeat_enumerator(array)
  size = array.size

  Enumerator.new do |yielder|
    n = 0
    loop do
      yielder.yield array[n % size]
      n = n + 1
    end
  end
end

.constant(const) ⇒ Object



65
66
67
# File 'lib/reacto/behaviours.rb', line 65

def constant(const)
  -> (_val) { const }
end

.enumerable(enumerable_value) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/reacto/behaviours.rb', line 28

def enumerable(enumerable_value)
  ->(tracker) do
    begin
      enumerable_value.each do |val|
        break unless tracker.subscribed?
        tracker.on_value(val)
      end

      tracker.on_close if tracker.subscribed?
    rescue => error
      tracker.on_error(error) if tracker.subscribed?
    end
  end
end

.integers_enumeratorObject



43
44
45
46
47
48
49
50
51
# File 'lib/reacto/behaviours.rb', line 43

def integers_enumerator
  Enumerator.new do |yielder|
    n = 0
    loop do
      yielder << n
      n = n + 1
    end
  end
end

.same_predicate(val) ⇒ Object



69
70
71
# File 'lib/reacto/behaviours.rb', line 69

def same_predicate(val)
  -> (value) { val == value }
end

.single_tracker_value(tracker, value) ⇒ Object



14
15
16
17
18
# File 'lib/reacto/behaviours.rb', line 14

def single_tracker_value(tracker, value)
  with_close_and_error(tracker) do |subscriber|
    subscriber.on_value(value) if subscriber.subscribed?
  end
end

.single_value(value) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/reacto/behaviours.rb', line 20

def single_value(value)
  lambda do |tracker|
    with_close_and_error(tracker) do |subscriber|
      subscriber.on_value(value) if subscriber.subscribed?
    end
  end
end

.with_close_and_error(tracker, &block) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/reacto/behaviours.rb', line 5

def with_close_and_error(tracker, &block)
  begin
    yield tracker if block_given?
    tracker.on_close if tracker.subscribed?
  rescue StandardError => error
    tracker.on_error(error) if tracker.subscribed?
  end
end