Module: DDP::Server::Protocol::Data
- Included in:
- DDP::Server::Protocol
- Defined in:
- lib/ddp/server/protocol/data.rb
Overview
Protocol regarding handling data subscriptions
Defined Under Namespace
Classes: Subscription
Instance Method Summary collapse
- #handle_data ⇒ Object
- #handle_sub(id, name, params) ⇒ Object
- #handle_unsub(id) ⇒ Object
- #send_added(collection, id, fields = nil) ⇒ Object
- #send_added_before(collection, id, fields = nil, before = nil) ⇒ Object
- #send_changed(collection, id, fields = nil, cleared = nil) ⇒ Object
- #send_moved_before(collection, id, before = nil) ⇒ Object
- #send_nosub(id, error = nil) ⇒ Object
- #send_ready(subs) ⇒ Object
- #send_removed(collection, id) ⇒ Object
- #subscription_update(id, old_value, new_value) ⇒ Object
Instance Method Details
#handle_data ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/ddp/server/protocol/data.rb', line 8 def handle_data case @message['msg'] when 'sub' handle_sub(@message['id'], @message['name'], @message['params']) true when 'unsub' handle_unsub(@message['id']) true else false end end |
#handle_sub(id, name, params) ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/ddp/server/protocol/data.rb', line 21 def handle_sub(id, name, params) params ||= [] query = api.collection_query(name, *params) subscription = subscriptions[id] = Subscription.new(self, id, name, query) subscription.async.start send_ready([id]) rescue => e send_error_result(id, e) end |
#handle_unsub(id) ⇒ Object
40 41 42 43 44 |
# File 'lib/ddp/server/protocol/data.rb', line 40 def handle_unsub(id) subscription = @subscriptions.delete(id) subscription.stop unless subscription.nil? send_nosub(id) end |
#send_added(collection, id, fields = nil) ⇒ Object
52 53 54 55 56 |
# File 'lib/ddp/server/protocol/data.rb', line 52 def send_added(collection, id, fields = nil) = { msg: 'added', id: id, collection: collection } .merge!(fields: fields) if fields end |
#send_added_before(collection, id, fields = nil, before = nil) ⇒ Object
73 74 75 76 77 |
# File 'lib/ddp/server/protocol/data.rb', line 73 def send_added_before(collection, id, fields = nil, before = nil) = { msg: 'addedBefore', id: id, collection: collection, before: before } .merge!(fields: fields) if fields end |
#send_changed(collection, id, fields = nil, cleared = nil) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/ddp/server/protocol/data.rb', line 58 def send_changed(collection, id, fields = nil, cleared = nil) = { msg: 'changed', id: id, collection: collection } .merge!(fields: fields) if fields .merge!(cleared: cleared) if cleared end |
#send_moved_before(collection, id, before = nil) ⇒ Object
79 80 81 |
# File 'lib/ddp/server/protocol/data.rb', line 79 def send_moved_before(collection, id, before = nil) msg: 'movedBefore', id: id, collection: collection, before: before end |
#send_nosub(id, error = nil) ⇒ Object
46 47 48 49 50 |
# File 'lib/ddp/server/protocol/data.rb', line 46 def send_nosub(id, error = nil) = { msg: 'nosub', id: id } .merge!(error: error) if error end |
#send_ready(subs) ⇒ Object
69 70 71 |
# File 'lib/ddp/server/protocol/data.rb', line 69 def send_ready(subs) msg: 'ready', subs: subs end |
#send_removed(collection, id) ⇒ Object
65 66 67 |
# File 'lib/ddp/server/protocol/data.rb', line 65 def send_removed(collection, id) msg: 'removed', collection: collection, id: id end |
#subscription_update(id, old_value, new_value) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/ddp/server/protocol/data.rb', line 31 def subscription_update(id, old_value, new_value) subscription_name = @subscriptions[id].name return send_added(subscription_name, new_value['id'], new_value) if old_value.nil? return send_removed(subscription_name, old_value['id']) if new_value.nil? send_changed(subscription_name, old_value['id'], new_value, old_value.keys - new_value.keys) end |