rxdart
rxdart copied to clipboard
BehaviorSubject - way to access last value even after an exception
I see that BehaviorSubject was changed in https://github.com/ReactiveX/rxdart/issues/227 to essentially erase the most recent value if an error is added to the behavior subject. For my use case, I would like to be able to access the latest value, even if an error has been added to the stream. It would be great if the BehaviorSubject interface could include a way to track both the latest error and the latest value. It seems like it is possible to do this and still emit both the latest value and the latest error (in the appropriate order) for new subscribers, which should satisfy the needs of the users who requested https://github.com/ReactiveX/rxdart/issues/227 . What I'm proposing seems to provide strictly more information, whereas the current implementation erases information that is useful for my use case, and I assume for many other use cases.
On a related note, it's unintuitive that accessing the BehaviorSubject.value should return null when the subject in question has never actually taken a value of null, but this is exactly what happens if an error was added after the last value was emitted. Personally, I would prefer behavior where BehaviorSubject.value can only have values that were provided via the BehaviorSubject.value setter, add method, and the seeded factory. However, If the above request is not feasible, it would be great to at least have more clear documentation of this behavior in https://pub.dev/documentation/rxdart/latest/rx/BehaviorSubject/value.html .
Regarding nulls, we will have non-nullable types in Dart eventually,
On the errors, if the last event was an error, then having no value seems logic (to me), but maybe we can always wrap errors into a BehaviorSubjectError for example, which would have 2 props:
- error getter pointing to the original Error
- lastValue, which is the last valid value event
If multiple Error events would trigger in sequence, then lastValue would inherit each time.