CallbackHandler<CallbackInputType, CallbackReturnType> class

Implementation of ICallbackHandler for managing callbacks with generic types.

This class provides a high-performance callback management system using FastMap for O(1) registration and removal operations.

Type parameters:

  • CallbackInputType - The input parameter type accepted by callbacks
  • CallbackReturnType - The return type produced by callbacks

Example:

final handler = CallbackHandler<String, int>();
handler.register((input) => input.length);
final results = handler.call('Hello');
print(results); // {callback_hashCode: 5}

Dispatch semantics

call iterates over a snapshot of the registration order taken when the dispatch starts, so mutating the handler from inside a callback is safe: unregistering takes effect immediately, registering takes effect from the next dispatch, and no unrelated callback is ever skipped.

A callback that throws does not stop the dispatch. The error is routed to the onError handler, or — when none was supplied — to the current Zone, so it surfaces as an uncaught async error instead of being swallowed. The failed callback contributes no entry to the result map.

Performance characteristics:

  • Register: O(1) amortized
  • Unregister: O(n) where n is the number of registered callbacks
  • Invoke/Call: O(n) where n is the number of registered callbacks
  • Clear: O(n)
Implemented types

Constructors

CallbackHandler({void onError(Object error, StackTrace stackTrace)?})
Creates a new CallbackHandler instance.

Properties

hashCode int
The hash code for this object.
no setterinherited
map FastMap<int, CallbackWithReturn<CallbackInputType, CallbackReturnType>>
Internal storage for registered callbacks.
final
onError → void Function(Object error, StackTrace stackTrace)?
Invoked when a callback throws during call.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

call(CallbackInputType input) Map<int, CallbackReturnType>
Make the handler callable as a function.
override
clear() → void
Clear all registered callbacks
override
invoke(CallbackInputType input) Map<int, CallbackReturnType>
Invoke all registered callbacks with the given input (deprecated).
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
register(CallbackWithReturn<CallbackInputType, CallbackReturnType> callback) → void
Register a callback
override
toString() String
A string representation of this object.
inherited
unregister(CallbackWithReturn<CallbackInputType, CallbackReturnType> callback) → void
Unregister a callback
override

Operators

operator ==(Object other) bool
The equality operator.
inherited