Class: Insika::MiddlewareStack

Inherits:
Object
  • Object
show all
Defined in:
lib/insika/middleware.rb

Overview

Rack-like composition: registration order = execution order (the first is the outermost link). It does NOT rescue (an exception propagates as a turn failure) nor does it have a special halt mechanism — the short-circuit is structural (the link does not call nxt).

Instance Method Summary collapse

Constructor Details

#initialize(middlewares = []) ⇒ MiddlewareStack

Returns a new instance of MiddlewareStack.



25
26
27
# File 'lib/insika/middleware.rb', line 25

def initialize(middlewares = [])
  @middlewares = middlewares
end

Instance Method Details

#<<(middleware) ⇒ Object

Appends a link at the END (innermost). This is the plugin Loader's registration seam ("collections that respond to <<") — it runs at boot, single-fiber, before the server accepts connections, so appending here never races a turn.



33
34
35
36
# File 'lib/insika/middleware.rb', line 33

def <<(middleware)
  @middlewares << middleware
  self
end

#call(state, &terminal) ⇒ Object



38
39
40
41
42
43
# File 'lib/insika/middleware.rb', line 38

def call(state, &terminal)
  chain = @middlewares.reverse.reduce(terminal) do |nxt, mw|
    proc { |s| mw.call(s, &nxt) }
  end
  chain.call(state)
end