From d9d7371c568fe99ef460202ebe7217bfed050e88 Mon Sep 17 00:00:00 2001 From: Javan Makhmali Date: Thu, 25 Jun 2015 11:36:40 -0400 Subject: [PATCH] Assume subscribers have an identifier --- lib/assets/javascripts/cable.js.coffee | 7 ++-- .../javascripts/cable/channel.js.coffee | 6 ++-- .../cable/subscriber_manager.js.coffee | 35 +++++++++++-------- 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/lib/assets/javascripts/cable.js.coffee b/lib/assets/javascripts/cable.js.coffee index 4c93f8f062..86e08e15c0 100644 --- a/lib/assets/javascripts/cable.js.coffee +++ b/lib/assets/javascripts/cable.js.coffee @@ -13,8 +13,5 @@ class @Cable params = if typeof channel is "object" then channel else {channel} new Cable.Channel this, params, mixin - sendMessage: (identifier, data) -> - @sendCommand(identifier, "message", data) - - sendCommand: (identifier, command, data) -> - @connection.send({identifier, command, data}) + send: (data) -> + @connection.send(data) diff --git a/lib/assets/javascripts/cable/channel.js.coffee b/lib/assets/javascripts/cable/channel.js.coffee index bbdc9c5589..9168a76d3c 100644 --- a/lib/assets/javascripts/cable/channel.js.coffee +++ b/lib/assets/javascripts/cable/channel.js.coffee @@ -2,7 +2,7 @@ class Cable.Channel constructor: (@cable, params = {}, mixin) -> @identifier = JSON.stringify(params) extend(this, mixin) - @cable.subscribers.add(@identifier, this) + @cable.subscribers.add(this) # Perform a channel action with the optional data passed as an attribute perform: (action, data = {}) -> @@ -10,10 +10,10 @@ class Cable.Channel @send(data) send: (data) -> - @cable.sendMessage(@identifier, JSON.stringify(data)) + @cable.send(command: "message", identifier: @identifier, data: JSON.stringify(data)) unsubscribe: -> - @cable.subscribers.remove(@identifier) + @cable.subscribers.remove(this) extend = (object, properties) -> if properties? diff --git a/lib/assets/javascripts/cable/subscriber_manager.js.coffee b/lib/assets/javascripts/cable/subscriber_manager.js.coffee index 98223d076c..e2e6c9e228 100644 --- a/lib/assets/javascripts/cable/subscriber_manager.js.coffee +++ b/lib/assets/javascripts/cable/subscriber_manager.js.coffee @@ -2,25 +2,32 @@ class Cable.SubscriberManager constructor: (@cable) -> @subscribers = {} - add: (identifier, subscriber) -> + add: (subscriber) -> + {identifier} = subscriber @subscribers[identifier] = subscriber - if @cable.sendCommand(identifier, "subscribe") - @notify(identifier, "connected") + if @sendCommand("subscribe", identifier) + @notify(subscriber, "connected") reload: -> - for identifier in Object.keys(@subscribers) - if @cable.sendCommand(identifier, "subscribe") - @notify(identifier, "connected") + for identifier, subscriber of @subscribers + if @sendCommand("subscribe", identifier) + @notify(subscriber, "connected") - remove: (identifier) -> - if subscriber = @subscribers[identifier] - @cable.sendCommand(identifier, "unsubscribe") - delete @subscribers[identifier] + remove: (subscriber) -> + {identifier} = subscriber + @sendCommand("unsubscribe", identifier) + delete @subscribers[identifier] notifyAll: (event, args...) -> - for identifier in Object.keys(@subscribers) - @notify(identifier, event, args...) + for identifier, subscriber of @subscribers + @notify(subscriber, event, args...) - notify: (identifier, event, args...) -> - if subscriber = @subscribers[identifier] + notify: (subscriber, event, args...) -> + if typeof subscriber is "string" + subscriber = @subscribers[subscriber] + + if subscriber subscriber[event]?(args...) + + sendCommand: (command, identifier) -> + @cable.send({command, identifier})