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(val) ⇒ Object
-
#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
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/volt/reactive/reactive_array.rb', line 102 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 :<<
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/volt/reactive/reactive_array.rb', line 87 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 :[]=
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/volt/reactive/reactive_array.rb', line 25 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(val) ⇒ Object
78 79 80 |
# File 'lib/volt/reactive/reactive_array.rb', line 78 def delete(val) self.delete_at(@array.index(val)) end |
#delete_at(index) ⇒ Object
alias :__old_delete_at :delete_at
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/volt/reactive/reactive_array.rb', line 51 def delete_at(index) index_val = index.cur __clear_element(index) model = @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! @persistor.removed(model) if @persistor return model end |
#insert(*args) ⇒ Object
alias :__old_insert :insert
122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/volt/reactive/reactive_array.rb', line 122 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
214 215 216 |
# File 'lib/volt/reactive/reactive_array.rb', line 214 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
163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/volt/reactive/reactive_array.rb', line 163 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)
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/volt/reactive/reactive_array.rb', line 182 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) # puts "SCOPE CHECK: TFI: #{method_name.inspect} - #{args.inspect} on #{self.inspect}" 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
136 137 138 139 140 141 142 143 |
# File 'lib/volt/reactive/reactive_array.rb', line 136 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
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/volt/reactive/reactive_array.rb', line 145 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 |