action_cable_notifications 0.1.19 → 0.1.24
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 +4 -4
- data/app/assets/javascripts/action_cable_notifications/collection.coffee +26 -16
- data/app/assets/javascripts/action_cable_notifications/default_callbacks.coffee +1 -1
- data/app/assets/javascripts/action_cable_notifications/store.coffee +20 -12
- data/lib/action_cable_notifications/channel.rb +35 -21
- data/lib/action_cable_notifications/channel_actions.rb +3 -3
- data/lib/action_cable_notifications/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 778924ed40589e3d7653fa0fa7c7cc7409fbb115
|
4
|
+
data.tar.gz: dfa866c25e56b55845c2474167742fb4a76592d2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
-
|
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 =
|
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 = _.
|
99
|
+
fields = _.extend( @trackedRecords[recordIndex], fields )
|
98
100
|
@trackedRecords.splice(recordIndex, 1)
|
99
101
|
|
100
|
-
|
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
|
-
|
115
|
-
|
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
|
-
|
130
|
-
|
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
|
@@ -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
|
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
|
-
|
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
|
-
|
118
|
-
|
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
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
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
|
-
|
105
|
+
model_name = model.model_name.collection
|
92
106
|
|
93
107
|
# Sets channel options
|
94
|
-
self.ActionCableNotifications[
|
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
|
-
|
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
|
-
|
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
|
-
|
132
|
+
command: data[:command],
|
133
133
|
error: error || record.errors.full_messages
|
134
134
|
}
|
135
135
|
|
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.
|
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-
|
11
|
+
date: 2016-10-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|