Defines an advanced interception dispatcher that coordinates multiple method interceptors.
A MethodInterceptorDispatcher extends the standard MethodInterceptor contract by adding a conditional dispatch mechanism. It allows interceptors to be applied selectively based on dynamic context such as method hierarchy, invocation metadata, or specific runtime conditions.
This interface serves as the central orchestration layer in JetLeaf’s interception pipeline. It determines which interceptors should apply to a given invocation and in what order, ensuring correct and predictable execution flow.
Responsibilities
- Manage ordered interceptor chains
- Perform conditional matching and routing
- Dispatch intercepted function calls with contextual control
Example
class DefaultInterceptorDispatcher implements MethodInterceptorDispatcher {
final List<MethodInterceptor> _interceptors;
DefaultInterceptorDispatcher(this._interceptors);
@override
Future<T?> intercept<T>(MethodInvocation<T> invocation) {
return when(
invocation.getOriginalRequest(),
invocation.getTarget(),
0,
arguments: invocation.getArgument(),
methodName: invocation.getMethod().getName(),
);
}
@override
Future<T> when<T>(
MethodInvocator<T> function,
Object target,
int hierarchy, {
ExecutableArgument? arguments,
String? methodName,
bool? failIfNotFound = false,
}) async {
// Dispatch logic: apply interceptors conditionally based on metadata
for (final interceptor in _interceptors) {
if (interceptor.supports(target, methodName)) {
return await interceptor.interceptInvocation(
target, function, arguments, hierarchy,
);
}
}
if (failIfNotFound == true) {
throw StateError('No matching interceptor found for $methodName');
}
return await function();
}
}
@see MethodInterceptor @see MethodInvocation
- Implementers
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
-
when<
T> (AsyncMethodInvocator< T> function, Object target, String methodName, [ExecutableArgument? arguments, Class? targetClass]) → Future<T> - Executes a function with interception support and conditional routing.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited