Skip to content

Commit 45b9457

Browse files
committed
reactive count
1 parent 7dfe00a commit 45b9457

File tree

5 files changed

+41
-18
lines changed

5 files changed

+41
-18
lines changed

lib/volt/page/bindings/content_binding.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ def initialize(page, target, context, binding_name, getter)
1717

1818
def update
1919
value = @value.cur.or('')
20+
if value.reactive?
21+
puts "GOT CUR: #{value.inspect}"
22+
end
2023

2124
# Exception values display the exception as a string
2225
value = value.to_s

lib/volt/page/bindings/template_binding.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def call_ready
174174
# Set the current section on the controller if it wants so it can manipulate
175175
# the dom if needed
176176
if @controller.respond_to?(:section=)
177-
@controller.dom_section = @current_template.dom_ection
177+
@controller.section = @current_template.dom_section
178178
end
179179

180180
if @controller.respond_to?(:dom_ready)

lib/volt/reactive/reactive_count.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@ def cur
1717
# After events are bound, we keep a cache of each cell's count
1818
# value, and base the results
1919
def cached_count
20+
@cached_results = []
21+
2022

2123
end
2224

2325
# Before events are bound, when .cur is called, we simply
2426
# run the count on the source object.
2527
def direct_count
2628
count = 0
27-
@source.size.cur.times do |index|
29+
@source.cur.size.times do |index|
2830
val = @source[index]
2931
result = @block.call(val).cur
3032
if result == true
@@ -37,23 +39,25 @@ def direct_count
3739

3840
def setup_listeners
3941
@cell_trackers = []
42+
@setup = false
4043
@added_tracker = @source.on('added') do |_, index|
4144
change_cell_count(@source.size.cur)
42-
trigger!('changed')
4345
end
4446

4547
@removed_tracker = @source.on('removed') do |_, index|
4648
change_cell_count(@source.size.cur)
47-
trigger!('changed')
4849
end
4950

51+
@setup = true
52+
5053
# Initial cell tracking
5154
change_cell_count(@source.size.cur)
5255
end
5356

5457
# We need to make sure we're listening on the result from each cell,
5558
# that way we can trigger when the value changes.
5659
def change_cell_count(size)
60+
# puts "CHANGE SIZE: #{size}"
5761
current_size = @cell_trackers.size
5862

5963
if current_size < size
@@ -66,6 +70,7 @@ def change_cell_count(size)
6670
result = @block.call(val)
6771

6872
@cell_trackers << result.on('changed') do
73+
# puts "RESULT CHANGED: #{index}"
6974
trigger!('changed')
7075
end
7176
end

lib/volt/reactive/reactive_value.rb

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -239,21 +239,23 @@ def event_removed(event, last, last_for_event)
239239

240240
# Fetch the current value
241241
def cur(shallow=false, ignore_cache=false)
242-
# Return from cache if it is cached
242+
# Use cache if it is cached
243243
if @cur_cache && !shallow && !ignore_cache
244-
return @cur_cache
245-
end
246-
247-
if @getter.class == ::Proc
248-
# Get the current value, capture any errors
249-
begin
250-
result = @getter.call
251-
rescue => e
252-
result = e
253-
end
244+
# We might be caching another reactive value, so we just set
245+
# it as the result and let it get unwrapped.
246+
result = @cur_cache
254247
else
255-
# getter is just an object, return it
256-
result = @getter
248+
if @getter.class == ::Proc
249+
# Get the current value, capture any errors
250+
begin
251+
result = @getter.call
252+
rescue => e
253+
result = e
254+
end
255+
else
256+
# getter is just an object, return it
257+
result = @getter
258+
end
257259
end
258260

259261
if !shallow && result.reactive?
@@ -268,7 +270,7 @@ def cur(shallow=false, ignore_cache=false)
268270
def update_followers
269271
return if @setting_up
270272
if has_listeners?
271-
current_obj = cur(false, true)
273+
current_obj = cur(true, true)
272274
should_attach = current_obj.respond_to?(:on)
273275

274276
if should_attach

spec/models/reactive_count_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require 'volt/models'
2+
3+
describe ReactiveCount do
4+
it "should call cur through the reactive count to the number" do
5+
model = ReactiveValue.new(Model.new)
6+
7+
model._items << {_name: 'ok'}
8+
9+
count = model._items.count {|m| m._name == 'ok' }
10+
11+
expect(count.cur).to eq(1)
12+
end
13+
end

0 commit comments

Comments
 (0)