From 0ce0cf0c04b53ee6c7038d8912dd1ed433f7935f Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Tue, 20 Oct 2015 17:17:18 -0500 Subject: Allow rejecting subscriptions from the channel --- lib/assets/javascripts/cable/connection.coffee | 2 ++ lib/assets/javascripts/cable/subscriptions.coffee | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) (limited to 'lib/assets/javascripts/cable') diff --git a/lib/assets/javascripts/cable/connection.coffee b/lib/assets/javascripts/cable/connection.coffee index 33159130c7..9de3cc0be4 100644 --- a/lib/assets/javascripts/cable/connection.coffee +++ b/lib/assets/javascripts/cable/connection.coffee @@ -58,6 +58,8 @@ class Cable.Connection switch type when Cable.INTERNAL_MESSAGES.SUBSCRIPTION_CONFIRMATION @consumer.subscriptions.notify(identifier, "connected") + when Cable.INTERNAL_MESSAGES.SUBSCRIPTION_REJECTION + @consumer.subscriptions.rejectSubscription(identifier) else @consumer.subscriptions.notify(identifier, "received", message) diff --git a/lib/assets/javascripts/cable/subscriptions.coffee b/lib/assets/javascripts/cable/subscriptions.coffee index 4efb384ee2..497fcb074e 100644 --- a/lib/assets/javascripts/cable/subscriptions.coffee +++ b/lib/assets/javascripts/cable/subscriptions.coffee @@ -27,11 +27,22 @@ class Cable.Subscriptions for subscription in @subscriptions @sendCommand(subscription, "subscribe") + rejectSubscription: (identifier) -> + subscriptions = @findAll(identifier) + + for subscription in subscriptions + @removeSubscription(subscription) + @notify(subscription, "rejected") + remove: (subscription) -> - @subscriptions = (s for s in @subscriptions when s isnt subscription) + @removeSubscription(subscription) + unless @findAll(subscription.identifier).length @sendCommand(subscription, "unsubscribe") + removeSubscription: (subscription) -> + @subscriptions = (s for s in @subscriptions when s isnt subscription) + findAll: (identifier) -> s for s in @subscriptions when s.identifier is identifier @@ -48,7 +59,7 @@ class Cable.Subscriptions for subscription in subscriptions subscription[callbackName]?(args...) - if callbackName in ["initialized", "connected", "disconnected"] + if callbackName in ["initialized", "connected", "disconnected", "rejected"] {identifier} = subscription @record(notification: {identifier, callbackName, args}) -- cgit v1.2.3