volt 0.8.14 → 0.8.15

Sign up to get free protection for your applications and to get access to all the features.
Files changed (150) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -1
  3. data/Readme.md +8 -2
  4. data/VERSION +1 -1
  5. data/app/volt/controllers/notices_controller.rb +1 -1
  6. data/app/volt/models/user.rb +2 -2
  7. data/app/volt/tasks/live_query/live_query_pool.rb +1 -1
  8. data/app/volt/tasks/query_tasks.rb +1 -1
  9. data/app/volt/tasks/store_tasks.rb +1 -1
  10. data/app/volt/tasks/user_tasks.rb +2 -2
  11. data/lib/volt/boot.rb +2 -2
  12. data/lib/volt/cli/asset_compile.rb +31 -27
  13. data/lib/volt/cli.rb +64 -65
  14. data/lib/volt/config.rb +25 -23
  15. data/lib/volt/console.rb +17 -16
  16. data/lib/volt/controllers/model_controller.rb +82 -80
  17. data/lib/volt/data_stores/data_store.rb +2 -2
  18. data/lib/volt/data_stores/mongo_driver.rb +2 -2
  19. data/lib/volt/extra_core/inflections.rb +2 -2
  20. data/lib/volt/extra_core/inflector/inflections.rb +185 -183
  21. data/lib/volt/extra_core/inflector/methods.rb +50 -48
  22. data/lib/volt/extra_core/string.rb +2 -2
  23. data/lib/volt/models/array_model.rb +93 -92
  24. data/lib/volt/models/cursor.rb +3 -2
  25. data/lib/volt/models/model.rb +248 -251
  26. data/lib/volt/models/model_hash_behaviour.rb +44 -44
  27. data/lib/volt/models/model_helpers.rb +38 -36
  28. data/lib/volt/models/model_state.rb +16 -17
  29. data/lib/volt/models/model_wrapper.rb +25 -24
  30. data/lib/volt/models/persistors/array_store.rb +145 -143
  31. data/lib/volt/models/persistors/base.rb +18 -16
  32. data/lib/volt/models/persistors/flash.rb +24 -22
  33. data/lib/volt/models/persistors/local_store.rb +46 -44
  34. data/lib/volt/models/persistors/model_identity_map.rb +10 -8
  35. data/lib/volt/models/persistors/model_store.rb +76 -76
  36. data/lib/volt/models/persistors/params.rb +19 -17
  37. data/lib/volt/models/persistors/query/query_listener.rb +65 -63
  38. data/lib/volt/models/persistors/query/query_listener_pool.rb +12 -10
  39. data/lib/volt/models/persistors/store.rb +28 -28
  40. data/lib/volt/models/persistors/store_factory.rb +12 -10
  41. data/lib/volt/models/persistors/store_state.rb +33 -31
  42. data/lib/volt/models/url.rb +96 -104
  43. data/lib/volt/models/validations.rb +56 -54
  44. data/lib/volt/models/validators/length_validator.rb +24 -22
  45. data/lib/volt/models/validators/presence_validator.rb +14 -12
  46. data/lib/volt/page/bindings/attribute_binding.rb +106 -106
  47. data/lib/volt/page/bindings/base_binding.rb +23 -21
  48. data/lib/volt/page/bindings/component_binding.rb +3 -1
  49. data/lib/volt/page/bindings/content_binding.rb +34 -34
  50. data/lib/volt/page/bindings/each_binding.rb +113 -113
  51. data/lib/volt/page/bindings/event_binding.rb +38 -34
  52. data/lib/volt/page/bindings/if_binding.rb +56 -54
  53. data/lib/volt/page/bindings/template_binding/grouped_controllers.rb +24 -22
  54. data/lib/volt/page/bindings/template_binding.rb +182 -185
  55. data/lib/volt/page/channel.rb +79 -77
  56. data/lib/volt/page/channel_stub.rb +29 -27
  57. data/lib/volt/page/document.rb +6 -5
  58. data/lib/volt/page/document_events.rb +54 -52
  59. data/lib/volt/page/page.rb +139 -138
  60. data/lib/volt/page/string_template_renderer.rb +36 -36
  61. data/lib/volt/page/sub_context.rb +26 -25
  62. data/lib/volt/page/targets/attribute_section.rb +27 -25
  63. data/lib/volt/page/targets/attribute_target.rb +7 -6
  64. data/lib/volt/page/targets/base_section.rb +27 -26
  65. data/lib/volt/page/targets/binding_document/base_node.rb +3 -1
  66. data/lib/volt/page/targets/binding_document/component_node.rb +85 -82
  67. data/lib/volt/page/targets/binding_document/html_node.rb +11 -9
  68. data/lib/volt/page/targets/dom_section.rb +78 -77
  69. data/lib/volt/page/targets/dom_target.rb +8 -6
  70. data/lib/volt/page/targets/dom_template.rb +90 -88
  71. data/lib/volt/page/targets/helpers/comment_searchers.rb +51 -49
  72. data/lib/volt/page/tasks.rb +59 -57
  73. data/lib/volt/page/template_renderer.rb +17 -14
  74. data/lib/volt/page/url_tracker.rb +26 -24
  75. data/lib/volt/reactive/computation.rb +87 -88
  76. data/lib/volt/reactive/dependency.rb +30 -28
  77. data/lib/volt/reactive/eventable.rb +64 -62
  78. data/lib/volt/reactive/hash_dependency.rb +25 -23
  79. data/lib/volt/reactive/reactive_accessors.rb +34 -32
  80. data/lib/volt/reactive/reactive_array.rb +162 -162
  81. data/lib/volt/reactive/reactive_hash.rb +37 -35
  82. data/lib/volt/router/routes.rb +99 -101
  83. data/lib/volt/server/component_handler.rb +20 -21
  84. data/lib/volt/server/component_templates.rb +72 -70
  85. data/lib/volt/server/html_parser/attribute_scope.rb +109 -99
  86. data/lib/volt/server/html_parser/each_scope.rb +17 -16
  87. data/lib/volt/server/html_parser/if_view_scope.rb +51 -49
  88. data/lib/volt/server/html_parser/sandlebars_parser.rb +184 -177
  89. data/lib/volt/server/html_parser/textarea_scope.rb +24 -22
  90. data/lib/volt/server/html_parser/view_handler.rb +66 -65
  91. data/lib/volt/server/html_parser/view_parser.rb +23 -21
  92. data/lib/volt/server/html_parser/view_scope.rb +142 -141
  93. data/lib/volt/server/rack/asset_files.rb +81 -79
  94. data/lib/volt/server/rack/component_code.rb +17 -15
  95. data/lib/volt/server/rack/component_html_renderer.rb +14 -12
  96. data/lib/volt/server/rack/component_paths.rb +72 -71
  97. data/lib/volt/server/rack/index_files.rb +36 -39
  98. data/lib/volt/server/rack/opal_files.rb +43 -41
  99. data/lib/volt/server/rack/source_map_server.rb +23 -21
  100. data/lib/volt/server/socket_connection_handler.rb +46 -45
  101. data/lib/volt/server/socket_connection_handler_stub.rb +21 -19
  102. data/lib/volt/server.rb +60 -58
  103. data/lib/volt/spec/setup.rb +3 -3
  104. data/lib/volt/tasks/dispatcher.rb +24 -23
  105. data/lib/volt/tasks/task_handler.rb +35 -33
  106. data/lib/volt/utils/ejson.rb +8 -6
  107. data/lib/volt/utils/generic_counting_pool.rb +33 -31
  108. data/lib/volt/utils/generic_pool.rb +73 -70
  109. data/lib/volt/utils/local_storage.rb +42 -38
  110. data/lib/volt/volt/environment.rb +1 -1
  111. data/lib/volt.rb +44 -42
  112. data/spec/apps/kitchen_sink/app/main/assets/css/todos.css +28 -0
  113. data/spec/apps/kitchen_sink/app/main/config/routes.rb +1 -0
  114. data/spec/apps/kitchen_sink/app/main/controllers/main_controller.rb +2 -2
  115. data/spec/apps/kitchen_sink/app/main/controllers/todos_controller.rb +17 -0
  116. data/spec/apps/kitchen_sink/app/main/views/main/main.html +1 -0
  117. data/spec/apps/kitchen_sink/app/main/views/todos/index.html +24 -0
  118. data/spec/apps/kitchen_sink/config.ru +1 -1
  119. data/spec/controllers/reactive_accessors_spec.rb +5 -5
  120. data/spec/extra_core/inflector_spec.rb +2 -2
  121. data/spec/integration/list_spec.rb +68 -0
  122. data/spec/models/model_spec.rb +57 -57
  123. data/spec/models/persistors/params_spec.rb +6 -6
  124. data/spec/models/persistors/store_spec.rb +7 -7
  125. data/spec/models/validations_spec.rb +3 -3
  126. data/spec/page/bindings/content_binding_spec.rb +7 -7
  127. data/spec/page/bindings/template_binding_spec.rb +4 -5
  128. data/spec/page/sub_context_spec.rb +2 -2
  129. data/spec/reactive/computation_spec.rb +10 -10
  130. data/spec/reactive/dependency_spec.rb +2 -2
  131. data/spec/reactive/eventable_spec.rb +4 -4
  132. data/spec/reactive/reactive_array_spec.rb +13 -13
  133. data/spec/router/routes_spec.rb +5 -5
  134. data/spec/server/html_parser/sandlebars_parser_spec.rb +9 -9
  135. data/spec/server/html_parser/view_parser_spec.rb +27 -27
  136. data/spec/server/rack/asset_files_spec.rb +5 -5
  137. data/spec/server/rack/component_paths_spec.rb +2 -2
  138. data/spec/tasks/live_query_spec.rb +2 -2
  139. data/spec/tasks/query_tasks.rb +1 -1
  140. data/spec/tasks/query_tracker_spec.rb +1 -1
  141. data/spec/templates/targets/binding_document/component_node_spec.rb +2 -2
  142. data/spec/utils/generic_counting_pool_spec.rb +2 -2
  143. data/spec/utils/generic_pool_spec.rb +2 -2
  144. data/templates/component/controllers/main_controller.rb +1 -1
  145. data/templates/model/model.rb.tt +2 -2
  146. data/templates/newgem/app/newgem/controllers/main_controller.rb.tt +2 -2
  147. data/templates/project/app/main/controllers/main_controller.rb +1 -1
  148. data/templates/project/config.ru +1 -1
  149. metadata +10 -3
  150. data/app/volt/assets/js/vertxbus.js +0 -216
