handleComputedEvent method

Handles the computed event by updating the state with the computed results.

Implementation

@protected
Stream<S> handleComputedEvent(
  FastCalculatorBlocEventPayload? payload,
) async* {
  if (payload != null) {
    debugLog('Results computed', debugLabel: debugLabel);
    debugLog('Results', value: payload.results, debugLabel: debugLabel);

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

    if (isValid) {
      final fields = currentState.fields;
      // Note: capture the fields outside the closure to avoid
      // capturing the whole state object.
      final params = fields.toJson();

      _analyticsDebouncer?.run(() {
        if (params.isNotEmpty && !closed) {
          analyticsEventController.add(BlocAnalyticsEvent(
            type: FastCalculatorBlocAnalyticEvent.computedFields,
            parameters: params,
          ));
        }
      });
    }

    yield currentState.copyWith(
      results: payload.results,
      isValid: isValid,
      isDirty: isDirty,
      isBusy: false,
    ) as S;
  }

  yield currentState.copyWith(isBusy: false) as S;
}