Packages
commanded
0.14.0-rc.0
1.4.10
1.4.9
1.4.8
1.4.7
1.4.6
1.4.3
1.4.2
1.4.1
1.4.0
1.4.0-rc.0
1.3.1
1.3.0
1.2.0
1.1.1
1.1.0
1.0.1
1.0.0
1.0.0-rc.1
1.0.0-rc.0
0.19.1
0.19.0
0.18.1
0.18.0
0.17.5
0.17.4
0.17.3
0.17.2
0.17.1
0.17.0
0.16.0
0.16.0-rc.1
0.16.0-rc.0
0.15.1
0.15.0
0.14.0
0.14.0-rc.0
0.13.0
0.12.0
0.11.0
0.10.0
0.9.0
0.8.5
0.8.4
0.8.3
0.8.1
0.8.0
0.7.1
0.6.2
0.6.1
0.6.0
0.4.0
0.3.1
0.3.0
0.2.1
0.2.0
0.1.0
Use Commanded to build your own Elixir applications following the CQRS/ES pattern.
Current section
Files
Jump to
Current section
Files
lib/commanded/middleware/pipeline.ex
defmodule Commanded.Middleware.Pipeline do
@moduledoc """
Pipeline is a struct used as an argument in the callback functions of modules implementing the `Commanded.Middleware` behaviour.
This struct must be returned by each function to be used in the next middleware based on the configured middleware chain.
## Pipeline fields
* `assigns` - shared user data as a map.
* `command` - the command struct being dispatched.
* `consistency` - the requested dispatch consistency, either: `:eventual` (default) or `:strong`
* `identity` - an atom specifying a field in the command containing the
aggregate's identity or a one-arity function that returns
an identity from the command being dispatched.
* `halted` - the boolean status on whether the pipeline was halted
* `response` - set the response to send back to the caller
"""
defstruct [
assigns: %{},
command: nil,
consistency: nil,
identity: nil,
halted: false,
response: nil,
]
alias Commanded.Middleware.Pipeline
@doc """
Puts the `key` with value equal to `value` into `assigns` map
"""
def assign(%Pipeline{assigns: assigns} = pipeline, key, value) when is_atom(key) do
%Pipeline{pipeline | assigns: Map.put(assigns, key, value)}
end
@doc """
Has the pipeline been halted?
"""
def halted?(%Pipeline{halted: halted}), do: halted
@doc """
Halts the pipeline by preventing further middleware downstream from being invoked.
Prevents dispatch of the command if `halt` occurs in a `before_dispatch` callback.
"""
def halt(%Pipeline{} = pipeline) do
%Pipeline{pipeline | halted: true}
end
@doc """
Extract the response from the pipeline, if present, or use the given response
"""
def response(%Pipeline{response: response}), do: response
@doc """
Sets the response to be returned to the dispatch caller
"""
def respond(%Pipeline{response: nil} = pipeline, response) do
%Pipeline{pipeline | response: response}
end
def respond(%Pipeline{} = pipeline, _response), do: pipeline
@doc """
Executes the middleware chain
"""
def chain(pipeline, stage, middleware)
def chain(%Pipeline{} = pipeline, _stage, []), do: pipeline
def chain(%Pipeline{halted: true} = pipeline, :before_dispatch, _middleware), do: pipeline
def chain(%Pipeline{halted: true} = pipeline, :after_dispatch, _middleware), do: pipeline
def chain(%Pipeline{} = pipeline, stage, [module | modules]) do
chain(apply(module, stage, [pipeline]), stage, modules)
end
end