@@ -1,24 +1,26 @@
1
- module Persistors
2
- # Implements the base persistor functionality.
3
- class Base
4
- def loaded(initial_state=nil)
5
- end
1
+ module Volt
2
+ module Persistors
3
+ # Implements the base persistor functionality.
4
+ class Base
5
+ def loaded(initial_state=nil)
6
+ end
6
7
 
7
- def changed(attribute_name)
8
- end
8
+ def changed(attribute_name)
9
+ end
9
10
 
10
- def added(model, index)
11
- end
11
+ def added(model, index)
12
+ end
12
13
 
13
- # For removed, the default action is to call changed for it
14
- def removed(attribute_name)
15
- changed(attribute_name)
16
- end
14
+ # For removed, the default action is to call changed for it
15
+ def removed(attribute_name)
16
+ changed(attribute_name)
17
+ end
17
18
 
18
- def event_added(event, first, first_for_event)
19
- end
19
+ def event_added(event, first, first_for_event)
20
+ end
20
21
 
21
- def event_removed(event, last, last_for_event)
22
+ def event_removed(event, last, last_for_event)
23
+ end
22
24
  end
23
25
  end
24
26
  end
@@ -1,31 +1,33 @@
1
1
  require 'volt/models/persistors/base'
2
2
 
3
- module Persistors
4
- class Flash < Base
5
- def initialize(model)
6
- @model = model
7
- end
3
+ module Volt
4
+ module Persistors
5
+ class Flash < Base
6
+ def initialize(model)
7
+ @model = model
8
+ end
8
9
 
