middleware 0.1.0

Middleware pattern


To use this package, put the following dependency into your project's dependencies section:

dub.json
dub.sdl

Middleware

Wrap an action so you can run other actions both before and after that action.

Heavily inspired by this ruby gem.

Installation

Add "middleware": "~>0.1.0" to your dub dependencies.

Usage

Annotated example:

import middleware; // Import the Middleware libraries

// Create a Middleware. Must inherit from Middleware
class Trace : Middleware
{
    private string message_;

    this(string message)
    {
        message_ = message;
    }

    // You must override this.
    // MWArgs is a Variant[string]. You simply pass arguments to this
    // when running the Stack.
    override void opCall(MWArgs args)
    {
        import std.stdio : writefln;

        // Before the next middleware...
        writefln!"--> %s"(message_);

        // Call the next middleware. If you don't, the chain stops.
        // The next_ property is from the abstract Middleware superclass, and
        // it's a Middleware.
        next_(args);

        // After the next middleware...
        writefln!"<-- %s"(message_);
    }
}

// Build a stack.
Stack stack;

// Push the middleware...
stack.push(new Trace("A"));
stack.push(new Trace("B"));
stack.push(new Trace("C"));

// And run it!
stack.run();

Building and running that should give the following output:

--> A
--> B
--> C
<-- C
<-- B
<-- A

License

This is all under MIT.

Authors:
unleashy
Dependencies:
none
Versions:
0.1.0 2017-Jul-06
~master 2017-Jul-06
Show all 2 versions
Download Stats:
  • 0 downloads today

  • 0 downloads this week

  • 0 downloads this month

  • 0 downloads total