emit method

void emit(
  1. ViewStatus value, {
  2. String? errorMessage,
  3. Exception? exception,
  4. String? reason,
  5. Iterable<IObservable<BaseEvent>>? changes,
})

Implementation

void emit(ViewStatus value,
    {String? errorMessage,
    Exception? exception,
    String? reason,
    Iterable<IObservable<BaseEvent>>? changes}) {
  assert((exception != null && errorMessage == null) ||
      (errorMessage != null && exception == null) ||
      (errorMessage == null && exception == null));

  if (exception != null) {
    if (exception is HttpException) {
      errorMessage = exception.message;
    } else {
      errorMessage = exception.toString();
    }
  }

  if (errorMessage != null &&
      errorMessage.startsWith("{") &&
      errorMessage.endsWith("}")) {
    final json = jsonDecode(errorMessage) as Map<String, dynamic>;
    if (json["errors"] != null) {
      errorMessage = "";
      if (json["errors"] is Map<String, dynamic>) {
        (json["errors"] as Map<String, dynamic>)
            .forEach((key, dynamic value) {
          errorMessage =
              (errorMessage ?? "") + (value as List<dynamic>).join('\n');
        });
      } else {
        errorMessage = (json["errors"] as List<String>).join("\n");
      }
    }
  }

  final newEvent = ViewChangedEvent(
    status: value,
    errorMessage: errorMessage,
    reason: reason,
    changes: changes ?? <IObservable<BaseEvent>>[],
  );

  if (lastEvent == newEvent) return;

  lastEvent = newEvent;

  eventSubject.sink.add(
    lastEvent,
  );

  if (value == ViewStatus.submissionSuccess) hasChanges = false;
}