9
- def added(model, index)
10
- if Volt.client?
11
- # Setup a new timer for clearing the flash.
12
- %x{
13
- setTimeout(function() {
14
- self.$clear_model(model);
15
- }, 5000);
16
- }
10
+ def added(model, index)
11
+ if Volt.client?
12
+ # Setup a new timer for clearing the flash.
13
+ %x{
14
+ setTimeout(function() {
15
+ self.$clear_model(model);
16
+ }, 5000);
17
+ }
18
+ end
17
19
  end
18
- end
19
20
 
20
- def clear_model(model)
21
- @model.delete(model)
21
+ def clear_model(model)
22
+ @model.delete(model)
22
23
 
23
- # Clear out the parent collection (usually the main flash)
24
- # Makes it so flash.empty? reflects if there is any outstanding
25
- # flashes.
26
- if @model.size == 0
27
- collection_name = @model.path[-1]
28
- @model.parent.delete(collection_name)
24
+ # Clear out the parent collection (usually the main flash)
25
+ # Makes it so flash.empty? reflects if there is any outstanding
26
+ # flashes.
27
+ if @model.size == 0
28
+ collection_name = @model.path[-1]
29
+ @model.parent.delete(collection_name)
30
+ end
29
31
  end
30
32
  end
31
33
  end
