RpcCallScope class final
Structured concurrency scope for an RPC call.
Tracks resources (stream subscriptions, cleanup callbacks) and automatically disposes them when the call ends — by success, error, cancellation, or deadline.
The server injects one scope per incoming call into the handler's context and closes it when the call ends, running registered disposers in LIFO order. Retrieve it inside a responder handler:
Future<Response> myHandler(Request req, {RpcContext? context}) async {
final scope = context?.callScope;
scope?.onDispose(() => releaseResource());
scope?.listen(externalStream, (event) { ... }); // auto-cancelled
return Response(...);
}
(Each call processor also owns a separate internal scope for its own transport-level subscriptions.)
Constructors
- RpcCallScope({RpcContext? context, Duration disposerTimeout = defaultDisposerTimeout})
- Creates a scope optionally bound to an RpcContext.
Properties
- cancellationToken → RpcCancellationToken?
-
The cancellation token from the bound context (if any).
no setter
- disposerTimeout → Duration
-
How long a single disposer may take before it is abandoned. The scope a
server builds for each call uses defaultDisposerTimeout.
final
-
done
→ Future<
void> -
Completes when the scope is closed.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- isClosed → bool
-
Whether the scope has been closed.
no setter
- remaining → Duration?
-
Remaining time until deadline, or null if no deadline.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
close(
) → Future< void> - Closes the scope and runs all disposers in reverse order.
-
listen<
T> (Stream< T> stream, void onData(T event), {Function? onError, void onDone()?, bool? cancelOnError}) → StreamSubscription<T> -
Listens to
streamand auto-cancels the subscription when the scope closes. Returns the subscription for manual control. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
onDispose(
FutureOr< void> callback()) → void - Registers a cleanup callback that runs when the scope closes.
-
toString(
) → String -
A string representation of this object.
inherited
-
track<
T> (Stream< T> stream) → Stream<T> -
Wraps
streamso it auto-cancels when the scope closes. -
use<
T> (T resource, FutureOr< void> dispose(T resource)) → T -
Registers
disposeforresourceand returns the resource — acquire and clean-up in one expression, like a scopedusing/with:
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Constants
- defaultDisposerTimeout → const Duration
- The default disposerTimeout.