Class: LiveQuery
Overview
Tracks a channel and a query on a collection. Alerts the listener when the data in the query changes.
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
-
#current_ids ⇒ Object
readonly
Returns the value of attribute current_ids.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
Instance Method Summary collapse
- #add_channel(channel) ⇒ Object
-
#initial_data ⇒ Object
return the query results the first time a channel connects.
-
#initialize(pool, data_store, collection, query) ⇒ LiveQuery
constructor
A new instance of LiveQuery.
- #notify!(skip_channel = nil, only_channel = nil) ⇒ Object
- #notify_added(index, data, skip_channel) ⇒ Object
- #notify_changed(id, data, skip_channel) ⇒ Object
- #notify_moved(id, new_position, skip_channel) ⇒ Object
- #notify_removed(ids, skip_channel) ⇒ Object
- #remove_channel(channel) ⇒ Object
- #run(skip_channel = nil) ⇒ Object
Constructor Details
#initialize(pool, data_store, collection, query) ⇒ LiveQuery
Returns a new instance of LiveQuery.
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'app/volt/tasks/live_query/live_query.rb', line 8 def initialize(pool, data_store, collection, query) @pool = pool @collection = collection @query = query @channels = [] @data_store = data_store @query_tracker = QueryTracker.new(self, @data_store) run end |
Instance Attribute Details
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
6 7 8 |
# File 'app/volt/tasks/live_query/live_query.rb', line 6 def collection @collection end |
#current_ids ⇒ Object (readonly)
Returns the value of attribute current_ids.
6 7 8 |
# File 'app/volt/tasks/live_query/live_query.rb', line 6 def current_ids @current_ids end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
6 7 8 |
# File 'app/volt/tasks/live_query/live_query.rb', line 6 def query @query end |
Instance Method Details
#add_channel(channel) ⇒ Object
62 63 64 |
# File 'app/volt/tasks/live_query/live_query.rb', line 62 def add_channel(channel) @channels << channel end |
#initial_data ⇒ Object
return the query results the first time a channel connects
54 55 56 57 58 59 60 |
# File 'app/volt/tasks/live_query/live_query.rb', line 54 def initial_data @query_tracker.results.map.with_index do |data, index| data = data.dup data['_id'] = data['_id'].to_s [index, data] end end |
#notify!(skip_channel = nil, only_channel = nil) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'app/volt/tasks/live_query/live_query.rb', line 75 def notify!(skip_channel = nil, only_channel = nil) if only_channel channels = [only_channel] else channels = @channels end channels = channels.reject { |c| c == skip_channel } channels.each do |channel| yield(channel) end end |
#notify_added(index, data, skip_channel) ⇒ Object
32 33 34 35 36 37 |
# File 'app/volt/tasks/live_query/live_query.rb', line 32 def notify_added(index, data, skip_channel) # puts "Added: #{index} - #{data.inspect}" notify! do |channel| channel.('added', nil, @collection, @query, index, data) end end |
#notify_changed(id, data, skip_channel) ⇒ Object
46 47 48 49 50 51 |
# File 'app/volt/tasks/live_query/live_query.rb', line 46 def notify_changed(id, data, skip_channel) # puts "Changed: #{id}, #{data}" notify!(skip_channel) do |channel| channel.('changed', nil, @collection, @query, id, data) end end |
#notify_moved(id, new_position, skip_channel) ⇒ Object
39 40 41 42 43 44 |
# File 'app/volt/tasks/live_query/live_query.rb', line 39 def notify_moved(id, new_position, skip_channel) # puts "Moved: #{id}, #{new_position}" notify! do |channel| channel.('moved', nil, @collection, @query, id, new_position) end end |
#notify_removed(ids, skip_channel) ⇒ Object
25 26 27 28 29 30 |
# File 'app/volt/tasks/live_query/live_query.rb', line 25 def notify_removed(ids, skip_channel) # puts "Removed: #{ids.inspect}" notify! do |channel| channel.('removed', nil, @collection, @query, ids) end end |
#remove_channel(channel) ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'app/volt/tasks/live_query/live_query.rb', line 66 def remove_channel(channel) @channels.delete(channel) if @channels.size == 0 # remove this query, no one is listening anymore @pool.remove(@collection, @query) end end |
#run(skip_channel = nil) ⇒ Object
21 22 23 |
# File 'app/volt/tasks/live_query/live_query.rb', line 21 def run(skip_channel = nil) @query_tracker.run(skip_channel) end |