Class: AnyCable::Rails::Connection::Subscriptions

Inherits:
ActionCable::Connection::Subscriptions
  • Object
show all
Defined in:
lib/anycable/rails/next/connection.rb

Instance Method Summary collapse

Instance Method Details

#execute_command(data) ⇒ Object

Wrap the original #execute_command to pre-initialize the channel for unsubscribe/message and return true/false to indicate successful/unsuccessful subscription.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/anycable/rails/next/connection.rb', line 73

def execute_command(data)
  cmd = data["command"]

  # We need the current channel identifier in pub/sub
  Current.identifier = data["identifier"]

  load(data["identifier"]) unless cmd == "subscribe"

  super

  return true unless cmd == "subscribe"

  subscription = subscriptions[data["identifier"]]
  !(subscription.nil? || subscription.rejected?)
end

#load(identifier) ⇒ Object

Find or create a channel for a given identifier



98
99
100
101
102
103
104
105
# File 'lib/anycable/rails/next/connection.rb', line 98

def load(identifier)
  return subscriptions[identifier] if subscriptions[identifier]

  subscription = subscription_from_identifier(identifier)
  raise "Channel not found: #{ActiveSupport::JSON.decode(identifier).fetch("channel")}" unless subscription

  subscriptions[identifier] = subscription
end

#restore(subscriptions, istate) ⇒ Object

Restore channels from the list of identifiers and the state



90
91
92
93
94
95
# File 'lib/anycable/rails/next/connection.rb', line 90

def restore(subscriptions, istate)
  subscriptions.each do |id|
    channel = load(id)
    channel.__istate__ = ActiveSupport::JSON.decode(istate[id]) if istate[id]
  end
end