Class: Easyop::Events::Bus::Adapter

Inherits:
Base
  • Object
show all
Defined in:
lib/easyop/events/bus/adapter.rb

Overview

Inheritable base class for custom bus implementations.

Subclass this (not Bus::Base) when building a transport adapter for an external message broker, pub/sub system, or any custom delivery mechanism. Bus::Base defines the required interface and glob helpers. Bus::Adapter adds two production-grade utilities on top:

_safe_invoke(handler, event)   — call a handler, swallow StandardError
_compile_pattern(pattern)      — glob/string → cached Regexp

Both are protected so they are accessible in subclasses but not part of the external bus interface.

Minimum contract

Override #publish and #subscribe. Override #unsubscribe if your transport supports subscription cancellation.

Example — minimal adapter

class PrintBus < Easyop::Events::Bus::Adapter
  def initialize
    super
    @subs  = []
    @mutex = Mutex.new
  end

  def publish(event)
    snap = @mutex.synchronize { @subs.dup }
    snap.each do |sub|
      _safe_invoke(sub[:handler], event) if _pattern_matches?(sub[:pattern], event.name)
    end
  end

  def subscribe(pattern, &block)
    handle = { pattern: _compile_pattern(pattern), handler: block }
    @mutex.synchronize { @subs << handle }
    handle
  end

  def unsubscribe(handle)
    @mutex.synchronize { @subs.delete(handle) }
  end
end

Example — decorator (wraps another bus)

class LoggingBus < Easyop::Events::Bus::Adapter
  def initialize(inner)
    super()
    @inner = inner
  end

  def publish(event)
    Rails.logger.info "[bus:publish] #{event.name} payload=#{event.payload}"
    @inner.publish(event)
  end

  def subscribe(pattern, &block)
    @inner.subscribe(pattern, &block)
  end

  def unsubscribe(handle)
    @inner.unsubscribe(handle)
  end
end

Easyop::Events::Registry.bus = LoggingBus.new(Easyop::Events::Bus::Memory.new)

Method Summary

Methods inherited from Base

#publish, #subscribe, #unsubscribe