action_cable_notifications 0.1.18 → 0.1.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3d3881c154e82ff694803dae629b00ef8110d979
4
- data.tar.gz: be3fcb83d77cc590afbbc82982ef4eed706b304e
3
+ metadata.gz: 1dab7586f3ac550c6ac8b849b658c9b6298b13d4
4
+ data.tar.gz: 445cf42e5076f055d9c72cb83c71eb56a4fb87fe
5
5
  SHA512:
6
- metadata.gz: 3f7ce84fcef2aea235649357959f5ffaeae93af487922a2e06fadf371419e996a454b3c4f7929f3f53e298ab2ef289828c5a0e3921bfd600a10b4101e0b2e63f
7
- data.tar.gz: db6d782b49af060688c3bb53a8702ec421c6393f7ce50319da561405d5cb41ceeae6e67e2ae8615d3230db63d3b059c6a14f2e0501cd1114860ab81f6d9cf6ec
6
+ metadata.gz: e98437193f244b7b372fc090307cc6b386864d1b6adf7d3b32a9999ddaa7906c57a0b9dae3b15ba9e4ba2394fde4e695fb724a0f1bcb5bbfdbb8b6b7deeb1d62
7
+ data.tar.gz: eff1938cc3c84ca6fc76633109bc8e13d13de759956db8c89930c590752a099e1027c32c69204bccad5154c0d3e5857f67bcb03be5cf886bd7a7cb7d6e6ae1b4
@@ -28,6 +28,9 @@ class CableNotifications.Collection
28
28
  # Cleanup performed commands
29
29
  _.remove(@commandsCache, {performed: true})
30
30
 
31
+ # Fetch data from upstream server when connection is resumed
32
+ @fetch()
33
+
31
34
  # Public methods
32
35
  #######################################
33
36
 
@@ -40,11 +43,12 @@ class CableNotifications.Collection
40
43
  @sync = false
41
44
  # Stores upstream commands when there is no connection to the server
42
45
  @commandsCache = []
46
+ # Stores records that needs to be tracked when inserted into the collection
47
+ @trackedRecords = []
43
48
 
44
49
  # Bind private methods to class instance
45
50
  ########################################################
46
51
  upstream = upstream.bind(this)
47
- connectionChanged = connectionChanged.bind(this)
48
52
 
49
53
  # Sync collection to ActionCable Channel
50
54
  syncToChannel: (@channel) ->
@@ -52,7 +56,7 @@ class CableNotifications.Collection
52
56
 
53
57
  Tracker.autorun () =>
54
58
  @channel.isConnected()
55
- connectionChanged()
59
+ connectionChanged.call(this)
56
60
 
57
61
  # Fetch records from upstream
58
62
  fetch: (params) ->
@@ -63,21 +67,39 @@ class CableNotifications.Collection
63
67
  _.filter(@data, selector)
64
68
 
65
69
  # Find a record
66
- find: (selector={}) ->
67
- _.find(@data, selector)
70
+ find: (selector={}, options={}) ->
71
+ record = _.find(@data, selector)
72
+
73
+ if !record and options.track
74
+ if selector.id
75
+ trackedRecord = _.find(@trackedRecords, {id: selector.id})
76
+ if trackedRecord
77
+ trackedRecord
78
+ else
79
+ trackedRecord = {id: selector.id}
80
+ @trackedRecords.push trackedRecord
81
+ trackedRecord
82
+ else
83
+ console.warn("[find] Id must be specified to track records")
84
+ else
85
+ record
68
86
 
69
87
  # Creates a new record
70
88
  create: (fields={}) ->
71
89
  record = _.find(@data, {id: fields.id})
72
- if( record )
90
+ if record
73
91
  console.warn("[create] Not expected to find an existing record with id #{fields.id}")
74
92
  return
75
93
 
94
+ # Search in tracked records
95
+ recordIndex = _.findIndex(@trackedRecords, {id: fields.id})
96
+ if recordIndex>=0
97
+ fields = _.merge( @trackedRecords[recordIndex], fields )
98
+ @trackedRecords.splice(recordIndex, 1)
99
+
76
100
  @data.push (fields) unless @sync
77
101
 
78
- upstream("create",
79
- fields: fields
80
- )
102
+ upstream("create", {fields: fields})
81
103
  fields
82
104
 
83
105
  # Update an existing record
@@ -96,9 +96,6 @@ class CableNotifications.Store
96
96
  else
97
97
  collection.syncToChannel(channel)
98
98
  channelInfo.collections.push collection
99
-
100
- # Fetch data from uptream server
101
- collection.fetch()
102
99
  else
103
100
  # Initialize channelInfo
104
101
  @channels[channelId] =
@@ -120,7 +117,4 @@ class CableNotifications.Store
120
117
  # Assigns channel to collection and turns on Sync
121
118
  collection.syncToChannel(channel)
122
119
 
123
- # Fetch data from upstream server
124
- collection.fetch()
125
-
126
120
  return true
@@ -1,4 +1,5 @@
1
1
  require 'action_cable_notifications/channel_actions.rb'
2
+ require 'action_cable_notifications/channel_cache.rb'
2
3
 
3
4
  module ActionCableNotifications
4
5
  module Channel
@@ -71,6 +72,7 @@ module ActionCableNotifications
71
72
  private
