Class: ReactiveCount
Instance Method Summary
collapse
-
#cached_count ⇒ Object
After events are bound, we keep a cache of each cell’s count value, and base the results.
-
#change_cell_count(size) ⇒ Object
We need to make sure we’re listening on the result from each cell, that way we can trigger when the value changes.
-
#cur ⇒ Object
-
#direct_count ⇒ Object
Before events are bound, when .cur is called, we simply run the count on the source object.
-
#event_added(event, scope_provider, first, first_for_event) ⇒ Object
-
#event_removed(event, last, last_for_event) ⇒ Object
-
#initialize(source, block) ⇒ ReactiveCount
constructor
A new instance of ReactiveCount.
-
#inspect ⇒ Object
-
#reactive? ⇒ Boolean
-
#setup_listeners ⇒ Object
-
#teardown_listeners ⇒ Object
included, #reactive_method_tag
Constructor Details
#initialize(source, block) ⇒ ReactiveCount
Returns a new instance of ReactiveCount.
8
9
10
11
|
# File 'lib/volt/reactive/reactive_count.rb', line 8
def initialize(source, block)
@source = ReactiveValue.new(source)
@block = block
end
|
Instance Method Details
#cached_count ⇒ Object
After events are bound, we keep a cache of each cell’s count value, and base the results
19
20
21
|
# File 'lib/volt/reactive/reactive_count.rb', line 19
def cached_count
end
|
#change_cell_count(size) ⇒ Object
We need to make sure we’re listening on the result from each cell, that way we can trigger when the value changes.
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/volt/reactive/reactive_count.rb', line 57
def change_cell_count(size)
current_size = @cell_trackers.size
if current_size < size
current_size.upto(size-1) do |index|
val = @source[index]
result = @block.call(val)
@cell_trackers << result.on('changed') do
trigger!('changed')
end
end
elsif current_size > size
(current_size-1).downto(size) do |index|
@cell_trackers[index].remove
@cell_trackers.delete_at(index)
end
end
end
|
13
14
15
|
# File 'lib/volt/reactive/reactive_count.rb', line 13
def cur
direct_count
end
|
#direct_count ⇒ Object
Before events are bound, when .cur is called, we simply run the count on the source object.
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/volt/reactive/reactive_count.rb', line 25
def direct_count
count = 0
@source.size.cur.times do |index|
val = @source[index]
result = @block.call(val).cur
if result == true
count += 1
end
end
count
end
|
#event_added(event, scope_provider, first, first_for_event) ⇒ Object
97
98
99
|
# File 'lib/volt/reactive/reactive_count.rb', line 97
def event_added(event, scope_provider, first, first_for_event)
setup_listeners if first
end
|
#event_removed(event, last, last_for_event) ⇒ Object
101
102
103
|
# File 'lib/volt/reactive/reactive_count.rb', line 101
def event_removed(event, last, last_for_event)
teardown_listeners if last
end
|
105
106
107
|
# File 'lib/volt/reactive/reactive_count.rb', line 105
def inspect
"@#{cur}"
end
|
4
5
6
|
# File 'lib/volt/reactive/reactive_count.rb', line 4
def reactive?
true
end
|
#setup_listeners ⇒ Object
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/volt/reactive/reactive_count.rb', line 38
def setup_listeners
puts "SETUP LISTENERS"
@cell_trackers = []
@added_tracker = @source.on('added') do |_, index|
change_cell_count(@source.size.cur)
trigger!('changed')
end
@removed_tracker = @source.on('removed') do |_, index|
change_cell_count(@source.size.cur)
trigger!('changed')
end
change_cell_count(@source.size.cur)
end
|
#teardown_listeners ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/volt/reactive/reactive_count.rb', line 84
def teardown_listeners
@added_tracker.remove
@added_tracker = nil
@removed_tracker.remove
@removed_tracker = nil
change_cell_count(0)
@cell_trackers = nil
puts "TEARDOWN"
end
|