@@ -2,64 +2,66 @@ require 'volt/models/persistors/base'
2
2
  require 'volt/utils/local_storage'
3
3
  require 'json'
4
4
 
5
- module Persistors
5
+ module Volt
6
+ module Persistors
6
7
 
7
- # Backs a collection in the local store
8
- class LocalStore < Base
9
- def initialize(model)
10
- @model = model
11
- end
8
+ # Backs a collection in the local store
9
+ class LocalStore < Base
10
+ def initialize(model)
11
+ @model = model
12
+ end
12
13
 
13
- # Find the root for this model
14
- def root_model
15
- node = @model
14
+ # Find the root for this model
15
+ def root_model
16
+ node = @model
16
17
 
17
- loop do
18
- parent = node.parent
19
- if parent
20
- node = parent
21
- else
22
- break
18
+ loop do
19
+ parent = node.parent
20
+ if parent
21
+ node = parent
22
+ else
23
+ break
24
+ end
23
25
  end
24
- end
25
26
 
26
- return node
27
- end
27
+ return node
28
+ end
28
29
 
29
- # Called when a model is added to the collection
30
- def added(model, index)
31
- root_model.persistor.save_all
32
- end
30
+ # Called when a model is added to the collection
31
+ def added(model, index)
32
+ root_model.persistor.save_all
33
+ end
33
34
 
34
- def loaded(initial_state=nil)
35
- # When the main model is first loaded, we pull in the data from the
36
- # store if it exists
37
- if @model.path == []
38
- json_data = LocalStorage['volt-store']
39
- if json_data
40
- root_attributes = JSON.parse(json_data)
35
+ def loaded(initial_state=nil)
36
+ # When the main model is first loaded, we pull in the data from the
37
+ # store if it exists
38
+ if @model.path == []
39
+ json_data = LocalStorage['volt-store']
40
+ if json_data
41
+ root_attributes = JSON.parse(json_data)
41
42
 
42
- @loading_data = true
43
- root_attributes.each_pair do |key, value|
44
- @model.send(:"_#{key}=", value)
43
+ @loading_data = true
44
+ root_attributes.each_pair do |key, value|
45
+ @model.send(:"_#{key}=", value)
46
+ end
47
+ @loading_data = nil
45
48
  end
46
- @loading_data = nil
47
49
  end
48
50
  end
49
- end
50
51
 
