mapEventToState method

  1. @override
Stream<S> mapEventToState(
  1. FastCalculatorBlocEvent<FastCalculatorResults> event
)
override

Maps an event to a new state and emits the state changes as a stream.

Takes a FastCalculatorBlocEvent and returns a stream of state changes. Uses an asynchronous generator to yield new states based on the incoming event.

Implementation

@override
Stream<S> mapEventToState(FastCalculatorBlocEvent event) async* {
  final payload = event.payload;
  final eventType = event.type;

  if (eventType == FastCalculatorBlocEventType.init) {
    yield* handleInitEvent();
  } else if (eventType == FastCalculatorBlocEventType.initialized) {
    yield* handleInitializedEvent();
  } else if (eventType == FastCalculatorBlocEventType.initFailed) {
    yield* handleInitializeFailedEvent(
      payload as FastCalculatorBlocEventPayload<R>?,
    );
  } else if (isInitialized) {
    if (eventType == FastCalculatorBlocEventType.patchValue) {
      yield* handlePatchValueEvent(payload);
    } else if (eventType == FastCalculatorBlocEventType.compute) {
      yield* handleComputeEvent();
    } else if (eventType == FastCalculatorBlocEventType.computed) {
      yield* handleComputedEvent(payload);
    } else if (eventType == FastCalculatorBlocEventType.computeFailed) {
      if (payload?.error != null) {
        await handleComputeError(payload!.error);
      }

      yield currentState.copyWith(
        results: await retrieveDefaultResult(),
        isBusy: false,
      ) as S;
    } else if (eventType == FastCalculatorBlocEventType.clear) {
      final nextState = await clearCalculatorState();
      await saveCalculatorState();
      yield nextState.copyWith(isInitialized: true) as S;
      addComputeEvent();
    } else if (eventType == FastCalculatorBlocEventType.share) {
      if (payload?.value is BuildContext) {
        await shareCalculatorState(payload!.value as BuildContext);
      }
    } else if (eventType == FastCalculatorBlocEventType.exportToPdf) {
      if (payload?.value is BuildContext) {
        await exportToPdf(payload!.value as BuildContext);
      }
    } else if (eventType == FastCalculatorBlocEventType.exportToCsv) {
      if (payload?.value is BuildContext) {
        await exportToCsv(payload!.value as BuildContext);
      }
    } else if (eventType == FastCalculatorBlocEventType.exportToExcel) {
      if (payload?.value is BuildContext) {
        await exportToExcel(payload!.value as BuildContext);
      }
    } else if (eventType == FastCalculatorBlocEventType.reset) {
      yield* handleResetEvent();
    } else if (eventType == FastCalculatorBlocEventType.loadMetadata) {
      yield* handleLoadMetadataEvent();
    } else if (eventType == FastCalculatorBlocEventType.patchMetadata) {
      yield* handlePatchMetadataEvent(payload);
    }
  } else {
    assert(false, 'FastCalculatorBloc is not initialized yet.');
  }
}