Class: Volt::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.



19
20
21
22
23
24
25
26
27
# File 'lib/volt/models/persistors/array_store.rb', line 19

def initialize(model, tasks = nil)
  super

  @query = @model.options[:query]
  @limit = @model.options[:limit]
  @skip = @model.options[:skip]

  @skip = nil if @skip == 0
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



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

def model
  @model
end

Class Method Details

.query_poolObject



15
16
17
# File 'lib/volt/models/persistors/array_store.rb', line 15

def self.query_pool
  @@query_pool
end

Instance Method Details

#add(index, data) ⇒ Object

Called from backend



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/volt/models/persistors/array_store.rb', line 161

def add(index, data)
  $loading_models = true

  data_id = data['_id'] || data[:_id]

  # Don't add if the model is already in the ArrayModel
  unless @model.array.find { |v| v._id == data_id }
    # Find the existing model, or create one
    new_model = @@identity_map.find(data_id) do
      new_options = @model.options.merge(path: @model.path + [:[]], parent: @model)
      @model.new_model(data, new_options, :loaded)
    end

    @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.



201
202
203
204
205
206
# File 'lib/volt/models/persistors/array_store.rb', line 201

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

#channel_nameObject



195
196
197
# File 'lib/volt/models/persistors/array_store.rb', line 195

def channel_name
  @model.path[-1]
end

#event_added(event, first, first_for_event) ⇒ Object



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

def event_added(event, first, first_for_event)
  # First event, we load the data.
  if first
    @has_events = true
    load_data
  end
end

#event_removed(event, last, last_for_event) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/volt/models/persistors/array_store.rb', line 37

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

#find(query = nil, &block) ⇒ Object

Find can take either a query object, or a block that returns a query object. Use the block style if you need reactive updating queries



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/volt/models/persistors/array_store.rb', line 118

def find(query = nil, &block)
  # Set a default query if there is no block
  if block
    if query
      fail 'Query should not be passed in to a find if a block is specified'
    end
    query = block
  else
    query ||= {}
  end

  return Cursor.new([], @model.options.merge(query: query))
end

#limit(limit) ⇒ Object



132
133
134
# File 'lib/volt/models/persistors/array_store.rb', line 132

def limit(limit)
  return Cursor.new([], @model.options.merge(limit: limit))
end

#load_dataObject

Called the first time data is requested from this collection



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/volt/models/persistors/array_store.rb', line 62

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 #{self.inspect}"
    change_state_to :loading

    if @query.is_a?(Proc)
      @query_computation = -> do
        stop_listening(false)

        change_state_to :loading

        new_query = @query.call

        run_query(@model, @query.call, @skip, @limit)
      end.watch!
    else
      run_query(@model, @query, @skip, @limit)
    end
  end
end

#remove(ids) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/volt/models/persistors/array_store.rb', line 180

def remove(ids)
  $loading_models = true
  ids.each do |id|
    # TODO: optimize this delete so we don't need to loop
    @model.each_with_index do |model, index|
      if model._id == id
        del = @model.delete_at(index)
        break
      end
    end
  end

  $loading_models = false
end

#removed(model) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/volt/models/persistors/array_store.rb', line 208

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
    StoreTasks.delete(channel_name, model.attributes[:_id])
  end
end

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/volt/models/persistors/array_store.rb', line 91

def run_query(model, query={}, skip=nil, limit=nil)
  @model.clear

  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

  # The full query contains the skip and limit
  full_query = [query, skip, limit]
  @query_listener = @@query_pool.lookup(collection, full_query) do
    # Create if it does not exist
    QueryListener.new(@@query_pool, @tasks, collection, full_query)
  end

  @query_listener.add_store(self)
end

#skip(skip) ⇒ Object



136
137
138
# File 'lib/volt/models/persistors/array_store.rb', line 136

def skip(skip)
  return Cursor.new([], @model.options.merge(skip: skip))
end

#stop_listening(stop_watching_query = true) ⇒ Object

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



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/volt/models/persistors/array_store.rb', line 47

def stop_listening(stop_watching_query = true)
  return if @has_events
  return if @fetch_promises && @fetch_promises.size > 0

  @query_computation.stop if @query_computation && stop_watching_query

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

  @state = :dirty
end

#then(&block) ⇒ Object

Returns a promise that is resolved/rejected when the query is complete. Any passed block will be passed to the promises then. Then will be passed the model.



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

def then(&block)
  raise "then must pass a block" unless block
  promise = Promise.new

  promise = promise.then(&block)

  if @state == :loaded
    promise.resolve(@model)
  else
    @fetch_promises ||= []
    @fetch_promises << promise

    load_data
  end

  promise
end

#unload_dataObject

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



85
86
87
88
89
# File 'lib/volt/models/persistors/array_store.rb', line 85

def unload_data
  puts "Unload Data"
  change_state_to :not_loaded
  @model.clear
end