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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of QueryListener.



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

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 Attribute Details

#listeningObject (readonly)

Returns the value of attribute listening.



6
7
8
# File 'lib/volt/models/persistors/query/query_listener.rb', line 6

def listening
  @listening
end

Instance Method Details

#add_listenerObject



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
54
# File 'lib/volt/models/persistors/query/query_listener.rb', line 19

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.model.change_state_to(:loaded_state, :loaded)

      # if Volt.server?
      #   store.model.change_state_to(:loaded_state, :dirty)
      # end
    end
  end.fail do |err|
    # TODO: need to make it so we can re-raise out of this promise
    Volt.logger.error("Error adding listener: #{err.inspect}")
    Volt.logger.error(err.backtrace) if err.respond_to?(:backtrace)

    # If we get back that the user signature is wrong, log the user out.
    if err.to_s.start_with?('user id or hash is incorrectly signed')
      # Delete the invalid cookie
      $page.cookies.delete(:user_id)
    end

    raise err
  end
end

#add_store(store, &block) ⇒ Object



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

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

    # Get an existing store to copy data from
    first_store_model = @stores.first.model
    first_store_model.each_with_index do |item, index|
      store.add(index, item.to_h)
    end

    store.model.change_state_to(:loaded_state, first_store_model.loaded_state)
  else
    # First time we've added a store, setup the listener and get
    # the initial data.
    add_listener
  end
end

#added(index, data) ⇒ Object



94
95
96
97
98
# File 'lib/volt/models/persistors/query/query_listener.rb', line 94

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

#changed(model_id, data) ⇒ Object



106
107
108
109
110
# File 'lib/volt/models/persistors/query/query_listener.rb', line 106

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

#remove_store(store) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/volt/models/persistors/query/query_listener.rb', line 78

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



100
101
102
103
104
# File 'lib/volt/models/persistors/query/query_listener.rb', line 100

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