dotnet / dotnet/reactive

BehaviorSubject suggestion: public bool UpdateValue(T value, bool onlyNotifyUpdates = false)

Open
#1,240 3 comments 0 reactions 0 assignees View on GitHub
[area] Rx
Dominant language
C#
Stars
7.2k
Forks
798
PR merge metrics
No merged PRs in 30d

Description

#### Feature request

Currently, if you want to know that setting the value (`OnNext(value)`) actually caused it to change, then you have to guarantee explicit access to the `BehaviorSubject` and apply locking to it.

Modifying the code to use `UpdateValue` at its core would help facilitate `BehaviorSubject` to allow checking for value changes when you set and not require any more locking/synchronization.

```cs
///
/// Updates the value and notifies all subscribed observers about the arrival of the specified element in the sequence.
///
/// The value to update and send to all observers.
/// If true will only notify if the value has changed.
public bool UpdateValue(T value, bool onlyNotifyUpdates = false)
{
bool same;
IObserver[] os;
lock (_gate)
{
CheckDisposed();
if (_isStopped) return false;

same = value is null ? _value is null : value.Equals(_value);
if (!same) _value = value;
else if (onlyNotifyUpdates) return false;
os = _observers.Data;
}

foreach (var o in os)
{
o.OnNext(value);
}

return !same;
}

///
/// Notifies all subscribed observers about the arrival of the specified element in the sequence.
///
/// The value to send to all observers.
public override void OnNext(T value) => UpdateValue(value);
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.