Sha256: 42029798ee2e65231312901421c47297e9b77fd123dfefa90eaa78c9a767f09a

Contents?: true

Size: 1.99 KB

Versions: 3

Compression:

Stored size: 1.99 KB

Contents

require 'messaging/routing/message_matcher'
require 'messaging/routing/route'
require 'messaging/routing/enqueued_route'
require 'messaging/routing/enqueue_message_handler'

module Messaging
  module Routing
    # Public: Sets up routes for the events that matches the given pattern
    #
    # pattern  - Which messages to route. Can be a string, a regexp,
    #            a Message class, a module or anything that responds to call.
    #
    # call:    -  Any object that responds to call.
    #             Will be called immediately for matching messages.
    #
    # enqueue: -  A constant that responds to call.
    #             Will be enqueued with Sidekiq for matching messages.
    #             Needs to be a constant that Sidekiq can serialize to a string
    #             and back again to a constant as you can't store procs in Redis.
    #
    # block    -  An optional block that will be called with each matching message.
    #
    #
    # Examples
    #
    # Messaging.routes.draw do
    #   on 'Events::BidPlaced', call: NotifyOtherBidders
    #
    #   on Events::BidPlaced, enqueue: NotifyOtherBidders
    #
    #   on Events, do |event|
    #     puts event.inspect
    #   end
    #
    #   on /.*Updated$/, enqueue: AuditChanges
    #
    #   on ->(m) { m.topic == 'my-topic' }, call: DoSometing, enqueue: DoSomethingElseWithSidekiq
    # end
    #
    def on(pattern = /.*/, call: nil, enqueue: nil, &block)
      routes << Route.new(pattern, call) if call
      routes << Route.new(pattern, block) if block_given?
      routes << EnqueuedRoute.new(pattern, enqueue) if enqueue
    end

    # Internal: Handles the message with the matching subscribers
    def handle(message)
      routes.map { |route| route.call(message) }
      message
    end

    # Internal: Used by Rails reloading in development.
    def clear_routes!
      @routes = Set.new
    end

    private

    def routes
      @routes ||= Set.new
    end

    def topics
      routes.flat_map(&:topics).map(&:to_s).uniq
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
messaging-3.5.1 lib/messaging/routing.rb
messaging-3.4.3 lib/messaging/routing.rb
messaging-3.4.2 lib/messaging/routing.rb