51
- # Callled when an item is changed (or removed)
52
- def changed(attribute_name)
53
- root_model.persistor.save_all
54
- end
52
+ # Callled when an item is changed (or removed)
53
+ def changed(attribute_name)
54
+ root_model.persistor.save_all
55
+ end
55
56
 
56
- # Called on the root
57
- def save_all
58
- return if @loading_data
57
+ # Called on the root
58
+ def save_all
59
+ return if @loading_data
59
60
 
60
- json_data = JSON.dump(@model.to_h)
61
+ json_data = JSON.dump(@model.to_h)
61
62
 
62
- LocalStorage['volt-store'] = json_data
63
+ LocalStorage['volt-store'] = json_data
64
+ end
63
65
  end
64
66
  end
65
- end
67
+ end
@@ -1,12 +1,14 @@
1
1
  require 'volt/utils/generic_counting_pool'
2
2
 
3
- # The identity map ensures that there is only one copy of a model
4
- # used on the front end at a time.
5
- class ModelIdentityMap < GenericCountingPool
6
- # add extends GenericCountingPool so it can add in a model without
7
- # a direct lookup. We use this when we create a model (without an id)
8
- # then save it and it gets assigned an id.
9
- def add(id, model)
10
- @pool[id] = [1, model]
3
+ module Volt
4
+ # The identity map ensures that there is only one copy of a model
5
+ # used on the front end at a time.
6
+ class ModelIdentityMap < GenericCountingPool
7
+ # add extends GenericCountingPool so it can add in a model without
8
+ # a direct lookup. We use this when we create a model (without an id)
9
+ # then save it and it gets assigned an id.
10
+ def add(id, model)
11
+ @pool[id] = [1, model]
12
+ end
11
13
  end
12
14
  end
@@ -1,121 +1,121 @@
1
1
  require 'volt/models/persistors/store'
2
2
  require 'volt/models/persistors/store_state'
3
3
 
4
- module Persistors
5
- class ModelStore < Store
6
- include StoreState
4
+ module Volt
5
+ module Persistors
6
+ class ModelStore < Store
7
+ include StoreState
7
8
 
8
- ID_CHARS = [('a'..'f'), ('0'..'9')].map {|v| v.to_a }.flatten
9
+ ID_CHARS = [('a'..'f'), ('0'..'9')].map { |v| v.to_a }.flatten
9
10
 
10
- attr_reader :model
11
- attr_accessor :in_identity_map
11
+ attr_reader :model
12
+ attr_accessor :in_identity_map
12
13
 
13
- def initialize(model, tasks)
14
- super
14
+ def initialize(model, tasks)
15
+ super
15
16
 
16
- @in_identity_map = false
17
- end
17
+ @in_identity_map = false
18
+ end
18
19
 
19
- def add_to_collection
20
- @in_collection = true
21
- ensure_setup
22
- changed
23
- end
20
+ def add_to_collection
21
+ @in_collection = true
22
+ ensure_setup
23
+ changed
24
+ end
24
25
 
25
- def remove_from_collection
26
- @in_collection = false
27
- end
26
+ def remove_from_collection
27
+ @in_collection = false
28
+ end
28
29
 
29
- # Called the first time a value is assigned into this model
30
- def ensure_setup
31
- if @model.attributes
32
- @model.attributes[:_id] ||= generate_id
30
+ # Called the first time a value is assigned into this model
31
+ def ensure_setup
32
+ if @model.attributes
33
+ @model.attributes[:_id] ||= generate_id
33
34
 
34
- add_to_identity_map
35
+ add_to_identity_map
36
+ end
35
37
  end
36
- end
37
38
 
38
- def add_to_identity_map
39
- unless @in_identity_map
40
- @@identity_map.add(@model._id, @model)
39
+ def add_to_identity_map
40
+ unless @in_identity_map
41
+ @@identity_map.add(@model._id, @model)
41
42
 
42
- @in_identity_map = true
43
+ @in_identity_map = true
44
+ end
43
45
  end
