GdlrMiddlewarePipeline

Inherits: RefCounted < Object

Wraps a core callable with before, around, after, and override callbacks.

Description

Create a pipeline around any callable and run it with the same arguments as the callable:

var pipeline := GdlrMiddlewarePipeline.new(func(amount: int): return amount + 1)
pipeline.use_before(func(amount: int): print("adding ", amount))
pipeline.use_around(func(next: Callable, amount: int): return await next.call(amount * 2))
pipeline.use_after(func(result: int, amount: int): return result * 3)
var result = await pipeline.run(2)  # 15

Callback signatures, where args are the arguments passed to run():

  • before: func(args...). Return values are ignored.

  • around: func(next: Callable, args...). Call next with the arguments to continue and return its result.

  • after: func(result, args...). Return the result to pass on.

  • core and core override: func(args...).

  • should_run: func(args...) -> bool.

The core and around callbacks can await. Before and after callbacks run without await and must return without waiting.

Order. Before and after callbacks run by ascending priority, then by insertion order. Around callbacks form layers. The callback with the lowest priority is the outer layer. Among core overrides whose predicate matches, the one with the highest priority replaces the core.

Tutorials

Methods

void

_init(core_: Callable = Callable())

void

set_core(fn: Callable)

Callable

get_core()

void

set_core_override(fn: Callable, priority: int = 0, should_run: Callable = Callable())

void

use_before(fn: Callable, priority: int = 0, should_run: Callable = Callable())

void

use_around(fn: Callable, priority: int = 0, should_run: Callable = Callable())

void

use_after(fn: Callable, priority: int = 0, should_run: Callable = Callable())

void

remove_by_callback(fn: Callable)

Variant

run(…) vararg


Method Descriptions

void _init(core_: Callable = Callable()) 🔗

Creates a pipeline. core_ is optional. Set it later with set_core().


void set_core(fn: Callable) 🔗

Sets the callable that the pipeline wraps.


Callable get_core() 🔗

Returns the callable that the pipeline wraps.


void set_core_override(fn: Callable, priority: int = 0, should_run: Callable = Callable()) 🔗

Adds a callable that replaces the core when should_run returns true. An override without a predicate always matches.


void use_before(fn: Callable, priority: int = 0, should_run: Callable = Callable()) 🔗

Adds a callback that runs before the core.


void use_around(fn: Callable, priority: int = 0, should_run: Callable = Callable()) 🔗

Adds a callback that wraps the core.


void use_after(fn: Callable, priority: int = 0, should_run: Callable = Callable()) 🔗

Adds a callback that runs after the core and can replace the result.


void remove_by_callback(fn: Callable) 🔗

Removes every callback and override that uses fn.


Variant run(…) vararg 🔗

Runs the arguments through the pipeline and returns the final result. Fails when no core is set.