action_cable_notifications 0.1.19 → 0.1.24

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1dab7586f3ac550c6ac8b849b658c9b6298b13d4
4
- data.tar.gz: 445cf42e5076f055d9c72cb83c71eb56a4fb87fe
3
+ metadata.gz: 778924ed40589e3d7653fa0fa7c7cc7409fbb115
4
+ data.tar.gz: dfa866c25e56b55845c2474167742fb4a76592d2
5
5
  SHA512:
6
- metadata.gz: e98437193f244b7b372fc090307cc6b386864d1b6adf7d3b32a9999ddaa7906c57a0b9dae3b15ba9e4ba2394fde4e695fb724a0f1bcb5bbfdbb8b6b7deeb1d62
7
- data.tar.gz: eff1938cc3c84ca6fc76633109bc8e13d13de759956db8c89930c590752a099e1027c32c69204bccad5154c0d3e5857f67bcb03be5cf886bd7a7cb7d6e6ae1b4
6
+ metadata.gz: 89bfc86631704c2b812b28f54a0f2238495b7512dc65e3a2432952b62738b4a3c34b348d8f5a286d0d9fdc3d7997f349d19014dee568fe1b8e93955860c0b9fb
7
+ data.tar.gz: 585982ecbff391e1edb27a5ebf72cd2d4dfb0c0333931bfa236cac377f494dcea5ea593d4d0d21f2da627ce7a836a4da84d427ecb88955d25331276fbb3e16c7
@@ -21,7 +21,7 @@ class CableNotifications.Collection
21
21
  connectionChanged = () ->
22
22
  if @channel.isConnected()
23
23
  _.each(@commandsCache, (cmd) ->
24
- if upstream(cmd.command, cmd.params)
24
+ if upstream.call(this, cmd.command, cmd.params)
25
25
  cmd.performed = true
26
26
  )
27
27
 
@@ -31,10 +31,11 @@ class CableNotifications.Collection
31
31
  # Fetch data from upstream server when connection is resumed
32
32
  @fetch()
33
33
 
34
+ @callbacks?.connectionChanged?.call(this)
35
+
34
36
  # Public methods
35
37
  #######################################
36
-
37
- constructor: (@store, @name, @tableName) ->
38
+ constructor: (@store, @name, @tableName, @callbacks) ->
38
39
  # Data storage array
39
40
  @data = []
40
41
  # Channel used to sync with upstream collection
@@ -46,9 +47,7 @@ class CableNotifications.Collection
46
47
  # Stores records that needs to be tracked when inserted into the collection
47
48
  @trackedRecords = []
48
49
 
49
- # Bind private methods to class instance
50
- ########################################################
51
- upstream = upstream.bind(this)
50
+ @callbacks?.initialize?.call(this)
52
51
 
53
52
  # Sync collection to ActionCable Channel
54
53
  syncToChannel: (@channel) ->
@@ -60,11 +59,14 @@ class CableNotifications.Collection
60
59
 
61
60
  # Fetch records from upstream
62
61
  fetch: (params) ->
63
- upstream("fetch", params)
62
+ upstream.call(this, "fetch", params)
64
63
 
65
64
  # Filter records from the current collection
66
65
  where: (selector={}) ->
67
- _.filter(@data, selector)
66
+ if @callbacks?.where?
67
+ @callbacks.where.call(this, selector)
68
+ else
69
+ _.filter(@data, selector)
68
70
 
69
71
  # Find a record
70
72
  find: (selector={}, options={}) ->
@@ -76,7 +78,7 @@ class CableNotifications.Collection
76
78
  if trackedRecord
77
79
  trackedRecord
78
80
  else
79
- trackedRecord = {id: selector.id}
81
+ trackedRecord = selector
80
82
  @trackedRecords.push trackedRecord
81
83
  trackedRecord
82
84
  else
@@ -94,12 +96,14 @@ class CableNotifications.Collection
94
96
  # Search in tracked records
95
97
  recordIndex = _.findIndex(@trackedRecords, {id: fields.id})
96
98
  if recordIndex>=0
97
- fields = _.merge( @trackedRecords[recordIndex], fields )
99
+ fields = _.extend( @trackedRecords[recordIndex], fields )
98
100
  @trackedRecords.splice(recordIndex, 1)
