Rx.Net.Plus
1.1.4
See the version list below for details.
dotnet add package Rx.Net.Plus --version 1.1.4
NuGet\Install-Package Rx.Net.Plus -Version 1.1.4
<PackageReference Include="Rx.Net.Plus" Version="1.1.4" />
paket add Rx.Net.Plus --version 1.1.4
#r "nuget: Rx.Net.Plus, 1.1.4"
// Install Rx.Net.Plus as a Cake Addin #addin nuget:?package=Rx.Net.Plus&version=1.1.4 // Install Rx.Net.Plus as a Cake Tool #tool nuget:?package=Rx.Net.Plus&version=1.1.4
Rx.Net.Plus
Bring reactiveness to variables and properties
ReactiveX gains popularity and is widely used in several platforms and languages.
If you have used Rx.Net (System.Reactive), you probably already dreamed to transform regular variable (like int, bool ) to become reactive.
What do we mean?
We would like to:
- React whenever variables change
- Connect variables to observable sources
- Not receive notification if same data is set again and again
- Continue to use these variables as before. Compare them to values, convert them...
Rx.Net.Plus introduces two new types for this purpose (+ several extension methods):
Type | Purpose |
---|---|
RxVar | replace regular variables (like int, bool...) |
RxProperty | to replace view-model properties (NotifyPropertyChanged pattern) |
RvVar
Let start with an example:
var isConnected = false.ToRx(); // Create a reactive variable`
Let say, connectionStatus is of type Observable
Observable connectionStatus;
we can do:
connectionStatus.Subscribe (isConnected);
or using our method:
isConnected.ListenTo (connectionStatus);
Now, RxVar's magic is to allow the following syntax:
if (isConnected != true)
{
// Do something.
}
As well:
var anotherRxBool = true.ToRx();
if (isConnected == connectionStatus)
{
// Do some action
}
With numeric variables (int, double...)
var intRx = 10.ToRx(); // Create a rxvar
if (intRx > 5)
{
// React !!
}
RxVar could be used as part of state machine, and instead of polling statuses, flags, counters...
Just react upon change in this manner:
isConnected.When(true).Notify (v => reactToEvent());
RxVar implements the following interfaces:
ISubject, IConvertible, IDisposable, IComparable, IEquatable
Distinct mode
By default, RxVar propagates its data (on update) only when a new distinct value is set.
That means that if some observer is subscribed to RxVar and the same value is assigned to RxVar, it will not be published.
In order to allow every update to be propagated, RxVar provide the following method:
SetDistinctMode
Limitations
As C# does not support overloading of the assignment operator, it is impossible to assign a value directly to a variable.
Therefore the following notation is disallowed:
var number = 20.3.ToRx();
number = 10; // Not possible
Instead, RxVar provides the following syntax:
number.Value = 10;
number.Set (10);
RxProperty for WPF
Rx.Net.Plus also provides means to leverage RxVar to WPF.
The name of class to use for properties is: RxProperty and it is directly derived from RxVar.
For instance:
public class ViewModel : IPropertyChangedProxy
{
public RxProperty IsFunctionEnabled { get; } = false.ToRxProperty();
public RxProperty Counter { get; }
public RxProperty Message { get; }
}
and in XAML:
< CheckBox IsChecked="{Binding IsFunctionEnabled}"/>
< TextBox Text="{Binding Counter}"/>
< TextBox Text="{Binding Message}"/>
Note that Rx.Net.Plus supports TwoWay binding mode (where target can update source as for CheckBox).
For data binding of RxProperty to work as expected, the view model shall implement a dedicated interface named IPropertyChangedProxy.
Assuming the View Model implements INotifyPropertyChanged, it shall also implements IPropertyChangedProxyas following:
void IPropertyChangedProxy.NotifyPropertyChanged(PropertyChangedEventArgs eventArgs)
{
OnPropertyChanged (eventArgs);
}
Binding shall occur after the view is created and bound to view model.
In Caliburn framework, this occurs within the OnViewAttached method as following:
class ViewModel : Screen, IPropertyChangedProxy
{
protected override void OnViewAttached(object view, object context)
{
base.OnViewAttached(view, context);
this.BindRxPropertiesToView (view); // Bind RxProperties to view
}
}
In classic MVVM or other frameworks (like Prism, MVVM Light...), call to BindRxPropertiesToView can be handled in Loaded event handler.
Extension Methods
Method | Description | Usage |
---|---|---|
RedirectTo | an alias of Subscribe | rxVar.RedirectTo(rxVar2) |
Notify | an alias of Subscribe | rxVar.Notify(rxVar2) |
When | equivalent of Where (value ⇒ value.Equals(v)) | rxVar.When(true).Notify (rxVar2) |
ToRxVar | creates an instance of RxVar of the type of var | var rxVar = 10.ToRxVar() |
ToRxVarAndSubscribe | creates an instance of RxVar of the same type of observable source and subscribe the new instance to source | IObservable<int> obs; var rxvar = obs.Where(v => v > 10).ToRxVarAndSubscribe(); |
ToRxProperty | creates an instance of RxProperty of the type of var | RxProperty<int> Counter => 10.ToRxProperty(); |
ToRxPropertyAndSubscribe** | creates an instance of RxProperty of the same type of observable source and subscribe the new instance to source | RxProperty<bool> IsPresent; |
BindRxPropertiesToView | bind the rxproperties of the view model vm (which implements IPropertyChangedProxy) to the view. | this.BindRxPropertiesToView(view) |
True, False, Not | pseudo attributes may be used as replacement for (var == true, var == false) | isConnected.True, isConnected.False, isConnected.No t |
DisposableBaseClass | an abstract class to derive from to support Dispose Pattern with some extras |
Contact us !
For any comment, suggestion...please leave a comment to reactivesw@outlook.com
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net is compatible. |
-
- System.Reactive (>= 4.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Rx.Net.Plus:
Package | Downloads |
---|---|
Rx.Net.Plus.Json
Extension to Rx.Net.Plus to support Json flat serialization |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.1.15 | 3,374 | 11/9/2020 |
1.1.14 | 445 | 11/5/2020 |
1.1.13 | 1,276 | 6/16/2020 |
1.1.12 | 1,739 | 10/10/2019 |
1.1.11 | 560 | 10/3/2019 |
1.1.10 | 8,383 | 8/1/2019 |
1.1.9 | 2,122 | 9/16/2018 |
1.1.8 | 2,404 | 9/4/2018 |
1.1.7 | 799 | 9/3/2018 |
1.1.6 | 792 | 9/2/2018 |
1.1.5 | 770 | 8/29/2018 |
1.1.4 | 773 | 8/27/2018 |
1.0.12 | 1,057 | 8/23/2018 |
1.0.11 | 742 | 8/23/2018 |
1.0.10 | 782 | 8/22/2018 |
1.0.9 | 805 | 8/2/2018 |
1.0.8 | 821 | 8/2/2018 |
1.0.7 | 769 | 7/30/2018 |
1.0.6 | 779 | 7/30/2018 |
1.0.5 | 767 | 7/30/2018 |
First release