Class: Zizq::Middleware::Chain
- Inherits:
-
Object
- Object
- Zizq::Middleware::Chain
- Defined in:
- lib/zizq/middleware.rb,
sig/generated/zizq/middleware.rbs
Overview
A linked chain of middleware ending with a terminal.
Each middleware must implement #call(arg, chain) where chain is
the next link. The terminal implements #call(arg).
When no middleware is registered, #call delegates directly to the
terminal with zero overhead.
chain = Zizq::Middleware::Chain.new(dispatcher)
chain.use(LoggingMiddleware.new)
chain.use(MetricsMiddleware.new)
chain.call(job)
# MetricsMiddleware -> LoggingMiddleware -> dispatcher
Instance Attribute Summary collapse
-
#terminal ⇒ Object
The terminal callable at the end of the chain.
Instance Method Summary collapse
-
#build ⇒ Object
: () -> untyped.
-
#call(arg) ⇒ Object
Execute the chain with the given argument.
-
#initialize(terminal) ⇒ Chain
constructor
: (untyped) -> void.
-
#use(middleware) ⇒ Object
Append a middleware to the chain.
Constructor Details
#initialize(terminal) ⇒ Chain
: (untyped) -> void
29 30 31 32 33 |
# File 'lib/zizq/middleware.rb', line 29 def initialize(terminal) #: (untyped) -> void @terminal = terminal @entries = [] #: Array[untyped] @built = nil #: untyped? end |
Instance Attribute Details
#terminal ⇒ Object
The terminal callable at the end of the chain.
27 28 29 |
# File 'lib/zizq/middleware.rb', line 27 def terminal @terminal end |
Instance Method Details
#build ⇒ Object
: () -> untyped
54 55 56 57 58 59 60 61 62 |
# File 'lib/zizq/middleware.rb', line 54 def build #: () -> untyped @built ||= if @entries.empty? @terminal else @entries.reverse.reduce(@terminal) do |next_link, mw| Link.new(mw, next_link) end end end |
#call(arg) ⇒ Object
Execute the chain with the given argument.
48 49 50 |
# File 'lib/zizq/middleware.rb', line 48 def call(arg) #: (Arg) -> Ret build.call(arg) end |
#use(middleware) ⇒ Object
Append a middleware to the chain.
42 43 44 45 |
# File 'lib/zizq/middleware.rb', line 42 def use(middleware) #: (untyped) -> void @entries << middleware @built = nil end |