rodydavis / signals.dart

Reactive programming made simple for Dart and Flutter
http://dartsignals.dev
Apache License 2.0
437 stars 50 forks source link

Transient inconsistent state behaviour #211

Closed ChizobaAmadi closed 6 months ago

ChizobaAmadi commented 7 months ago

I was debugging an issue and observed that an async signal can temporarily propagate an invalid state where isCompleted is false and the data is set. This is because the library sets the value before setting the _completer:

 void setValue(T value) {
    this.value = AsyncState.data(value);
    if (_completer.isCompleted) _completer = Completer<bool>();
    _completer.complete(true);
  }

Widget:

 Watch((context) {
          final activities = viewStateHandler.activities;
          final panels = viewStateHandler.sectionPanels;

          print("isCompleted: ${activities.isCompleted} "
              "type: ${activities.value.runtimeType} "
              "hasValue: ${activities
              .get()
              .hasValue}");

Logs flutter: isCompleted: false type: AsyncLoading<Map<String, List>> hasValue: false flutter: isCompleted: false type: AsyncData<Map<String, List>> hasValue: true

After a hot reload, I can see isCompleted is set to true;

I'm not sure if this is a desired behaviour, as it can lead to errors for anyone checking isCompleted instead od using the map function.

rodydavis commented 6 months ago

I actually think I can remove the completer on future versions because of future signal not extending stream signal.

There is a conflict on the completed vs the stream.

Thanks for finding this!

ChizobaAmadi commented 6 months ago

Ok ! Sounds good. Really nice package btw

rodydavis commented 6 months ago

This should have fixed it: https://github.com/rodydavis/signals.dart/pull/219