Class: QueryTasks

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

Constant Summary collapse

@@channel_live_queries =
{}

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Volt::Task

inherited, known_handlers, method_missing, #store

Constructor Details

#initialize(channel, dispatcher = nil) ⇒ QueryTasks

The dispatcher passes its self in



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

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

  # Load the query pool if not already setup
  self.class.live_query_pool
end

Class Method Details

.live_query_poolObject



6
7
8
# File 'app/volt/tasks/query_tasks.rb', line 6

def self.live_query_pool
  @@live_query_pool ||= LiveQueryPool.new(Volt::DataStore.fetch)
end

Instance Method Details

#add_listener(collection, query) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/volt/tasks/query_tasks.rb', line 19

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

  if @channel
    # For requests from the client (with @channel), we track the channel
    # so we can send the results back.  Server side requests don't stay live,
    # they simply return to :dirty once the query is issued.
    @channel.user_id = Volt.current_user_id

    # live_query.add_channel(@channel)
  end
  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

  if initial_data
    # Only send the filtered attributes for this user
    initial_data.map! do |data|
      [data[0], live_query.model_for_filter(data[1]).filtered_attributes]
    end
  end

  # @@live_query_pool.print

  [initial_data, error]
end

#close!Object

Removes a channel from all associated live queries



70
71
72
73
74
75
76
77
78
79
80
# File 'app/volt/tasks/query_tasks.rb', line 70

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



55
56
57
58
59
60
# File 'app/volt/tasks/query_tasks.rb', line 55

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.



64
65
66
67
# File 'app/volt/tasks/query_tasks.rb', line 64

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