Class: CableReady::Channels

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/cable_ready/channels.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeChannels

Returns a new instance of Channels.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cable_ready/channels.rb', line 14

def initialize
  @channels = {}
  @operations = {}
  i[
    add_css_class
    clear_storage
    console_log
    dispatch_event
    inner_html
    insert_adjacent_html
    insert_adjacent_text
    morph
    notification
    outer_html
    push_state
    remove
    remove_attribute
    remove_css_class
    remove_storage_item
    set_attribute
    set_cookie
    set_dataset_property
    set_focus
    set_property
    set_storage_item
    set_style
    set_styles
    set_value
    text_content
  ].each do |operation|
    add_operation operation
  end
end

Instance Attribute Details

#operationsObject

Returns the value of attribute operations.



8
9
10
# File 'lib/cable_ready/channels.rb', line 8

def operations
  @operations
end

Class Method Details

.configure {|CableReady::Channels.instance| ... } ⇒ Object

Yields:



10
11
12
# File 'lib/cable_ready/channels.rb', line 10

def self.configure
  yield CableReady::Channels.instance if block_given?
end

Instance Method Details

#[](identifier) ⇒ Object



52
53
54
# File 'lib/cable_ready/channels.rb', line 52

def [](identifier)
  @channels[identifier] ||= CableReady::Channel.new(identifier, operations)
end

#add_operation(operation, &implementation) ⇒ Object



48
49
50
# File 'lib/cable_ready/channels.rb', line 48

def add_operation(operation, &implementation)
  @operations[operation] = implementation || ->(options = {}) { enqueue_operation(operation, options) }
end

#broadcast(*identifiers, clear: true) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/cable_ready/channels.rb', line 56

def broadcast(*identifiers, clear: true)
  mutex.synchronize do
    @channels.values
      .reject { |channel| identifiers.any? && identifiers.exclude?(channel.identifier) }
      .select { |channel| channel.identifier.is_a?(String) }
      .tap do |channels|
        channels.each { |channel| @channels[channel.identifier].channel_broadcast(clear) }
        channels.each { |channel| @channels.except!(channel.identifier) if clear }
      end
  end
end

#broadcast_to(model, *identifiers, clear: true) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/cable_ready/channels.rb', line 68

def broadcast_to(model, *identifiers, clear: true)
  mutex.synchronize do
    @channels.values
      .reject { |channel| identifiers.any? && identifiers.exclude?(channel.identifier) }
      .reject { |channel| channel.identifier.is_a?(String) }
      .tap do |channels|
        channels.each { |channel| @channels[channel.identifier].channel_broadcast_to(model, clear) }
        channels.each { |channel| @channels.except!(channel.identifier) if clear }
      end
  end
end