Class: Persistors::ArrayStore
- Includes:
- StoreState
- Defined in:
- lib/volt/models/persistors/array_store.rb
Constant Summary collapse
- @@query_pool =
QueryListenerPool.new
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Class Method Summary collapse
Instance Method Summary collapse
-
#add(index, data) ⇒ Object
Called from backend.
-
#added(model, index) ⇒ Object
When a model is added to this collection, we call its “changed” method.
- #channel_name ⇒ Object
- #event_added(event, scope_provider, first, first_for_event) ⇒ Object
- #event_removed(event, last, last_for_event) ⇒ Object
- #find(query = {}) ⇒ Object
-
#initialize(model, tasks = nil) ⇒ ArrayStore
constructor
A new instance of ArrayStore.
-
#load_data ⇒ Object
Called the first time data is requested from this collection.
- #remove(ids) ⇒ Object
- #removed(model) ⇒ Object
- #run_query(model, query = {}) ⇒ Object
-
#stop_listening ⇒ Object
Called when an event is removed and we no longer want to keep in sync with the database.
-
#then(&block) ⇒ Object
Returns a promise that is resolved/rejected when the query is complete.
-
#unload_data ⇒ Object
Clear out the models data, since we’re not listening anymore.
Methods included from StoreState
#change_state_to, #loaded, #state
Methods inherited from Store
Methods inherited from Base
Constructor Details
#initialize(model, tasks = nil) ⇒ ArrayStore
Returns a new instance of ArrayStore.
17 18 19 20 21 22 23 |
# File 'lib/volt/models/persistors/array_store.rb', line 17 def initialize(model, tasks=nil) super query = @model.[:query] @query = ReactiveValue.from_hash(query || {}) end |
Instance Attribute Details
#model ⇒ Object (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_pool ⇒ Object
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
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/volt/models/persistors/array_store.rb', line 127 def add(index, data) $loading_models = true = @model..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, , :loaded) } @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.
165 166 167 168 169 170 |
# File 'lib/volt/models/persistors/array_store.rb', line 165 def added(model, index) if model.persistor # Tell the persistor it was added model.persistor.add_to_collection end end |
#channel_name ⇒ Object
158 159 160 |
# File 'lib/volt/models/persistors/array_store.rb', line 158 def channel_name @model.path[-1] end |
#event_added(event, scope_provider, first, first_for_event) ⇒ Object
25 26 27 28 |
# File 'lib/volt/models/persistors/array_store.rb', line 25 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
30 31 32 33 |
# File 'lib/volt/models/persistors/array_store.rb', line 30 def event_removed(event, last, last_for_event) # Remove listener where there are no more events on this model stop_listening if last end |
#find(query = {}) ⇒ Object
101 102 103 104 105 |
# File 'lib/volt/models/persistors/array_store.rb', line 101 def find(query={}) model = Cursor.new([], @model..merge(:query => query)) return ReactiveValue.new(model) end |
#load_data ⇒ Object
Called the first time data is requested from this collection
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/volt/models/persistors/array_store.rb', line 52 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
143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/volt/models/persistors/array_store.rb', line 143 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
172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/volt/models/persistors/array_store.rb', line 172 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 @tasks.call('StoreTasks', 'delete', channel_name, model.attributes[:_id]) end end |
#run_query(model, query = {}) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/volt/models/persistors/array_store.rb', line 80 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 @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_listening ⇒ Object
Called when an event is removed and we no longer want to keep in sync with the database.
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/volt/models/persistors/array_store.rb', line 37 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 |
#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.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/volt/models/persistors/array_store.rb', line 109 def then(&block) promise = Promise.new promise = promise.then(&block) if block if @state == :loaded promise.resolve(@model) else @fetch_promises ||= [] @fetch_promises << promise load_data end return promise end |
#unload_data ⇒ Object
Clear out the models data, since we’re not listening anymore.
75 76 77 78 |
# File 'lib/volt/models/persistors/array_store.rb', line 75 def unload_data change_state_to :not_loaded @model.clear end |