action_cable_notifications 0.1.10 → 0.1.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/assets/javascripts/action_cable_notifications/application.js +1 -1
- data/app/assets/javascripts/action_cable_notifications/cable_notifications.coffee +13 -0
- data/app/assets/javascripts/action_cable_notifications/collection.coffee +40 -0
- data/app/assets/javascripts/action_cable_notifications/default_callbacks.coffee +62 -0
- data/app/assets/javascripts/action_cable_notifications/store.coffee +54 -0
- data/app/assets/javascripts/action_cable_notifications.js +1 -1
- data/lib/action_cable_notifications/callbacks.rb +1 -1
- data/lib/action_cable_notifications/streams.rb +10 -1
- data/lib/action_cable_notifications/version.rb +1 -1
- metadata +6 -3
- data/app/assets/javascripts/action_cable_notifications/sync_to_store.coffee +0 -107
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6f1c452c20e07a93aa050d60b61d6a5c2fdba4a1
|
4
|
+
data.tar.gz: da28fc564086ef82612b56f209bd365df1d80127
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a769b28967a738637247207f4113cbebe510a2526a44099daed32be698e2207ce93b7b566b46f9344dab5092a62207bbb4f17f54246b9dc7e01b416388489cff
|
7
|
+
data.tar.gz: 50f9b8030b96f4aeaf82a7a5f8b030c627c577525128e9b8e6697425b883adab7585591c3879cce64a5498a2d99bbf1c14483f6254274a69fa604159922be118
|
@@ -0,0 +1,13 @@
|
|
1
|
+
#= export CableNotifications
|
2
|
+
#= require_self
|
3
|
+
#= require './store'
|
4
|
+
#= require './collection'
|
5
|
+
|
6
|
+
@CableNotifications =
|
7
|
+
stores: []
|
8
|
+
|
9
|
+
# Register a new store
|
10
|
+
registerStore: (store, callbacks) ->
|
11
|
+
new_store = new CableNotifications.Store(store, callbacks)
|
12
|
+
@stores.push new_store
|
13
|
+
new_store
|
@@ -0,0 +1,40 @@
|
|
1
|
+
class CableNotifications.Collection
|
2
|
+
constructor: (@store, @name) ->
|
3
|
+
@data = []
|
4
|
+
@channelInfo = null
|
5
|
+
|
6
|
+
# Private methods
|
7
|
+
#######################################
|
8
|
+
|
9
|
+
find: (selector={}) ->
|
10
|
+
_.find(@data, selector)
|
11
|
+
|
12
|
+
findIndex: (selector={}) ->
|
13
|
+
_.findIndex(@data, selector)
|
14
|
+
|
15
|
+
insert: (record) ->
|
16
|
+
@data.push (record)
|
17
|
+
|
18
|
+
remove: (selector={}) ->
|
19
|
+
index = @findIndex(selector)
|
20
|
+
if index < 0
|
21
|
+
console.warn("Couldn't find a matching record: #{selector}")
|
22
|
+
else
|
23
|
+
record = @data.splice(index, 1)
|
24
|
+
|
25
|
+
update: (selector={}, fields, options) ->
|
26
|
+
record = @find(selector)
|
27
|
+
if !record
|
28
|
+
if options.upsert
|
29
|
+
@insert(fields)
|
30
|
+
else
|
31
|
+
console.warn("Couldn't find a matching record: #{selector}")
|
32
|
+
else
|
33
|
+
_.extend(record, fields)
|
34
|
+
|
35
|
+
# http://docs.meteor.com/#/full/upsert
|
36
|
+
upsert: (selector={}, fields) ->
|
37
|
+
@update(selector, fields, {upsert: true})
|
38
|
+
|
39
|
+
filter: (selector={}) ->
|
40
|
+
_.filter(@data, selector)
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# Default callbacks for internal storage of received packets
|
2
|
+
class CableNotifications.Store.DefaultCallbacks
|
3
|
+
constructor: (@collections) ->
|
4
|
+
|
5
|
+
# Helper function
|
6
|
+
processPacketHelper = (packet, collection) ->
|
7
|
+
index = -1
|
8
|
+
record = null
|
9
|
+
|
10
|
+
local_collection = @collections[collection || packet.collection].data
|
11
|
+
if !local_collection
|
12
|
+
console.warn("[update_many]: Collection #{collection_name} doesn't exist")
|
13
|
+
else
|
14
|
+
index = _.find(local_collection, (record) -> record.id == packet.id)
|
15
|
+
if (index >= 0)
|
16
|
+
record = local_collection[index]
|
17
|
+
|
18
|
+
return {
|
19
|
+
collection: local_collection
|
20
|
+
index: index
|
21
|
+
record: record
|
22
|
+
}
|
23
|
+
|
24
|
+
# Callbacks
|
25
|
+
##################################################
|
26
|
+
|
27
|
+
collection_remove: (packet, collection) ->
|
28
|
+
console.warn 'Method not implemented: collection_remove '
|
29
|
+
|
30
|
+
create: (packet, collection) ->
|
31
|
+
data = processPacketHelper(packet, collection)
|
32
|
+
if data.record
|
33
|
+
console.warn 'Expected not to find a document already present for an add: ' + data.record
|
34
|
+
else
|
35
|
+
data.collection.push(packet.data)
|
36
|
+
|
37
|
+
update: (packet, collection) ->
|
38
|
+
data = processPacketHelper(packet, collection)
|
39
|
+
if !data.record
|
40
|
+
console.warn 'Expected to find a document to change'
|
41
|
+
else if !_.isEmpty(packet.data)
|
42
|
+
_.extend(data.record, packet.data)
|
43
|
+
|
44
|
+
update_many: (packet, collection) ->
|
45
|
+
collection_name = collection || packet.collection
|
46
|
+
local_collection = @collections[collection_name].data
|
47
|
+
if !local_collection
|
48
|
+
console.warn("[update_many]: Collection #{collection_name} doesn't exist")
|
49
|
+
else
|
50
|
+
_.each packet.data, (fields) ->
|
51
|
+
record = _.findIndex(local_collection, (r) -> r.id == fields.id)
|
52
|
+
if record>=0
|
53
|
+
_.extend(local_collection[record], fields)
|
54
|
+
else
|
55
|
+
local_collection.push(fields)
|
56
|
+
|
57
|
+
destroy: (packet, collection) ->
|
58
|
+
data = processPacketHelper(packet, collection)
|
59
|
+
if !data.record
|
60
|
+
console.warn 'Expected to find a document to remove'
|
61
|
+
else
|
62
|
+
data.collection.splice(data.index, 1)
|
@@ -0,0 +1,54 @@
|
|
1
|
+
#= require_self
|
2
|
+
#= require './default_callbacks'
|
3
|
+
|
4
|
+
class CableNotifications.Store
|
5
|
+
constructor: (@name, @callbacks) ->
|
6
|
+
@collections = {}
|
7
|
+
@channels = {}
|
8
|
+
|
9
|
+
if !@callbacks
|
10
|
+
@callbacks = new CableNotifications.Store.DefaultCallbacks(@collections)
|
11
|
+
|
12
|
+
this
|
13
|
+
|
14
|
+
# Private methods
|
15
|
+
#######################################
|
16
|
+
|
17
|
+
packetReceived = (channelInfo, collection) ->
|
18
|
+
(packet) ->
|
19
|
+
@storePacket(packet, collection)
|
20
|
+
channelInfo.callbacks.received?.apply channelInfo.channel, arguments
|
21
|
+
|
22
|
+
# Public methods
|
23
|
+
#######################################
|
24
|
+
|
25
|
+
# Register a new collection
|
26
|
+
registerCollection: (collection) ->
|
27
|
+
if @collections[collection]
|
28
|
+
console.warn '[registerCollection]: Collection already exists'
|
29
|
+
else
|
30
|
+
@collections[collection] = new CableNotifications.Collection(this, collection)
|
31
|
+
@collections[collection]
|
32
|
+
|
33
|
+
# Sync store using ActionCable received events
|
34
|
+
# collection parameter overrides the collection name specified in the incoming packets for this channel
|
35
|
+
syncToChannel: (channel, collection) ->
|
36
|
+
channelId = JSON.parse(channel.identifier)?.channel
|
37
|
+
|
38
|
+
if !channelId
|
39
|
+
console.warn "[syncToChannel]: Channel specified doesn't have an identifier"
|
40
|
+
else
|
41
|
+
@channels[channelId] =
|
42
|
+
channel: channel
|
43
|
+
callbacks: {
|
44
|
+
received: channel.received
|
45
|
+
}
|
46
|
+
|
47
|
+
channel.received = packetReceived(@channels[channelId], collection).bind(this)
|
48
|
+
channel
|
49
|
+
|
50
|
+
# Dispatch received packet to registered stores
|
51
|
+
# collection overrides the collection name specified in the incoming packet
|
52
|
+
storePacket: (packet, collection) ->
|
53
|
+
if packet && packet.msg
|
54
|
+
@callbacks[packet.msg]?(packet, collection)
|
@@ -1 +1 @@
|
|
1
|
-
//=
|
1
|
+
//= require ./action_cable_notifications/application
|
@@ -3,14 +3,20 @@ module ActionCableNotifications
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
included do
|
6
|
+
attr_accessor :stream_notification_options
|
6
7
|
# Actions to be done when the module is included
|
7
8
|
end
|
8
9
|
|
10
|
+
def get_initial_values
|
11
|
+
transmit self.stream_notification_options[:model].notify_initial self.stream_notification_options[:broadcasting]
|
12
|
+
end
|
13
|
+
|
9
14
|
private
|
10
15
|
|
11
16
|
def stream_notifications_for(model, options = {}, &block)
|
12
17
|
# Default options
|
13
18
|
options = {
|
19
|
+
model: model,
|
14
20
|
actions: [:create, :update, :destroy],
|
15
21
|
broadcasting: model.model_name.collection,
|
16
22
|
callback: nil,
|
@@ -20,6 +26,9 @@ module ActionCableNotifications
|
|
20
26
|
scope: :all # Default collection scope
|
21
27
|
}.merge(options)
|
22
28
|
|
29
|
+
# Sets channel options
|
30
|
+
self.stream_notification_options = options
|
31
|
+
|
23
32
|
# Checks if model already includes notification callbacks
|
24
33
|
if !model.respond_to? :ActionCableNotificationsOptions
|
25
34
|
model.send('include', ActionCableNotifications::Callbacks)
|
@@ -37,7 +46,7 @@ module ActionCableNotifications
|
|
37
46
|
# Transmit initial state if required
|
38
47
|
if options[:include_initial]
|
39
48
|
# XXX: Check if data should be transmitted
|
40
|
-
|
49
|
+
get_initial_values
|
41
50
|
end
|
42
51
|
|
43
52
|
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.
|
4
|
+
version: 0.1.14
|
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-09-
|
11
|
+
date: 2016-09-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -72,7 +72,10 @@ files:
|
|
72
72
|
- app/assets/config/action_cable_notifications_manifest.js
|
73
73
|
- app/assets/javascripts/action_cable_notifications.js
|
74
74
|
- app/assets/javascripts/action_cable_notifications/application.js
|
75
|
-
- app/assets/javascripts/action_cable_notifications/
|
75
|
+
- app/assets/javascripts/action_cable_notifications/cable_notifications.coffee
|
76
|
+
- app/assets/javascripts/action_cable_notifications/collection.coffee
|
77
|
+
- app/assets/javascripts/action_cable_notifications/default_callbacks.coffee
|
78
|
+
- app/assets/javascripts/action_cable_notifications/store.coffee
|
76
79
|
- app/assets/stylesheets/action_cable_notifications/application.css
|
77
80
|
- app/controllers/action_cable_notifications/application_controller.rb
|
78
81
|
- app/helpers/action_cable_notifications/application_helper.rb
|
@@ -1,107 +0,0 @@
|
|
1
|
-
class CableNotifications
|
2
|
-
|
3
|
-
#
|
4
|
-
# Private variables
|
5
|
-
#####################################
|
6
|
-
|
7
|
-
registered_stores = {}
|
8
|
-
|
9
|
-
#
|
10
|
-
# Public variables
|
11
|
-
#####################################
|
12
|
-
|
13
|
-
collections: null
|
14
|
-
|
15
|
-
#
|
16
|
-
# Private methods
|
17
|
-
#####################################
|
18
|
-
|
19
|
-
processPacketHelper = (packet, collection) ->
|
20
|
-
local_collection = @collections[collection || packet.collection]
|
21
|
-
index = null
|
22
|
-
record = null
|
23
|
-
|
24
|
-
index = _.findIndex(local_collection, (record) -> record.id == packet.id)
|
25
|
-
if (index >= 0)
|
26
|
-
record = local_collection[index]
|
27
|
-
|
28
|
-
return {
|
29
|
-
collection: local_collection
|
30
|
-
index: index
|
31
|
-
record: record
|
32
|
-
}
|
33
|
-
|
34
|
-
# Default callbacks for internal storage of received packets
|
35
|
-
# Need to set include_initial: true in broadcasting options
|
36
|
-
default_callbacks =
|
37
|
-
initialize: (collection) ->
|
38
|
-
@collections[collection] = []
|
39
|
-
|
40
|
-
collection_add: (packet, collection) ->
|
41
|
-
for index, row of packet.data
|
42
|
-
@collections[collection || packet.collection].push( row )
|
43
|
-
|
44
|
-
collection_remove: (packet, collection) ->
|
45
|
-
console.warn 'Method not implemented: collection_remove '
|
46
|
-
|
47
|
-
create: (packet, collection) ->
|
48
|
-
data = processPacketHelper(packet, collection)
|
49
|
-
if data.record
|
50
|
-
console.warn 'Expected not to find a document already present for an add: ' + data.record
|
51
|
-
else
|
52
|
-
data.collection.push(packet.data)
|
53
|
-
|
54
|
-
update: (packet, collection) ->
|
55
|
-
data = processPacketHelper(packet, collection)
|
56
|
-
if !data.record
|
57
|
-
console.warn 'Expected to find a document to change'
|
58
|
-
else if !_.isEmpty(packet.data)
|
59
|
-
_.extend(data.record, packet.data)
|
60
|
-
|
61
|
-
update_many: (packet, collection) ->
|
62
|
-
local_collection = @collections[collection || packet.collection]
|
63
|
-
_.each packet.data, (fields) ->
|
64
|
-
record = _.findIndex(local_collection, (r) -> r.id == fields.id)
|
65
|
-
if record
|
66
|
-
_.extend(local_collection[index], fields)
|
67
|
-
|
68
|
-
destroy: (packet, collection) ->
|
69
|
-
data = processPacketHelper(packet, collection)
|
70
|
-
if !data.record
|
71
|
-
console.warn 'Expected to find a document to remove'
|
72
|
-
else
|
73
|
-
data.collection.splice(data.index, 1)
|
74
|
-
|
75
|
-
#
|
76
|
-
# Public methods
|
77
|
-
#####################################
|
78
|
-
constructor: ->
|
79
|
-
@collections = {}
|
80
|
-
|
81
|
-
# Binds local methods and callbacks to this class
|
82
|
-
for name, callback of default_callbacks
|
83
|
-
default_callbacks[name] = callback.bind(this)
|
84
|
-
|
85
|
-
processPacketHelper = processPacketHelper.bind(this)
|
86
|
-
|
87
|
-
# Registers a new store
|
88
|
-
registerStore: (collection, callbacks) ->
|
89
|
-
if callbacks
|
90
|
-
registered_stores[collection] = callbacks
|
91
|
-
else
|
92
|
-
registered_stores[collection] = default_callbacks
|
93
|
-
|
94
|
-
# Initialize registered store
|
95
|
-
registered_stores[collection].initialize?(collection)
|
96
|
-
|
97
|
-
# Dispatch received packet to registered stores
|
98
|
-
storePacket: (packet, collection) ->
|
99
|
-
if packet && packet.msg
|
100
|
-
for store, callbacks of registered_stores
|
101
|
-
callbacks[packet.msg]?(packet, collection)
|
102
|
-
|
103
|
-
|
104
|
-
# Export to global namespace
|
105
|
-
#######################################
|
106
|
-
@App = {} unless @App
|
107
|
-
@App.cable_notifications = new CableNotifications()
|