class Discord::Stack(*T)
- Discord::Stack(*T)
- Reference
- Object
Overview
A Stack
is a generic container for a chain of middleware.
A middleware can be any class that implements
def call(payload, context, &block)
.
When #run
is passed a payload, it is passed into the first middleware. If
any middleware in the chain does not yield
, execution of the rest of the
chain will stop.
#run
can also be invoked with a block that also accepts the payload and
Context
, which will be called after all middleware successfully run.
class Middleware
def call(payload, context)
# Make this middleware available in context for later
context.put self
# Do some checks against payload
even = (payload % 2).zero?
# Store some things in context
context.put payload * 2
yield if even
end
end
stack = Stack.new(Middleware.new)
stack.run(2) do |payload, context|
payload # => 2
context[Middleware] # => 4
end
stack.run(1) do |payload, context|
# Code here is never run!
end
NOTE While you can implement Stack
yourself, it is already implemented
internally around the Client
handler methods. The above example is shown
for completeness.
Defined in:
discordcr-middleware/stack.crConstructors
Instance Method Summary
-
#run(payload : U, context : Context = Context.new, index = 0, &block : U, Context -> ) forall U
Runs a message through this middleware stack, with a trailing block
-
#run(payload : U, context : Discord::Context = Context.new, index = 0) forall U
Runs a message through this middleware stack
Constructor Detail
Instance Method Detail
Runs a message through this middleware stack, with a trailing block
Runs a message through this middleware stack