Class: QueryTasks

Inherits:
Volt::TaskHandler show all
Defined in:
app/volt/tasks/query_tasks.rb

Constant Summary collapse

@@live_query_pool =
LiveQueryPool.new(DataStore.new)
@@channel_live_queries =
{}

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Volt::TaskHandler

inherited, known_handlers, method_missing, #store

Constructor Details

#initialize(channel, dispatcher = nil) ⇒ QueryTasks

The dispatcher passes its self in



13
14
15
16
# File 'app/volt/tasks/query_tasks.rb', line 13

def initialize(channel, dispatcher = nil)
  @channel = channel
  @dispatcher = dispatcher
end

Class Method Details

.live_query_poolObject



8
9
10
# File 'app/volt/tasks/query_tasks.rb', line 8

def self.live_query_pool
  @@live_query_pool
end

Instance Method Details

#add_listener(collection, query) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/volt/tasks/query_tasks.rb', line 18

def add_listener(collection, query)
  live_query = @@live_query_pool.lookup(collection, query)
  track_channel_in_live_query(live_query)

  # puts "Load data on #{collection.inspect} - #{query.inspect}"
  live_query.add_channel(@channel)

  errors = {}

  begin
    # Get the initial data
    initial_data = live_query.initial_data
  rescue => exception
    # Capture and pass up any exceptions
    error = { error: exception.message }
  end

  [initial_data, error]
end

#close!Object

Removes a channel from all associated live queries



53
54
55
56
57
58
59
60
61
62
63
# File 'app/volt/tasks/query_tasks.rb', line 53

def close!
  live_queries = @@channel_live_queries[@channel]

  if live_queries
    live_queries.each do |live_query|
      live_query.remove_channel(@channel)
    end
  end

  @@channel_live_queries.delete(@channel)
end

#initial_dataObject



38
39
40
41
42
43
# File 'app/volt/tasks/query_tasks.rb', line 38

def initial_data
  data = live_query.initial_data
  data[:_id] = data[:_id].to_s

  data
end

#remove_listener(collection, query) ⇒ Object

Remove a listening channel, the LiveQuery will automatically remove itsself from the pool when there are no channels.



47
48
49
50
# File 'app/volt/tasks/query_tasks.rb', line 47

def remove_listener(collection, query)
  live_query = @@live_query_pool.lookup(collection, query)
  live_query.remove_channel(@channel)
end