Class: ReactiveArray
- Includes:
- ObjectTracking, ReactiveTags
- Defined in:
- lib/volt/reactive/reactive_array.rb
Overview
< Array
Direct Known Subclasses
Instance Method Summary collapse
- #+(array) ⇒ Object
-
#<<(value) ⇒ Object
alias :__old_append :<<.
- #==(*args) ⇒ Object
-
#[]=(index, value) ⇒ Object
alias :__old_assign :[]=.
-
#delete_at(index) ⇒ Object
alias :__old_delete_at :delete_at.
-
#initialize(array = []) ⇒ ReactiveArray
constructor
A new instance of ReactiveArray.
-
#insert(*args) ⇒ Object
alias :__old_insert :insert.
- #inspect ⇒ Object
-
#method_missing(method_name, *args, &block) ⇒ Object
Forward any missing methods to the array.
-
#split_scope(scope) ⇒ Object
TODO: This is an opal work around.
-
#trigger_for_index!(event_name, index, *passed_args) ⇒ Object
Trigger the changed event to any values fetched either through the lookup ([]), #last, or any fetched through the array its self.
- #trigger_on_direct_listeners!(event, *args) ⇒ Object
- #trigger_size_change! ⇒ Object
Methods included from ObjectTracking
Methods included from ReactiveTags
included, #reactive_method_tag
Constructor Details
#initialize(array = []) ⇒ ReactiveArray
Returns a new instance of ReactiveArray.
7 8 9 |
# File 'lib/volt/reactive/reactive_array.rb', line 7 def initialize(array=[]) @array = array end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
Forward any missing methods to the array
12 13 14 |
# File 'lib/volt/reactive/reactive_array.rb', line 12 def method_missing(method_name, *args, &block) @array.send(method_name, *args, &block) end |
Instance Method Details
#+(array) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/volt/reactive/reactive_array.rb', line 91 def +(array) old_size = self.size # TODO: += is funky here, might need to make a .plus! method result = ReactiveArray.new(@array.dup + array) old_size.upto(result.size-1) do |index| trigger_for_index!('changed', index) trigger_on_direct_listeners!('added', old_size + index) end trigger_size_change! return result end |
#<<(value) ⇒ Object
alias :__old_append :<<
76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/volt/reactive/reactive_array.rb', line 76 def <<(value) result = (@array << value) # Track new value __track_element(self.size-1, value) trigger_for_index!('changed', self.size-1) trigger_on_direct_listeners!('added', self.size-1) trigger_size_change! return result end |
#==(*args) ⇒ Object
16 17 18 |
# File 'lib/volt/reactive/reactive_array.rb', line 16 def ==(*args) @array.==(*args) end |
#[]=(index, value) ⇒ Object
alias :__old_assign :[]=
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/volt/reactive/reactive_array.rb', line 26 def []=(index, value) index_val = index.cur # Clean old value __clear_element(index) @array[index.cur] = value # Track new value __track_element(index, value) # Also track the index if its reactive if index.reactive? # TODO: Need to clean this up when the index changes event_chain.add_object(index.reactive_manager) do |event, *args| trigger_for_index!(event, index.cur) end end # Trigger changed trigger_for_index!('changed', index_val) end |
#delete_at(index) ⇒ Object
alias :__old_delete_at :delete_at
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/volt/reactive/reactive_array.rb', line 52 def delete_at(index) index_val = index.cur __clear_element(index) @array.delete_at(index_val) trigger_on_direct_listeners!('removed', index_val) # Trigger a changed event for each element in the zone where the # lookup would change index.upto(self.size+1) do |position| trigger_for_index!('changed', position) end trigger_size_change! end |
#insert(*args) ⇒ Object
alias :__old_insert :insert
111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/volt/reactive/reactive_array.rb', line 111 def insert(*args) old_size = self.size result = @array.insert(*args) old_size.upto(result.size-1) do |index| trigger_for_index!('changed', index) trigger_on_direct_listeners!('added', old_size+index) end trigger_size_change! return result end |
#inspect ⇒ Object
202 203 204 |
# File 'lib/volt/reactive/reactive_array.rb', line 202 def inspect "#<#{self.class.to_s} #{@array.inspect}>" end |
#split_scope(scope) ⇒ Object
TODO: This is an opal work around. Currently there is a bug with destructuring method_name, *args, block = scope
152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/volt/reactive/reactive_array.rb', line 152 def split_scope(scope) if scope scope = scope.dup method_name = scope.shift block = scope.pop return method_name, scope, block else return nil,[],nil end end |
#trigger_for_index!(event_name, index, *passed_args) ⇒ Object
Trigger the changed event to any values fetched either through the lookup ([]), #last, or any fetched through the array its self. (sum, max, etc…)
On an array, when an element is added or removed, we need to trigger change events on each method that does the following:
-
uses the whole array (max, sum, etc…)
-
accesses this specific element - array
-
accesses an element via a method (first, last)
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/volt/reactive/reactive_array.rb', line 171 def trigger_for_index!(event_name, index, *passed_args) self.trigger_by_scope!(event_name, *passed_args) do |scope| # method_name, *args, block = scope method_name, args, block = split_scope(scope) result = case method_name when nil # no method name means the event was bound directly, we don't # want to trigger changed on the array its self. false when :[] # Extract the current index if its reactive arg_index = args[0].cur # TODO: we could handle negative indicies better arg_index == index.cur || arg_index < 0 when :last index.cur == self.size-1 when :first index.cur == 0 when :size, :length # Size does not depend on the contents of the cells false else true end result end end |
#trigger_on_direct_listeners!(event, *args) ⇒ Object
125 126 127 128 129 130 131 132 |
# File 'lib/volt/reactive/reactive_array.rb', line 125 def trigger_on_direct_listeners!(event, *args) trigger_by_scope!(event, *args) do |scope| # Only if it is bound directly to us. Don't pass # down the chain !scope || scope[0] == nil end end |
#trigger_size_change! ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/volt/reactive/reactive_array.rb', line 134 def trigger_size_change! trigger_by_scope!('changed') do |scope| # method_name, *args, block = scope method_name, args, block = split_scope(scope) result = case method_name && method_name.to_sym when :size, :length true else false end result end end |