Class: GraphQL::Streaming::ActionCableSubscriber
- Inherits:
-
Object
- Object
- GraphQL::Streaming::ActionCableSubscriber
- Defined in:
- lib/graphql/streaming/action_cable_subscriber.rb
Overview
A subscriber for a channel-query combo.
Constant Summary collapse
- CHANNEL_PREFIX =
"graphql_subscription_"
Class Method Summary collapse
-
.trigger(subscription_handle, trigger_options = {}) ⇒ Object
Trigger an event with arguments.
Instance Method Summary collapse
-
#close ⇒ void
Tell this subscriber to stop sending patches.
-
#initialize(channel, query_id) { ... } ⇒ ActionCableSubscriber
constructor
A new instance of ActionCableSubscriber.
-
#register(subscription_handle, arguments) ⇒ Object
Subscribe to event named ‘subscription_handle`, but only when called with arguments `arguments`.
-
#subscribed? ⇒ Boolean
True if this subscriber registered any subscriptions.
Constructor Details
#initialize(channel, query_id) { ... } ⇒ ActionCableSubscriber
Returns a new instance of ActionCableSubscriber.
27 28 29 30 31 32 33 |
# File 'lib/graphql/streaming/action_cable_subscriber.rb', line 27 def initialize(channel, query_id, &query_exec) @channel = channel @query_id = query_id @query_exec = query_exec @subscribed = false @own_streams = [] end |
Class Method Details
.trigger(subscription_handle, trigger_options = {}) ⇒ Object
Trigger an event with arguments
44 45 46 |
# File 'lib/graphql/streaming/action_cable_subscriber.rb', line 44 def self.trigger(subscription_handle, = {}) ActionCable.server.broadcast("#{CHANNEL_PREFIX}#{subscription_handle}", ) end |
Instance Method Details
#close ⇒ void
This method returns an undefined value.
Tell this subscriber to stop sending patches
72 73 74 75 76 77 |
# File 'lib/graphql/streaming/action_cable_subscriber.rb', line 72 def close @channel.stop_specific_streams(@own_streams) @own_streams.clear @subscribed = false nil end |
#register(subscription_handle, arguments) ⇒ Object
Subscribe to event named ‘subscription_handle`, but only when called with arguments `arguments`
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/graphql/streaming/action_cable_subscriber.rb', line 52 def register(subscription_handle, arguments) @subscribed = true handle = "#{CHANNEL_PREFIX}#{subscription_handle}" original_args = stringify_hash(arguments) @own_streams << @channel.stream_from(handle) do |trigger_json| trigger_args = JSON.parse(trigger_json) if original_args == trigger_args reevaluate_query end end end |
#subscribed? ⇒ Boolean
Returns True if this subscriber registered any subscriptions.
66 67 68 |
# File 'lib/graphql/streaming/action_cable_subscriber.rb', line 66 def subscribed? @subscribed end |