Behaviour for pluggable components in the Gno.Commit.Processor pipeline.
The processor calls handle_state/3 on each middleware at every state
transition (see the state machine in Gno.Commit.Processor). The callback
receives the new state atom, the middleware struct, and the processor, and
must return {:ok, processor} (optionally with an updated middleware) or
an error tuple to abort. On failure, rollback/2 is called on all
middlewares that were already invoked.
The :state field on the middleware struct tracks the last state it was
called with, managed automatically by the processor.
Defining a Middleware
Use def_middleware/2 to define a Grax schema subclassing this module:
defmodule MyMiddleware do
use Gno.CommitMiddleware
def_middleware MyNS.MyMiddleware do
property :my_option, type: :string
end
@impl true
def handle_state(:initialized, middleware, processor) do
# custom logic
{:ok, processor}
end
endMiddlewares are configured in the Gno.CommitOperation of a Gno.Service.
See Gno.CommitLogger for a built-in example.
Summary
Functions
Returns the middleware module for the given IRI, or nil if not a middleware.
Checks if the given module is a Gno.CommitMiddleware.
Types
Callbacks
@callback handle_state(state :: atom(), middleware(), Gno.Commit.Processor.t()) :: {:ok, Gno.Commit.Processor.t()} | {:ok, Gno.Commit.Processor.t(), middleware()} | {:error, term()} | {:error, term(), Gno.Commit.Processor.t()}
@callback rollback(middleware(), Gno.Commit.Processor.t()) :: {:ok, Gno.Commit.Processor.t()} | {:ok, Gno.Commit.Processor.t(), middleware()} | {:error, term()} | {:error, term(), Gno.Commit.Processor.t()}
Functions
@spec from(Grax.Schema.t()) :: {:ok, t()} | {:error, any()}
@spec from!(Grax.Schema.t()) :: t()
@spec load( RDF.Graph.t() | RDF.Description.t(), RDF.IRI.coercible() | RDF.BlankNode.t(), opts :: keyword() ) :: {:ok, t()} | {:error, any()}
@spec load!( RDF.Graph.t() | RDF.Description.t(), RDF.IRI.coercible() | RDF.BlankNode.t(), opts :: keyword() ) :: t()
Returns the middleware module for the given IRI, or nil if not a middleware.
Checks if the given module is a Gno.CommitMiddleware.
Example
iex> Gno.CommitMiddleware.type?(Gno.CommitLogger)
true
iex> Gno.CommitMiddleware.type?(Gno.Commit)
false
iex> Gno.CommitMiddleware.type?(NonExisting)
false