Class: Volt::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.



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

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



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

def add_listener
  @listening = true

  # Call the backend and add the listner
  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



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

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.to_h)
    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



73
74
75
76
77
# File 'lib/volt/models/persistors/query/query_listener.rb', line 73

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

#changed(model_id, data) ⇒ Object



85
86
87
88
89
# File 'lib/volt/models/persistors/query/query_listener.rb', line 85

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

#remove_store(store) ⇒ Object



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

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



79
80
81
82
83
# File 'lib/volt/models/persistors/query/query_listener.rb', line 79

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