44
- end
45
46
 
46
- # Create a random unique id that can be used as the mongo id as well
47
- def generate_id
48
- id = []
49
- 24.times { id << ID_CHARS.sample }
47
+ # Create a random unique id that can be used as the mongo id as well
48
+ def generate_id
49
+ id = []
50
+ 24.times { id << ID_CHARS.sample }
50
51
 
51
- return id.join
52
- end
52
+ return id.join
53
+ end
53
54
 
54
- # Called when the model changes
55
- def changed(attribute_name=nil)
56
- path = @model.path
55
+ # Called when the model changes
56
+ def changed(attribute_name=nil)
57
+ path = @model.path
57
58
 
58
- promise = Promise.new
59
+ promise = Promise.new
59
60
 
60
- ensure_setup
61
+ ensure_setup
61
62
 
62
- path_size = path.size
63
- if !(defined?($loading_models) && $loading_models) && @tasks && path_size > 0 && !@model.nil?
64
- if path_size > 3 && (parent = @model.parent) && source = parent.parent
65
- @model.attributes[:"#{path[-4].singularize}_id"] = source._id
66
- end
63
+ path_size = path.size
64
+ if !(defined?($loading_models) && $loading_models) && @tasks && path_size > 0 && !@model.nil?
65
+ if path_size > 3 && (parent = @model.parent) && (source = parent.parent)
66
+ @model.attributes[:"#{path[-4].singularize}_id"] = source._id
67
+ end
67
68
 
68
- if !collection
69
- puts "Attempting to save model directly on store."
70
- raise "Attempting to save model directly on store."
71
- else
72
- StoreTasks.save(collection, self_attributes).then do |errors|
73
- if errors.size == 0
74
- promise.resolve(nil)
75
- else
76
- promise.reject(errors)
69
+ if !collection
70
+ puts "Attempting to save model directly on store."
71
+ raise "Attempting to save model directly on store."
72
+ else
73
+ StoreTasks.save(collection, self_attributes).then do |errors|
74
+ if errors.size == 0
75
+ promise.resolve(nil)
76
+ else
77
+ promise.reject(errors)
78
+ end
77
79
  end
78
80
  end
79
81
  end
82
+ promise
80
83
  end
81
84
 
82
- return promise
83
- end
84
-
85
- def event_added(event, first, first_for_event)
86
- if first_for_event && event == :changed
87
- ensure_setup
85
+ def event_added(event, first, first_for_event)
86
+ if first_for_event && event == :changed
87
+ ensure_setup
88
+ end
88
89
  end
89
- end
90
90
 
91
- # Update the models based on the id/identity map. Usually these requests
92
- # will come from the backend.
93
- def self.changed(model_id, data)
94
- model = @@identity_map.lookup(model_id)
91
+ # Update the models based on the id/identity map. Usually these requests
92
+ # will come from the backend.
93
+ def self.changed(model_id, data)
94
+ model = @@identity_map.lookup(model_id)
95
95
 
96
- if model
97
- data.each_pair do |key, value|
98
- if key != :_id
99
- model.send(:"_#{key}=", value)
96
+ if model
97
+ data.each_pair do |key, value|
98
+ if key != :_id
99
+ model.send(:"_#{key}=", value)
100
+ end
100
101
  end
101
102
  end
102
103
  end
103
- end
104
104
 
105
- def [](val)
106
- raise "Models do not support hash style lookup. Hashes inserted into other models are converted to models, see https://github.com/voltrb/volt#automatic-model-conversion"
107
- end
105
+ def [](val)
106
+ raise "Models do not support hash style lookup. Hashes inserted into other models are converted to models, see https://github.com/voltrb/volt#automatic-model-conversion"
107
+ end
108
108
 
109
- private
109
+ private
110
110
  # Return the attributes that are only for this store, not any sub-associations.
111
111
  def self_attributes
112
112
  # Don't store any sub-stores, those will do their own saving.