72
73
 
73
74
  include ActionCableNotifications::Channel::Actions
75
+ include ActionCableNotifications::Channel::Cache
74
76
 
75
77
  #
76
78
  # Streams notification for ActiveRecord model changes
@@ -120,73 +122,5 @@ module ActionCableNotifications
120
122
  end
121
123
  end
122
124
 
123
- #
124
- # Updates server side cache of client side collections
125
- # XXX compute cache diff before sending to clients
126
- #
127
- def update_cache(packet)
128
- updated = false
129
-
130
- # Check if collection already exists
131
- new_collection = false
132
- if @collections[packet[:collection]].nil?
133
- @collections[packet[:collection]] = []
134
- new_collection = true
135
- end
136
-
137
- collection = @collections[packet[:collection]]
138
-
139
- case packet[:msg]
140
- when 'upsert_many'
141
- if new_collection
142
- packet[:data].each do |record|
143
- collection.push record
144
- end
145
- updated = true
146
- else
147
- packet[:data].each do |record|
148
- current_record = collection.find{|c| c[:id]==record[:id]}
149
- if current_record
150
- new_record = current_record.merge(record)
151
- if new_record != current_record
152
- current_record.merge!(record)
153
- updated = true
154
- end
155
- else
156
- collection.push record
157
- updated = true
158
- end
159
- end
160
- end
161
-
162
- when 'create'
163
- record = collection.find{|c| c[:id]==packet[:id]}
164
- if !record
165
- @collections[packet[:collection]].push packet[:data]
166
- updated = true
167
- end
168
-
169
- when 'update'
170
- record = @collections[packet[:collection]].find{|c| c[:id]==packet[:id]}
171
- if record
172
- record.merge!(packet[:data])
173
- updated = true
174
- end
175
-
176
- when 'destroy'
177
- index = @collections[packet[:collection]].find_index{|c| c.id==packet[:id]}
178
- if index
179
- @collections[packet[:collection]].delete_at(index)
180
- updated = true
181
- end
182
-
183
- else
184
- updated = true
185
- end
186
-
187
- updated
188
-
189
- end
190
-
191
125
  end
192
126
  end
@@ -0,0 +1,88 @@
1
+ module ActionCableNotifications
2
+ module Channel
3
+ module Cache
4
+
5
+ #
6
+ # Updates server side cache of client side collections
7
+ # XXX compute cache diff before sending to clients
8
+ #
9
+ def update_cache(packet)
10
+ updated = false
11
+
12
+ # Check if collection already exists
13
+ new_collection = false
14
+ if @collections[packet[:collection]].nil?
15
+ @collections[packet[:collection]] = []
16
+ new_collection = true
17
+ end
18
+
19
+ collection = @collections[packet[:collection]]
20
+
21
+ case packet[:msg]
22
+ when 'update_many'
23
+ if !new_collection
24
+ packet[:data].each do |record|
25
+ current_record = collection.find{|c| c[:id]==record[:id]}
26
+ if current_record
27
+ new_record = current_record.merge(record)
28
+ if new_record != current_record
29
+ current_record.merge!(record)
30
+ updated = true
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ when 'upsert_many'
37
+ if new_collection
38
+ packet[:data].each do |record|
39
+ collection.push record
40
+ end
41
+ updated = true
42
+ else
43
+ packet[:data].each do |record|
44
+ current_record = collection.find{|c| c[:id]==record[:id]}
45
+ if current_record
46
+ new_record = current_record.merge(record)
47
+ if new_record != current_record
48
+ current_record.merge!(record)
49
+ updated = true
50
+ end
51
+ else
52
+ collection.push record
53
+ updated = true
54
+ end
55
+ end
56
+ end
57
+
58
+ when 'create'
59
+ record = collection.find{|c| c[:id]==packet[:id]}
60
+ if !record
61
+ @collections[packet[:collection]].push packet[:data]
62
+ updated = true
63
+ end
64
+
65
+ when 'update'
66
+ record = @collections[packet[:collection]].find{|c| c[:id]==packet[:id]}
67
+ if record
68
+ record.merge!(packet[:data])
69
+ updated = true
70
+ end
71
+
72
+ when 'destroy'
73
+ index = @collections[packet[:collection]].find_index{|c| c.id==packet[:id]}
74
+ if index
75
+ @collections[packet[:collection]].delete_at(index)
76
+ updated = true
77
+ end
78
+
79
+ else
80
+ updated = true
81
+ end
82
+
83
+ updated
84
+ end
85
+
86
+ end
87
+ end
88
+ end
@@ -1,3 +1,3 @@
1
1
  module ActionCableNotifications
2
- VERSION = '0.1.18'
2
+ VERSION = '0.1.19'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: action_cable_notifications
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.18
4
+ version: 0.1.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - ByS Sistemas de Control
@@ -116,6 +116,7 @@ files:
116
116
  - lib/action_cable_notifications.rb
117
117
  - lib/action_cable_notifications/channel.rb
118
118
  - lib/action_cable_notifications/channel_actions.rb
119
+ - lib/action_cable_notifications/channel_cache.rb
119
120
  - lib/action_cable_notifications/engine.rb
120
121
  - lib/action_cable_notifications/model.rb
121
122
  - lib/action_cable_notifications/version.rb