action_cable_notifications 0.1.8 → 0.1.9

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: 5a5b294019da537a20da5de08471a9c7bff2de04
4
- data.tar.gz: 386bb05710c8c1c8857c65a8576895e0bc712359
3
+ metadata.gz: 10d38c1784eed12e550b74daac11de60c16641eb
4
+ data.tar.gz: 82afbcf44adfb536f9900d94a60d669e800e9bfc
5
5
  SHA512:
6
- metadata.gz: 131a9ecbf7b35f060de693e6cb0f71fee8f3a88c5a3f2985d061c6749f363a3c64c03a8e8ed37d45ff32272d85eef42077c0c8b4dcb7943f86adea99eb2efe2c
7
- data.tar.gz: 5166a418c3c85c6b0b3fff2dbdf2b4f50d227c0ba8b2b94b4cfa9bd88f733c1a32da51523090b57951161a7097c653b4316cfd29226297143e9b3d56e9a3f78b
6
+ metadata.gz: 7dd2c4f06d394aaa5d7562b5fb1a542851ff7a6978658aaf43cc5223d25bc11dc36fd573fe0da1e47bbda95b5c047ccf37ae22fcda18eb440c2c5bc6eae30d24
7
+ data.tar.gz: 479f92747da68fbedaf87733f742085f94e87c5bb3c084d3eadb751ddf23e61ae8429bc821fb835ab1bc1a0d9d1604d07fbc982db1e5d16d84162944d3dff597
@@ -10,4 +10,5 @@
10
10
  // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
11
  // about supported directives.
12
12
  //
13
+ //= require lodash
13
14
  //= require_tree .
@@ -0,0 +1,99 @@
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
+ @collections[collection || packet.collection] = packet.data
42
+
43
+ collection_remove: (packet, collection) ->
44
+ console.warn 'Method not implemented: collection_remove '
45
+
46
+ added: (packet, collection) ->
47
+ data = processPacketHelper(packet, collection)
48
+ if data.record
49
+ console.warn 'Expected not to find a document already present for an add: ' + data.record
50
+ else
51
+ data.collection.push(packet.data)
52
+
53
+ changed: (packet, collection) ->
54
+ data = processPacketHelper(packet, collection)
55
+ if !data.record
56
+ console.warn 'Expected to find a document to change'
57
+ else if !_.isEmpty(packet.data)
58
+ _.extend(data.record, packet.data)
59
+
60
+ removed: (packet, collection) ->
61
+ data = processPacketHelper(packet, collection)
62
+ if !data.record
63
+ console.warn 'Expected to find a document to remove'
64
+ else
65
+ data.collection.splice(data.index, 1)
66
+
67
+ #
68
+ # Public methods
69
+ #####################################
70
+ constructor: ->
71
+ @collections = {}
72
+
73
+ # Binds local methods and callbacks to this class
74
+ for name, callback of default_callbacks
75
+ default_callbacks[name] = callback.bind(this)
76
+
77
+ processPacketHelper = processPacketHelper.bind(this)
78
+
79
+ # Registers a new store
80
+ registerStore: (collection, callbacks) ->
81
+ if callbacks
82
+ registered_stores[collection] = callbacks
83
+ else
84
+ registered_stores[collection] = default_callbacks
85
+
86
+ # Initialize registered store
87
+ registered_stores[collection].initialize?(collection)
88
+
89
+ # Dispatch received packet to registered stores
90
+ storePacket: (packet, collection) ->
91
+ if packet && packet.msg
92
+ for store, callbacks of registered_stores
93
+ callbacks[packet.msg]?(packet, collection)
94
+
95
+
96
+ # Export to global namespace
97
+ #######################################
98
+ @App = {} unless @App
99
+ @App.cable_notifications = new CableNotifications()
@@ -0,0 +1 @@
1
+ //= require_tree ./action_cable_notifications
@@ -59,7 +59,7 @@ module ActionCableNotifications
59
59
  if options.present?
60
60
  {
61
61
  collection: self.model_name.collection,
62
- msg: 'add_collection',
62
+ msg: 'collection_add',
63
63
  data: self.scoped_collection(options[:scope])
64
64
  }
65
65
  end
@@ -1,3 +1,4 @@
1
+ require 'lodash-rails'
1
2
  require 'action_cable_notifications/callbacks.rb'
2
3
  require 'action_cable_notifications/streams.rb'
3
4
 
@@ -1,3 +1,3 @@
1
1
  module ActionCableNotifications
2
- VERSION = '0.1.8'
2
+ VERSION = '0.1.9'
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.8
4
+ version: 0.1.9
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-04 00:00:00.000000000 Z
11
+ date: 2016-09-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -30,6 +30,20 @@ dependencies:
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
32
  version: 5.0.0.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: lodash-rails
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: 4.15.0
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: 4.15.0
33
47
  - !ruby/object:Gem::Dependency
34
48
  name: sqlite3
35
49
  requirement: !ruby/object:Gem::Requirement
@@ -56,7 +70,9 @@ files:
56
70
  - README.md
57
71
  - Rakefile
58
72
  - app/assets/config/action_cable_notifications_manifest.js
73
+ - app/assets/javascripts/action_cable_notifications.js
59
74
  - app/assets/javascripts/action_cable_notifications/application.js
75
+ - app/assets/javascripts/action_cable_notifications/sync_to_store.coffee
60
76
  - app/assets/stylesheets/action_cable_notifications/application.css
61
77
  - app/controllers/action_cable_notifications/application_controller.rb
62
78
  - app/helpers/action_cable_notifications/application_helper.rb