113
- @model.attributes.reject {|k,v| v.is_a?(Model) || v.is_a?(ArrayModel) }
113
+ @model.attributes.reject { |k, v| v.is_a?(Model) || v.is_a?(ArrayModel) }
114
114
  end
115
115
 
116
116
  def collection
117
117
  @model.path[-2]
118
118
  end
119
-
119
+ end
120
120
  end
121
121
  end
@@ -1,27 +1,29 @@
1
1
  require 'volt/models/persistors/base'
2
2
 
3
- module Persistors
4
- class Params < Base
5
- def initialize(model)
6
- @model = model
7
- end
3
+ module Volt
4
+ module Persistors
5
+ class Params < Base
6
+ def initialize(model)
7
+ @model = model
8
+ end
8
9
 
9
- def changed(attribute_name)
10
- if RUBY_PLATFORM == 'opal'
11
- %x{
12
- if (window.setTimeout && this.$run_update.bind) {
13
- if (window.paramsUpdateTimer) {
14
- clearTimeout(window.paramsUpdateTimer);
10
+ def changed(attribute_name)
11
+ if RUBY_PLATFORM == 'opal'
12
+ %x{
13
+ if (window.setTimeout && this.$run_update.bind) {
14
+ if (window.paramsUpdateTimer) {
15
+ clearTimeout(window.paramsUpdateTimer);
16
+ }
17
+ window.paramsUpdateTimer = setTimeout(this.$run_update.bind(this), 0);
15
18
  }
16
- window.paramsUpdateTimer = setTimeout(this.$run_update.bind(this), 0);
17
19
  }
18
- }
20
+ end
19
21
  end
20
- end
21
22
 
22
- def run_update
23
- if Volt.client?
24
- $page.url.update!
23
+ def run_update
24
+ if Volt.client?
25
+ $page.url.update!
26
+ end
25
27
  end
26
28
  end
27
29
  end
@@ -1,87 +1,89 @@
1
- # The query listener is what gets notified on the backend when the results from
2
- # a query have changed. It then will make the necessary changes to any ArrayStore's
3
- # to get them to display the new data.
4
- class QueryListener
5
- def initialize(query_listener_pool, tasks, collection, query)
6
- @query_listener_pool = query_listener_pool
7
- @tasks = tasks
8
- @stores = []
1
+ module Volt
2
+ # The query listener is what gets notified on the backend when the results from
3
+ # a query have changed. It then will make the necessary changes to any ArrayStore's
4
+ # to get them to display the new data.
5
+ class QueryListener
6
+ def initialize(query_listener_pool, tasks, collection, query)
7
+ @query_listener_pool = query_listener_pool
8
+ @tasks = tasks
9
+ @stores = []
9
10
 
10
- @collection = collection
11
- @query = query
11
+ @collection = collection
12
+ @query = query
12
13
 
13
- @listening = false
14
- end
14
+ @listening = false
15
+ end
15
16
 
16
- def add_listener
17
- @listening = true
18
- QueryTasks.add_listener(@collection, @query).then do |ret|
19
- results, errors = ret
17
+ def add_listener
18
+ @listening = true
19
+ QueryTasks.add_listener(@collection, @query).then do |ret|
20
+ results, errors = ret
20
21
 
21
- # When the initial data comes back, add it into the stores.
22
- @stores.dup.each do |store|
23
- # Clear if there are existing items
24
- store.model.clear if store.model.size > 0
22
+ # When the initial data comes back, add it into the stores.
23
+ @stores.dup.each do |store|
24
+ # Clear if there are existing items
25
+ store.model.clear if store.model.size > 0
25
26
 
26
- results.each do |index, data|
27
- store.add(index, data)
28
- end
27
+ results.each do |index, data|
28
+ store.add(index, data)
29
+ end
29
30
 
30
- store.change_state_to(:loaded)
31
+ store.change_state_to(:loaded)
32
+ end
33
+ end.fail do |err|
34
+ puts "Err: #{err.inspect}"
31
35
  end
32
- end.fail do |err|
33
- puts "Err: #{err.inspect}"
34
36
  end
35
- end
36
37
 
37
- def add_store(store, &block)
38
- @stores << store
38
+ def add_store(store, &block)
39
+ @stores << store
39
40
 
40
- if @listening
41
- # We are already listening and have this model somewhere else,
42
- # copy the data from the existing model.
43
- store.model.clear
44
- @stores.first.model.each_with_index do |item, index|
45
- store.add(index, item.to_h)
41
+ if @listening
42
+ # We are already listening and have this model somewhere else,
43
+ # copy the data from the existing model.
44
+ store.model.clear
45
+ @stores.first.model.each_with_index do |item, index|
46
+ store.add(index, item.to_h)
47
+ end
48
+ else
49
+ # First time we've added a store, setup the listener and get
50
+ # the initial data.
51
+ add_listener
46
52
  end
47
- else
48
- # First time we've added a store, setup the listener and get
49
- # the initial data.
50
- add_listener
51
53
  end
52
- end
53
54
 
54
- def remove_store(store)
55
- @stores.delete(store)
55
+ def remove_store(store)
56
+ @stores.delete(store)
56
57
 
57
- # When there are no stores left, remove the query listener from
58
- # the pool, it can get created again later.
59
- if @stores.size == 0
60
- @query_listener_pool.remove(@collection, @query)
58
+ # When there are no stores left, remove the query listener from
59
+ # the pool, it can get created again later.
60
+ if @stores.size == 0
61
+ @query_listener_pool.remove(@collection, @query)
61
62
 
62
- # Stop listening
63
- if @listening
64
- @listening = false
65
- QueryTasks.remove_listener(@collection, @query)
63
+ # Stop listening
64
+ if @listening
65
+ @listening = false
66
+ QueryTasks.remove_listener(@collection, @query)
67
+ end
66
68
  end
67
69
  end
68
- end
69
70
 
70
- def added(index, data)
71
- @stores.each do |store|
72
- store.add(index, data)
71
+ def added(index, data)
72
+ @stores.each do |store|
73
+ store.add(index, data)
74
+ end
73
75
  end
74
- end
75
76
 
76
- def removed(ids)
77
- @stores.each do |store|
78
- store.remove(ids)
77
+ def removed(ids)
78
+ @stores.each do |store|
79
+ store.remove(ids)
80
+ end
79
81
  end
80
- end
81
82
 
82
- def changed(model_id, data)
83
- $loading_models = true
84
- Persistors::ModelStore.changed(model_id, data)
85
- $loading_models = false
83
+ def changed(model_id, data)
84
+ $loading_models = true
85
+ Persistors::ModelStore.changed(model_id, data)
86
+ $loading_models = false
87
+ end
86
88
  end
87
89
  end
@@ -1,17 +1,19 @@
1
1
  require 'volt/utils/generic_pool'
2
2
  require 'volt/models/persistors/query/query_listener'
3
3
 
4
- # Keeps track of all query listeners, so they can be reused in different
5
- # places. Dynamically generated queries may end up producing the same
6
- # query in different places. This makes it so we only need to track a
7
- # single query at once. Data updates will only be sent once as well.
8
- class QueryListenerPool < GenericPool
9
- def print
10
- puts "--- Running Queries ---"
4
+ module Volt
5
+ # Keeps track of all query listeners, so they can be reused in different
6
+ # places. Dynamically generated queries may end up producing the same
7
+ # query in different places. This makes it so we only need to track a
8
+ # single query at once. Data updates will only be sent once as well.
9
+ class QueryListenerPool < GenericPool
10
+ def print
11
+ puts "--- Running Queries ---"
11
12
 
12
- @pool.each_pair do |table,query_hash|
13
- query_hash.keys.each do |query|
14
- puts "#{table}: #{query.inspect}"
13
+ @pool.each_pair do |table, query_hash|
14
+ query_hash.keys.each do |query|
15
+ puts "#{table}: #{query.inspect}"
16
+ end
15
17
  end
16
18
  end
17
19
  end