Coder Social home page Coder Social logo

rainxh11 / rxt Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 49 KB

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

License: MIT License

C# 100.00%
computed iobservable observable reactive reactive-extensions reactive-programming reactive-object

rxt's Introduction

Version Downloads
Latest version Downloads

Rx<T>

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

This library provides 4 types: Reactive<T>, ConcurrentReactive<T>, Computed<T>, Computed<T,R>

Example Usage:

Reactive<T>

a reactive wrapper object that track changes of the underlying object it is wrapping.

  1. Create a reactive object:
using RxT;

var searchQuery = new Reactive<string>("");
  1. Change the state:
searchQuery.Value = "search";
  1. Track its state changes as an IObservable<T>
searchQuery
    .Subscribe(x => Console.WriteLine($"Search Query: {x}"));

There is also an additional ConcurrentReactive<T> a thread-safe version of Reactive<T>.

Computed<T>

a read-only reactive object that track changes of a Reactive<T> object while applying a custom filter to its underlying IObservable<T>

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

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

Both Reactive<T> & Computed<T> implements the IObservable<T> interface

Computed<T,R>

same as Computed<T> but with different type for Value

using RxT;

var searchQuery = new Reactive<string>("");
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<T>.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<T>.Modify() method

using RxT;

var pagination = new Reactive<Pagination>(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

pagination.Modify(p => p.Page = 2);

same thing can be done to Computed<T>, keep in mind it will change the source Reactive<T>.Value as it is passed by reference

paginationComputed = pagination.SpawnComputed(obs => obs)
paginationComputed.ModifySource(p => p.Page = 2);

rxt's People

Contributors

rainxh11 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.