Path: redb-route/redb.Route/Definitions/LoopDefinition.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Definitions/LoopDefinition.cs
----
using redb.Route.Abstractions;
using redb.Route.Processors;
namespace redb.Route.Definitions;
///
/// Scope-opener definition for the Loop EIP.
/// Repeats the body pipeline either a fixed number of times, a dynamic number of times
/// resolved from the exchange, or while a predicate holds. Inherits the leaf DSL from
/// ; close with .
///
public class LoopDefinition : RouteDefinitionBase, IRouteScope
{
private enum LoopMode { Count, CountFactory, While }
private readonly LoopMode _mode;
private readonly int _count;
private readonly Func? _countFactory;
private readonly IPredicate? _condition;
private readonly bool _copy;
private readonly bool _shareScope;
/// Creates a count-based loop definition.
internal LoopDefinition(int count, bool copy, bool shareScope)
{
if (count < 0) throw new ArgumentOutOfRangeException(nameof(count), "Must be non-negative.");
_mode = LoopMode.Count;
_count = count;
_copy = copy;
_shareScope = shareScope;
}
/// Creates a dynamic-count loop definition.
internal LoopDefinition(Func countFactory, bool copy, bool shareScope)
{
ArgumentNullException.ThrowIfNull(countFactory);
_mode = LoopMode.CountFactory;
_countFactory = countFactory;
_copy = copy;
_shareScope = shareScope;
}
/// Creates a while-condition loop definition.
internal LoopDefinition(IPredicate condition, bool copy, bool shareScope)
{
ArgumentNullException.ThrowIfNull(condition);
_mode = LoopMode.While;
_condition = condition;
_copy = copy;
_shareScope = shareScope;
}
internal LoopDefinition(Func condition, bool copy, bool shareScope)
: this(new Predicates.LambdaPredicate(condition ?? throw new ArgumentNullException(nameof(condition))), copy, shareScope)
{
}
/// Whether each iteration receives an independent clone of the exchange.
public bool Copy => _copy;
// ── Navigation ─────────────────────────────────────────────────────────────
/// Closes this loop scope and returns the parent route definition.
public IRouteDefinition EndLoop()
=> (IRouteDefinition)(Parent ?? throw new InvalidOperationException(
"EndLoop() called without a parent route."));
///
public IRouteDefinition End() => EndLoop();
// ── IProcessorDefinition ───────────────────────────────────────────────────
///
public override IProcessor CreateProcessor(IRouteContext context)
{
var body = BuildPipeline(Outputs, context);
if (_mode == LoopMode.Count)
return new LoopProcessor(body, _count, _copy, _shareScope);
if (_mode == LoopMode.CountFactory)
return new LoopProcessor(body, _countFactory!, _copy, _shareScope);
return new LoopProcessor(body, _condition!, _copy, _shareScope);
}
private static IProcessor BuildPipeline(IList outputs, IRouteContext context)
=> NodePipeline.Body(context, outputs);
}