Class: LiveQuery

Inherits:
Object show all
Defined in:
app/volt/tasks/live_query/live_query.rb

Overview

Tracks a channel and a query on a collection. Alerts the listener when the data in the query changes.

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#collectionObject (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_idsObject (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

#queryObject (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_dataObject

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.send_message('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.send_message('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.send_message('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.send_message('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