99
101
 
100
- @data.push (fields) unless @sync
102
+ if !@sync
103
+ @data.push (fields)
104
+ @callbacks?.create?.call(this, fields)
101
105
 
102
- upstream("create", {fields: fields})
106
+ upstream.call(this, "create", {fields: fields})
103
107
  fields
104
108
 
105
109
  # Update an existing record
@@ -111,8 +115,11 @@ class CableNotifications.Collection
111
115
  else
112
116
  console.warn("[update] Couldn't find a matching record:", selector)
113
117
  else
114
- _.extend(record, fields)
115
- upstream("update", {id: record.id, fields: fields})
118
+ if !@sync
119
+ @callbacks?.update?.call(this, selector, fields, options)
120
+ _.extend(record, fields)
121
+
122
+ upstream.call(this, "update", {id: record.id, fields: fields})
116
123
  record
117
124
 
118
125
  # Update an existing record or inserts a new one if there is no match
@@ -126,6 +133,9 @@ class CableNotifications.Collection
126
133
  console.warn("[destroy] Couldn't find a matching record:", selector)
127
134
  else
128
135
  record = @data[index]
129
- @data.splice(index, 1) unless @sync
130
- upstream("destroy", {id: record.id})
136
+ if !@sync
137
+ @data.splice(index, 1)
138
+ @callbacks.destroy.call(this, selector)
139
+
140
+ upstream.call(this, "destroy", {id: record.id})
131
141
  record
@@ -26,4 +26,4 @@ class CableNotifications.Store.DefaultCallbacks
26
26
  collection.destroy({id: packet.id})
27
27
 
28
28
  error: (packet, collection) ->
29
- console.error "[#{packet.cmd}]: #{packet.error}"
29
+ console.error "[#{packet.command}]: #{packet.error}"
@@ -56,12 +56,12 @@ class CableNotifications.Store
56
56
  #######################################
57
57
 
58
58
  # Register a new collection
59
- registerCollection: (name, channel, tableName) ->
59
+ registerCollection: (name, channel, tableName, actions) ->
60
60
  tableName = name unless tableName
61
61
  if @collections[name]
62
62
  console.warn "[registerCollection]: Collection '#{name}' already exists"
63
63
  else
64
- @collections[name] = new CableNotifications.Collection(this, name, tableName)
64
+ @collections[name] = new CableNotifications.Collection(this, name, tableName, actions)
65
65
  if channel
66
66
  @syncToChannel(channel, @collections[name])
67
67
 
@@ -84,18 +84,25 @@ class CableNotifications.Store
84
84
  console.warn "[syncToChannel]: Channel specified doesn't have an identifier"
85
85
  return false
86
86
 
87
- if @collections[collection.name] < 0
87
+ if !@collections[collection.name]
88
88
  console.warn "[syncToChannel]: Collection does not exists in the store"
89
89
  return false
90
90
 
91
+ if collection.channel == channel
92
+ console.warn "[syncToChannel]: Collection is already been synced with channel '#{channelId}'"
93
+ return false
94
+
95
+ channel.collections = [] unless channel.collections
96
+
97
+ existingCollection = _.find(channel.collections, {tableName: collection.tableName})
98
+ if existingCollection
99
+ console.warn "[syncToChannel]: Table '#{collection.tableName}' is already being synced with channel '#{channelId}' in collection '#{existingCollection.name}'"
100
+
101
+ # Copies data from existing collection to the new collection
102
+ collection.data = _.cloneDeep(existingCollection.data)
103
+
91
104
  if @channels[channelId]
92
- channelInfo = @channels[channelId]
93
- if _.find(channelInfo.collections, {name: collection.name})
94
- console.warn "[syncToChannel]: Collection '#{collection.name}' is already being synced with channel '#{channelId}'"
95
- return false
96
- else
97
- collection.syncToChannel(channel)
98
- channelInfo.collections.push collection
105
+ @channels[channelId].collections.push collection
99
106
  else
100
107
  # Initialize channelInfo
101
108
  @channels[channelId] =
@@ -114,7 +121,8 @@ class CableNotifications.Store
114
121
  channel.disconnected = channelDisconnected(@channels[channelId]).bind(this)
