RetryableUseCaseBuilder<TBloc extends JuiceBloc<TState> , TState extends BlocState, TEvent extends EventBase> class
A use case builder that automatically retries failed operations.
Wraps another use case and intercepts failures, retrying based on configurable backoff strategy and retry conditions.
Basic Usage
class MyBloc extends JuiceBloc<MyState> {
MyBloc() : super(MyState(), [
() => RetryableUseCaseBuilder(
typeOfEvent: FetchDataEvent,
useCaseGenerator: () => FetchDataUseCase(),
maxRetries: 3,
backoff: ExponentialBackoff(initial: Duration(seconds: 1)),
),
], []);
}
How It Works
- The wrapped use case executes normally
- If it calls
emitFailure, the error is captured - If the error is retryable, waits (backoff) and retries
- On success (emitUpdate) or non-retryable error, stops
- After max retries, the final failure is emitted
Retry Conditions
By default, retries when:
- Error is a JuiceException with
isRetryable == true - Error is NOT a JuiceException (assumes retryable)
Custom retry logic:
RetryableUseCaseBuilder(
// ...
retryWhen: (error) => error is NetworkException,
)
Cancellation
If the event is CancellableEvent and gets cancelled during a retry backoff, the operation aborts and emits CancelingStatus.
- Implemented types
Constructors
- RetryableUseCaseBuilder({required Type typeOfEvent, required UseCaseGenerator useCaseGenerator, int maxRetries = 3, BackoffStrategy? backoff, bool retryWhen(Object error)?, OnRetryCallback? onRetry, UseCaseEventBuilder? initialEventBuilder, EventConcurrency concurrency = EventConcurrency.concurrent})
- Creates a retryable use case builder.
Properties
- backoff → BackoffStrategy
-
Strategy for calculating delay between retries.
final
- concurrency → EventConcurrency
-
How same-type events are processed relative to one another.
final
- eventType → Type
-
The type of event this use case handles.
no setteroverride
- generator → UseCaseGenerator
-
Creates the use case instance that will handle events.
no setteroverride
- hashCode → int
-
The hash code for this object.
no setterinherited
- initialEventBuilder → UseCaseEventBuilder?
-
Optional builder for creating an initial event when the bloc starts.
no setteroverride
- maxRetries → int
-
Maximum number of retry attempts.
final
- onRetry → OnRetryCallback?
-
Callback invoked before each retry attempt.
final
- retryWhen → bool Function(Object error)?
-
Custom predicate to determine if an error should trigger a retry.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- typeOfEvent → Type
-
The event type this use case handles.
final
- useCaseGenerator → UseCaseGenerator
-
Factory function to create the wrapped use case.
final
Methods
-
close(
) → Future< void> -
Cleanup method called when the use case is disposed.
override
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited