Class: Persistors::ArrayStore

Inherits:
Store show all
Includes:
StoreState
Defined in:
lib/volt/models/persistors/array_store.rb

Constant Summary collapse

@@query_pool =
QueryListenerPool.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from StoreState

#change_state_to, #loaded, #state

Methods inherited from Store

#read_new_model, #saved?

Methods inherited from Base

#changed, #loaded

Constructor Details

#initialize(model, tasks = nil) ⇒ ArrayStore

Returns a new instance of ArrayStore.



17
18
19
20
21
22
23
24
# File 'lib/volt/models/persistors/array_store.rb', line 17

def initialize(model, tasks=nil)
  # puts "NEW ARRAY STORE FOR #{model.inspect}"
  super

  query = @model.options[:query]

  @query = ReactiveValue.from_hash(query || {})
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



11
12
13
# File 'lib/volt/models/persistors/array_store.rb', line 11

def model
  @model
end

Class Method Details

.query_poolObject



13
14
15
# File 'lib/volt/models/persistors/array_store.rb', line 13

def self.query_pool
  @@query_pool
end

Instance Method Details

#add(index, data) ⇒ Object

Called from backend



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/volt/models/persistors/array_store.rb', line 124

def add(index, data)
  $loading_models = true
  # puts "INSERT: #{data.inspect} into #{self.inspect}"

  new_options = @model.options.merge(path: @model.path + [:[]], parent: @model)

  # Don't add if the model is already in the ArrayModel
  if !@model.cur.array.find {|v| v['_id'] == data['_id'] }
    # Find the existing model, or create one
    new_model = @@identity_map.find(data['_id']) { @model.new_model(data.symbolize_keys, new_options, :loaded) }

    # puts "ADD: #{new_model.attributes.inspect}"
    @model.insert(index, new_model)
  end

  $loading_models = false
end

#added(model, index) ⇒ Object

When a model is added to this collection, we call its “changed” method. This should trigger a save.



168
169
170
171
172
173
# File 'lib/volt/models/persistors/array_store.rb', line 168

def added(model, index)
  if model.persistor
    # Tell the persistor it was added
    model.persistor.add_to_collection
  end
end

#channel_nameObject



161
162
163
# File 'lib/volt/models/persistors/array_store.rb', line 161

def channel_name
  @model.path[-1]
end

#event_added(event, scope_provider, first, first_for_event) ⇒ Object



26
27
28
29
# File 'lib/volt/models/persistors/array_store.rb', line 26

def event_added(event, scope_provider, first, first_for_event)
  # First event, we load the data.
  load_data if first
end

#event_removed(event, last, last_for_event) ⇒ Object



31
32
33
34
# File 'lib/volt/models/persistors/array_store.rb', line 31

def event_removed(event, last, last_for_event)
  # Remove listener where there are no more events on this model
  stop_listening if last
end

#fetch(&block) ⇒ Object

Fetch does a one time load of the data on an unloaded model and returns the result.



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/volt/models/persistors/array_store.rb', line 111

def fetch(&block)
  # puts "FETCH: #{@state.inspect}"
  if @state == :loaded
    block.call(@model)
  else
    @fetch_callbacks ||= []
    @fetch_callbacks << block

    load_data
  end
end

#find(query = {}) ⇒ Object



103
104
105
106
107
# File 'lib/volt/models/persistors/array_store.rb', line 103

def find(query={})
  model = Cursor.new([], @model.options.merge(:query => query))

  return ReactiveValue.new(model)
end

#load_dataObject

Called the first time data is requested from this collection



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/volt/models/persistors/array_store.rb', line 53

def load_data
  # Don't load data from any queried
  if @state == :not_loaded || @state == :dirty
    puts "Load Data at #{@model.path.inspect} - query: #{@query.inspect}"# on #{@model.inspect}"
    change_state_to :loading

    @query_changed_listener.remove if @query_changed_listener
    if @query.reactive?
      # puts "SETUP REACTIVE QUERY LISTENER: #{@query.inspect}"
      # Query might change, change the query when it does
      @query_changed_listener = @query.on('changed') do
        stop_listening

        # Don't load again if all of the listeners are gone
        load_data if @model.has_listeners?
      end
    end

    run_query(@model, @query.deep_cur)
  end
end

#remove(ids) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/volt/models/persistors/array_store.rb', line 142

def remove(ids)
  $loading_models = true
  ids.each do |id|
    puts "delete at: #{id} on #{@model.inspect}"

    # TODO: optimize this delete so we don't need to loop
    @model.each_with_index do |model, index|
      puts "#{model._id.inspect} vs #{id.inspect} - #{index}"
      if model._id == id
        del = @model.delete_at(index)
        puts "DELETED AT #{index}: #{del.inspect} - #{@model.inspect}"
        break
      end
    end
  end

  $loading_models = false
end

#removed(model) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/volt/models/persistors/array_store.rb', line 175

def removed(model)
  if model.persistor
    # Tell the persistor it was removed
    model.persistor.remove_from_collection
  end

  if defined?($loading_models) && $loading_models
    return
  else
    puts "delete #{channel_name} - #{model.attributes[:_id]}"
    @tasks.call('StoreTasks', 'delete', channel_name, model.attributes[:_id])
  end
end

#run_query(model, query = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/volt/models/persistors/array_store.rb', line 81

def run_query(model, query={})
  collection = model.path.last
  # Scope to the parent
  if model.path.size > 1
    parent = model.parent

    parent.persistor.ensure_setup if parent.persistor

    if parent && (attrs = parent.attributes) && attrs[:_id].true?
      query[:"#{model.path[-3].singularize}_id"] = attrs[:_id]
    end
  end

  # puts "IN QUERY: #{query.inspect} - #{self.inspect}"
  @query_listener = @@query_pool.lookup(collection, query) do
    # Create if it does not exist
    QueryListener.new(@@query_pool, @tasks, collection, query)
  end

  @query_listener.add_store(self)
end

#stop_listeningObject

Called when an event is removed and we no longer want to keep in sync with the database.



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/volt/models/persistors/array_store.rb', line 38

def stop_listening
  if @query_changed_listener
    @query_changed_listener.remove
    @query_changed_listener = nil
  end

  if @query_listener
    @query_listener.remove_store(self)
    @query_listener = nil
  end

  @state = :dirty
end

#unload_dataObject

Clear out the models data, since we’re not listening anymore.



76
77
78
79
# File 'lib/volt/models/persistors/array_store.rb', line 76

def unload_data
  change_state_to :not_loaded
  @model.clear
end