115
122
  channel.isConnected = channelIsConnected(@channels[channelId]).bind(this)
116
123
 
117
- # Assigns channel to collection and turns on Sync
118
- collection.syncToChannel(channel)
124
+ # Assigns channel to collection and turns on Sync
125
+ collection.syncToChannel(channel)
126
+ channel.collections.push collection
119
127
 
120
128
  return true
@@ -29,25 +29,38 @@ module ActionCableNotifications
29
29
  data.deep_symbolize_keys!
30
30
 
31
31
  channel_options = self.ActionCableNotifications[data[:collection]]
32
- model = channel_options[:model]
33
- broadcasting = channel_options[:broadcasting]
34
- model_options = model.ActionCableNotificationsOptions[broadcasting]
35
-
36
- params = {
37
- model: model,
38
- model_options: model_options,
39
- params: data[:params]
40
- }
41
-
42
- case data[:command]
43
- when "fetch"
44
- fetch(params)
45
- when "create"
46
- create(params)
47
- when "update"
48
- update(params)
49
- when "destroy"
50
- destroy(params)
32
+ if channel_options
33
+ model = channel_options[:model]
34
+ broadcasting = channel_options[:broadcasting]
35
+ model_options = model.ActionCableNotificationsOptions[broadcasting]
36
+
37
+ params = {
38
+ model: model,
39
+ model_options: model_options,
40
+ params: data[:params],
41
+ command: data[:command]
42
+ }
43
+
44
+ case data[:command]
45
+ when "fetch"
46
+ fetch(params)
47
+ when "create"
48
+ create(params)
49
+ when "update"
50
+ update(params)
51
+ when "destroy"
52
+ destroy(params)
53
+ end
54
+ else
55
+ response = {
56
+ collection: data[:collection],
57
+ msg: 'error',
58
+ command: data[:command],
59
+ error: "Collection '#{data[:collection]}' does not exist."
60
+ }
61
+
62
+ # Send error notification to the client
63
+ transmit response
51
64
  end
52
65
  end
53
66
 
@@ -83,15 +96,16 @@ module ActionCableNotifications
83
96
  def stream_notifications_for(model, options = {})
84
97
  # Default options
85
98
  options = {
99
+ broadcasting: model.model_name.collection
86
100
  }.merge(options)
87
101
 
88
102
  # These options cannot be overridden
89
103
  options[:model] = model
90
104
  options[:channel] = self
91
- options[:broadcasting] = model.model_name.collection
105
+ model_name = model.model_name.collection
92
106
 
93
107
  # Sets channel options
94
- self.ActionCableNotifications[options[:broadcasting]] = options
108
+ self.ActionCableNotifications[model_name] = options
95
109
 
96
110
  # Checks if model already includes notification callbacks
97
111
  if !model.respond_to? :ActionCableNotificationsOptions
@@ -57,7 +57,7 @@ module ActionCableNotifications
57
57
  response = {
58
58
  collection: data[:model].model_name.collection,
59
59
  msg: 'error',
60
- cmd: 'create',
60
+ command: data[:command],
61
61
  error: error || record.errors.full_messages
62
62
  }
63
63
 
@@ -94,7 +94,7 @@ module ActionCableNotifications
94
94
  response = {
95
95
  collection: data[:model].model_name.collection,
96
96
  msg: 'error',
97
- cmd: 'update',
97
+ command: data[:command],
98
98
  error: error || record.errors.full_messages
99
99
  }
100
100
 
@@ -129,7 +129,7 @@ module ActionCableNotifications
129
129
  if error
130
130
  response = { collection: data[:model].model_name.collection,
131
131
  msg: 'error',
132
- cmd: 'destroy',
132
+ command: data[:command],
133
133
  error: error || record.errors.full_messages
134
134
  }
135
135
 
@@ -1,3 +1,3 @@
1
1
  module ActionCableNotifications
2
- VERSION = '0.1.19'
2
+ VERSION = '0.1.24'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: action_cable_notifications
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.19
4
+ version: 0.1.24
5
5
  platform: ruby
6
6
  authors:
7
7
  - ByS Sistemas de Control
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-03 00:00:00.000000000 Z
11
+ date: 2016-10-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails