Class: QueryListener

Inherits:
Object show all
Defined in:
lib/volt/models/persistors/query/query_listener.rb

Overview

The query listener is what gets notified on the backend when the results from a query have changed. It then will make the necessary changes to any ArrayStore’s to get them to display the new data.

Instance Method Summary collapse

Constructor Details

#initialize(query_listener_pool, tasks, collection, query) ⇒ QueryListener

Returns a new instance of QueryListener.



5
6
7
8
9
10
11
12
13
14
# File 'lib/volt/models/persistors/query/query_listener.rb', line 5

def initialize(query_listener_pool, tasks, collection, query)
  @query_listener_pool = query_listener_pool
  @tasks = tasks
  @stores = []

  @collection = collection
  @query = query

  @listening = false
end

Instance Method Details

#add_listenerObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/volt/models/persistors/query/query_listener.rb', line 16

def add_listener
  @listening = true
  QueryTasks.add_listener(@collection, @query).then do |ret|
    results, errors = ret

    # When the initial data comes back, add it into the stores.
    @stores.dup.each do |store|
      # Clear if there are existing items
      store.model.clear if store.model.size > 0

      results.each do |index, data|
        store.add(index, data)
      end

      store.change_state_to(:loaded)
    end
  end.fail do |err|
    puts "Err: #{err.inspect}"
  end
end

#add_store(store, &block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/volt/models/persistors/query/query_listener.rb', line 37

def add_store(store, &block)
  @stores << store

  if @listening
    # We are already listening and have this model somewhere else,
    # copy the data from the existing model.
    store.model.clear
    @stores.first.model.each_with_index do |item, index|
      store.add(index, item)
    end
  else
    # First time we've added a store, setup the listener and get
    # the initial data.
    add_listener
  end
end

#added(index, data) ⇒ Object



70
71
72
73
74
# File 'lib/volt/models/persistors/query/query_listener.rb', line 70

def added(index, data)
  @stores.each do |store|
    store.add(index, data)
  end
end

#changed(model_id, data) ⇒ Object



82
83
84
85
86
# File 'lib/volt/models/persistors/query/query_listener.rb', line 82

def changed(model_id, data)
  $loading_models = true
  Persistors::ModelStore.changed(model_id, data)
  $loading_models = false
end

#remove_store(store) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/volt/models/persistors/query/query_listener.rb', line 54

def remove_store(store)
  @stores.delete(store)

  # When there are no stores left, remove the query listener from
  # the pool, it can get created again later.
  if @stores.size == 0
    @query_listener_pool.remove(@collection, @query)

    # Stop listening
    if @listening
      @listening = false
      QueryTasks.remove_listener(@collection, @query)
    end
  end
end

#removed(ids) ⇒ Object



76
77
78
79
80
# File 'lib/volt/models/persistors/query/query_listener.rb', line 76

def removed(ids)
  @stores.each do |store|
    store.remove(ids)
  end
end