An open API service indexing awesome lists of open source software.

https://github.com/rainxh11/rxt

.NET Reactive Types that provide similar functionalities inspired from: (reactive, ref & computed) found in Vue.js
https://github.com/rainxh11/rxt

computed iobservable observable reactive reactive-extensions reactive-object reactive-programming

Last synced: 9 months ago
JSON representation

.NET Reactive Types that provide similar functionalities inspired from: (reactive, ref & computed) found in Vue.js

Awesome Lists containing this project

README

          

![](https://raw.githubusercontent.com/rainxh11/RxT/master/RxT.svg)

|Version|Downloads|
|-|-|
|[![Latest version](https://img.shields.io/nuget/v/RxT.svg)](https://www.nuget.org/packages/RxT/)|![Downloads](https://img.shields.io/nuget/dt/RxT.svg)|

# Rx\
.NET Reactive Types that provide similar functionalities inspired from: (`reactive`, `ref` & `computed`) found in **Vue.js**

### This library provides 4 types: `Reactive`, `ConcurrentReactive`, `Computed`, `Computed`
# Example Usage:
## `Reactive`
a reactive wrapper object that track changes of the underlying object it is wrapping.
1. Create a reactive object:
```csharp
using RxT;

var searchQuery = new Reactive("");
```
2. Change the state:
```csharp
searchQuery.Value = "search";
```
3. Track its state changes as an `IObservable`
```csharp
searchQuery
.Subscribe(x => Console.WriteLine($"Search Query: {x}"));
```
### There is also an additional `ConcurrentReactive` a thread-safe version of `Reactive`.
## `Computed`
a read-only reactive object that track changes of a `Reactive` object while applying a custom filter to its underlying `IObservable`

1. Create a computed object from previous `searchQuery` with 500ms debouncing filter
```csharp
using RxT;

var searchQuery = new Reactive("");
var searchQueryDebounced = searchQuery
.SpawnComputed(obs => obs.Throttle(500));
```
3. Track its state changes as an `IObservable`
```csharp
searchQueryDebounced
.Subscribe(x => Console.WriteLine($"Search Query Debounced: {x}"));
```

Both `Reactive` & `Computed` implements the `IObservable` interface

## `Computed`
same as `Computed` but with different type for `Value`
```csharp
using RxT;

var searchQuery = new Reactive("");
var searchLength = searchQuery
.SpawnComputed(
// Transform function to apply each time state changes
query => query.Length,
obs => obs.DistinctUntilChanged());
```

## Changing values of nested properties:
changing `Reactive.Value` value directly will only trigger state change if `T` is a primitive type or `string`, nested properties will not trigger any state change.

### Solution:
use `Reactive.Modify()` method
```csharp
using RxT;

var pagination = new Reactive(new Pagination()
{
Page = 1,
PageSize = 25,
SortBy = "+createdAt"
});

pagination.Modify((p, triggerChange) =>
{
p.Page = 2;
triggerChange();
});

record Pagination
{
public int Page { get; set; }
public int PageSize { get; set; }
public string SortBy { get; set; }
}
```
you can also omit `triggerChange()` and state change will be triggered automatically
```csharp
pagination.Modify(p => p.Page = 2);
```
same thing can be done to `Computed`, keep in mind it will change the source `Reactive.Value` as it is passed by reference
```csharp
paginationComputed = pagination.SpawnComputed(obs => obs)
paginationComputed.ModifySource(p => p.Page = 2);
```