Class: ReactiveArray

Inherits:
Object show all
Includes:
ObjectTracking, ReactiveTags
Defined in:
lib/volt/reactive/reactive_array.rb

Overview

< Array

Direct Known Subclasses

ArrayModel

Instance Method Summary collapse

Methods included from ObjectTracking

#__setup_tracking

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



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/volt/reactive/reactive_array.rb', line 118

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 :<<



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/volt/reactive/reactive_array.rb', line 103

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 :[]=



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/volt/reactive/reactive_array.rb', line 33

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

#clearObject



94
95
96
97
# File 'lib/volt/reactive/reactive_array.rb', line 94

def clear
  @array = []
  trigger!('changed')
end

#delete(val) ⇒ Object



86
87
88
# File 'lib/volt/reactive/reactive_array.rb', line 86

def delete(val)
  self.delete_at(@array.index(val))
end

#delete_at(index) ⇒ Object

alias :__old_delete_at :delete_at



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/volt/reactive/reactive_array.rb', line 59

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

#each(&block) ⇒ Object

At the moment, each just passes through.



24
25
26
# File 'lib/volt/reactive/reactive_array.rb', line 24

def each(&block)
  @array.each(&block)
end

#insert(index, *objects) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/volt/reactive/reactive_array.rb', line 137

def insert(index, *objects)
  result = @array.insert(index, *objects)

  # All objects from index to the end have "changed"
  index.upto(result.size-1) do |idx|
    trigger_for_index!('changed', idx)
  end

  objects.size.times do |count|
    trigger_on_direct_listeners!('added', index+count)
  end

  trigger_size_change!

  return result
end

#inspectObject



232
233
234
# File 'lib/volt/reactive/reactive_array.rb', line 232

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



181
182
183
184
185
186
187
188
189
190
191
# File 'lib/volt/reactive/reactive_array.rb', line 181

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:

  1. uses the whole array (max, sum, etc…)

  2. accesses this specific element - array

  3. accesses an element via a method (first, last)



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/volt/reactive/reactive_array.rb', line 200

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



154
155
156
157
158
159
160
161
# File 'lib/volt/reactive/reactive_array.rb', line 154

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



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/volt/reactive/reactive_array.rb', line 163

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