Sha256: fcf97af19e79ddd5e65e3aa2417f9df17c0a04b576b82fcbf028a7ac2e5fa958

Contents?: true

Size: 1.45 KB

Versions: 6

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

module CableReady
  # This class is a thread local singleton: CableReady::Channels.instance
  # SEE: https://github.com/socketry/thread-local/tree/master/guides/getting-started
  class Channels
    include Compoundable
    extend Thread::Local

    def initialize
      @channels = {}
    end

    def [](*keys)
      keys.select!(&:itself)
      identifier = keys.many? || (keys.one? && keys.first.respond_to?(:to_global_id)) ? compound(keys) : keys.pop
      @channels[identifier] ||= CableReady::Channel.new(identifier)
    end

    def broadcast(*identifiers, clear: true)
      @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].broadcast(clear: clear) }
          channels.each { |channel| @channels.except!(channel.identifier) if clear }
        end
    end

    def broadcast_to(model, *identifiers, clear: true)
      @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].broadcast_to(model, clear: clear) }
          channels.each { |channel| @channels.except!(channel.identifier) if clear }
        end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
cable_ready-5.0.0.pre9 lib/cable_ready/channels.rb
cable_ready-5.0.0.pre8 lib/cable_ready/channels.rb
cable_ready-5.0.0.pre7 lib/cable_ready/channels.rb
cable_ready-5.0.0.pre6 lib/cable_ready/channels.rb
cable_ready-5.0.0.pre5 lib/cable_ready/channels.rb
cable_ready-5.0.0.pre4 lib/cable_ready/channels.rb