Module: Pgbus::Streams::BroadcastableOverride

Defined in:
lib/pgbus/streams/broadcastable_override.rb

Overview

Runtime patch for ‘Turbo::Broadcastable` that adds `durable:` kwarg support to synchronous broadcast helpers. Applied at Rails engine boot time when `defined?(::Turbo::Broadcastable)` — see `Pgbus::Engine`.

Instance methods extract ‘durable:` from kwargs and set a thread-local (`Thread.current`) that `TurboBroadcastable#broadcast_stream_to` reads. The thread-local is always cleaned up after the broadcast, even on error.

The ‘_later_to` variants are NOT overridden because turbo-rails enqueues them as background jobs — the thread-local cannot survive into the job execution context. For async broadcasts, use `streams_durable_patterns` or `streams_default_broadcast_mode` config.

Class methods (‘broadcasts_to`, `broadcasts_refreshes_to`) accept `durable:` and store it so the generated callbacks set the thread-local before each broadcast.

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

BROADCAST_METHODS =
%i[
  broadcast_after_to
  broadcast_before_to
  broadcast_replace_to
  broadcast_append_to
  broadcast_prepend_to
  broadcast_update_to
  broadcast_remove_to
  broadcast_refresh_to
  broadcast_render_to
].freeze
BROADCAST_ACTION_METHODS =
%i[
  broadcast_action_to
].freeze

Class Method Summary collapse

Class Method Details

.install!(mod) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/pgbus/streams/broadcastable_override.rb', line 106

def self.install!(mod)
  return if mod.ancestors.include?(self)

  mod.prepend(self)

  # Turbo::Broadcastable uses ActiveSupport::Concern, which extends
  # each including class with Turbo::Broadcastable::ClassMethods.
  # Prepending our ClassMethods onto that nested module ensures any
  # class that includes Turbo::Broadcastable picks up our overrides
  # (broadcasts_to, broadcasts_refreshes_to) automatically.
  if defined?(::Turbo::Broadcastable::ClassMethods) &&
     !::Turbo::Broadcastable::ClassMethods.ancestors.include?(ClassMethods)
    ::Turbo::Broadcastable::ClassMethods.prepend(ClassMethods)
  end
end