Class: JetstreamBridge::ConsumerMiddleware::MiddlewareChain
- Inherits:
-
Object
- Object
- JetstreamBridge::ConsumerMiddleware::MiddlewareChain
- Defined in:
- lib/jetstream_bridge/consumer/middleware.rb
Overview
Middleware chain for consumer message processing
Allows you to wrap message processing with cross-cutting concerns like logging, error handling, metrics, tracing, etc.
Instance Method Summary collapse
-
#call(event) { ... } ⇒ void
Execute the middleware chain.
-
#initialize ⇒ MiddlewareChain
constructor
A new instance of MiddlewareChain.
-
#use(middleware) ⇒ self
Add a middleware to the chain.
Constructor Details
#initialize ⇒ MiddlewareChain
20 21 22 |
# File 'lib/jetstream_bridge/consumer/middleware.rb', line 20 def initialize @middlewares = [] end |
Instance Method Details
#call(event) { ... } ⇒ void
This method returns an undefined value.
Execute the middleware chain
38 39 40 41 42 43 44 45 |
# File 'lib/jetstream_bridge/consumer/middleware.rb', line 38 def call(event, &final_handler) chain = @middlewares.reverse.reduce(final_handler) do |next_middleware, middleware| lambda do |evt| middleware.call(evt) { next_middleware.call(evt) } end end chain.call(event) end |
#use(middleware) ⇒ self
Add a middleware to the chain
28 29 30 31 |
# File 'lib/jetstream_bridge/consumer/middleware.rb', line 28 def use(middleware) @middlewares << middleware self end |