handleComputeEvent method

  1. @protected
Stream<S> handleComputeEvent()

Handles the compute event by updating the state flags and performing the calculation.

Sets the isValid, isDirty, and isBusy flags in the state, then attempts to perform the computation. If successful, triggers the computed event.

If an error occurs, triggers the computeFailed event.

Yields a stream of state changes.

Implementation

@protected
Stream<S> handleComputeEvent() async* {
  debugLog('Will compute', debugLabel: debugLabel);
  yield* willCompute();

  debugLog('Computing results', debugLabel: debugLabel);

  final (isValid, isDirty) = await retrieveCalculatorStateStatus();

  yield currentState.copyWith(
    isValid: isValid,
    isDirty: isDirty,
    isBusy: true,
  ) as S;

  try {
    final results = await performCancellableAsyncOperation(compute());

    if (results != null) {
      addEvent(FastCalculatorBlocEvent.computed<R>(results));
    }
  } catch (error, stacktrace) {
    debugLog(
      'Failed to compute results',
      value: error,
      debugLabel: debugLabel,
    );

    debugLog(
      'Stacktrace',
      value: stacktrace,
      debugLabel: debugLabel,
    );

    addEvent(FastCalculatorBlocEvent.computeFailed<R>(error));
  }
}