Module: Motion::ActionCableExtentions::DeclarativeStreams

Includes:
Synchronization
Included in:
Channel
Defined in:
lib/motion/action_cable_extentions/declarative_streams.rb

Overview

Provides a ‘streaming_from(broadcasts, to:)` API that can be used to declaratively specify what `broadcasts` the channel is interested in receiving and `to` what method they should be routed.

Instance Method Summary collapse

Methods included from Synchronization

#perform_action, #subscribe_to_channel, #synchronize_entrypoint!, #unsubscribe_from_channel

Instance Method Details

#declarative_stream_targetObject



45
46
47
# File 'lib/motion/action_cable_extentions/declarative_streams.rb', line 45

def declarative_stream_target
  @_declarative_stream_target
end

#initializeObject



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/motion/action_cable_extentions/declarative_streams.rb', line 13

def initialize(*)
  super

  # Streams that we are currently interested in
  @_declarative_streams = Set.new

  # The method we are currently routing those streams to
  @_declarative_stream_target = nil

  # Streams that we are setup to listen to. Sadly, there is no public API
  # to stop streaming so this will only grow.
  @_declarative_stream_proxies = Set.new
end

#stop_all_streamsObject

Clean up declarative streams when all streams are stopped.



28
29
30
31
32
33
34
35
# File 'lib/motion/action_cable_extentions/declarative_streams.rb', line 28

def stop_all_streams
  super

  @_declarative_streams.clear
  @_declarative_stream_target = nil

  @_declarative_stream_proxies.clear
end

#streaming_from(broadcasts, to:) ⇒ Object

Declaratively routes provided broadcasts to the provided method.



38
39
40
41
42
43
# File 'lib/motion/action_cable_extentions/declarative_streams.rb', line 38

def streaming_from(broadcasts, to:)
  @_declarative_streams.replace(broadcasts)
  @_declarative_stream_target = to

  @_declarative_streams.each(&method(:_ensure_declarative_stream_proxy))
end