All Projects → kekekeks → NObservable

kekekeks / NObservable

Licence: MIT license
MobX like observable state management library with Blazor support

Programming Languages

C#
18002 projects
shell
77523 projects

Projects that are alternatives of or similar to NObservable

ngx-mobx
Mobx decorators for Angular Applications
Stars: ✭ 14 (-78.79%)
Mutual labels:  state-management, mobx
Prodo
Prodo is a React framework to build apps faster.
Stars: ✭ 114 (+72.73%)
Mutual labels:  state-management, mobx
Compare React State Management
React createContext vs Apollo vs MobX vs Redux in a simple todo app.
Stars: ✭ 81 (+22.73%)
Mutual labels:  state-management, mobx
Blue Chip
Normalizes GraphQL and JSON:API payloads into your state management system and provides ORM selectors to prepare data to be consumed by components
Stars: ✭ 332 (+403.03%)
Mutual labels:  state-management, mobx
Mobx Rest
REST conventions for Mobx
Stars: ✭ 164 (+148.48%)
Mutual labels:  state-management, mobx
Mobx State Tree
Full-featured reactive state management without the boilerplate
Stars: ✭ 6,317 (+9471.21%)
Mutual labels:  state-management, mobx
Datx
DatX is an opinionated JS/TS data store. It features support for simple property definition, references to other models and first-class TypeScript support.
Stars: ✭ 111 (+68.18%)
Mutual labels:  state-management, mobx
react-coat-ssr-demo
Demo with Typescript + React + Redux for server-side-rendering (SSR)
Stars: ✭ 100 (+51.52%)
Mutual labels:  state-management, mobx
Mobx.dart
MobX for the Dart language. Hassle-free, reactive state-management for your Dart and Flutter apps.
Stars: ✭ 2,038 (+2987.88%)
Mutual labels:  state-management, mobx
Xsm
State Management made eXtraordinarily simple and effective for Angular, React, and Vue
Stars: ✭ 138 (+109.09%)
Mutual labels:  state-management, mobx
React Coat
Structured React + Redux with Typescript and support for isomorphic rendering beautifully(SSR)
Stars: ✭ 290 (+339.39%)
Mutual labels:  state-management, mobx
Pulse
✨ Pulse is a global state and logic framework for reactive Typescript & Javascript applications. Supporting frameworks like VueJS, React and React Native.
Stars: ✭ 243 (+268.18%)
Mutual labels:  state-management, mobx
Mobx Keystone
A MobX powered state management solution based on data trees with first class support for Typescript, support for snapshots, patches and much more
Stars: ✭ 284 (+330.3%)
Mutual labels:  state-management, mobx
Flutter Boilerplate Project
A boilerplate project created in flutter using MobX and Provider.
Stars: ✭ 1,194 (+1709.09%)
Mutual labels:  state-management, mobx
micro-observables
A simple Observable library that can be used for easy state management in React applications.
Stars: ✭ 78 (+18.18%)
Mutual labels:  state-management, mobx
Reactstatemuseum
A whirlwind tour of React state management systems by example
Stars: ✭ 1,294 (+1860.61%)
Mutual labels:  state-management, mobx
mutable
State containers with dirty checking and more
Stars: ✭ 32 (-51.52%)
Mutual labels:  state-management, mobx
hooksy
Simple app state management based on react hooks
Stars: ✭ 58 (-12.12%)
Mutual labels:  state-management, mobx
React Atom
A simple way manage state in React, inspired by Clojure(Script) and reagent.cljs
Stars: ✭ 133 (+101.52%)
Mutual labels:  state-management, mobx
Flutter book
Flutter1.17.x book App,使用Mobx数据管理器支持Android和iOS,使用库json_serializable、json_annotation、dio。
Stars: ✭ 190 (+187.88%)
Mutual labels:  state-management, mobx

IL-instrumentation-based observables for C#

The idea is to implement something like MobX for .NET and to mirror its API when it's possible and makes sense. Library is originally intended to be used with Blazor, but isn't limited to that, so Blazor-specific bits live in a separate package.

For now library supports:

Missing features (aka TODO):

Usage

Installation

See also Fody usage.

NuGet installation

Install the NObservable NuGet package

dotnet add package NObservable

Add to FodyWeavers.xml

Add <NObservable/> to FodyWeavers.xml

<?xml version="1.0" encoding="utf-8" ?>
<Weavers>
  <NObservable/>
</Weavers>

API

[Observable] attribute

[Observable] instructs to instrument either one property or entire class. Property access will be tracked by NObservable.

Works like @observable decorator from MobX but can be also applied to entire class

[Observable]
class Foo
{
    public int Prop1 { get; set; }
    public int Prop2 { get; set; }
}
class Bar
{
    [Observable]
    public int Foo { get; set; }
    public int NotTracked{ get; set; }
}

Autorun

Works like autorun from MobX. It runs provided callback once and records all read property access to observable objects. If any of observed properties changes, callback will be run again and new property access list will be recorded

var o = new Foo{Prop1 = 1, Prop2 = 1};
Console.WriteLine("Initial run");
Observe.Autorun(() => {
    if(o.Prop1 == 3)
        Console.WriteLine($"Prop1: {o.Prop1} Prop2: {o.Prop2}");
    else
        Console.WriteLine($"Prop1: {o.Prop1}");    
});
Console.WriteLine("Setting Prop1 = 2, expecting update");
o.Prop1 = 2;
Console.WriteLine("Setting Prop2 = 2, it wasn't read last time, not expecting update");
o.Prop2 = 2;
Console.WriteLine("Setting Prop1 = 3, expecting update");
o.Prop1 = 3;
Console.WriteLine("Setting Prop2 = 3, it was read last time, expecting update");
o.Prop2 = 3;

Console output:

Initial run
Prop1: 1
Setting Prop1 = 2, expecting update
Prop1: 2
Setting Prop2 = 2, it wasn't read last time, not expecting update
Setting Prop1 = 3, expecting update
Prop1: 3 Prop2: 2
Setting Prop2 = 3, it was read last time, expecting update
Prop1: 3 Prop2: 3

When

Works like when from MobX. Either returns a task that completes when observed condition is met or runs a provided callback:

await Observe.When(() => o.Prop1 == 5);

Observe.When(() => o.Prop2 == 5, () => Console.WriteLine("callback"));

RunInAction

Works like runInAction from MobX. Groups multiple property updates so change reactions won't be triggered on each property set call.

Proper method instrumentation (@action decorator alternative) isn't implemented yet, but unlike MobX it would be possible to make it properly work with async functions.

var o = new Foo{Prop1 = 1};
Observe.Autorun(() => Console.WriteLine(o.Prop1));
Observe.RunInAction(() => {
    o.Prop1++;
    o.Prop1++;
    o.Prop1 = 5;
    
});

Console output:

1
5

Usage with Blazor

Install the NObservable.Blazor NuGet package and add NObservable to FodyWeavers

dotnet add package NObservable

Add UseNObservable to your Startup.cs:

public void Configure(IBlazorApplicationBuilder app)
{
    app.UseNObservable(); // <---------------
    app.AddComponent<App>("app");
}

Add @using NObservable.Blazor to _ViewImports.cshtml.

Add @implements IObserverComponent at the top of your blazor component

Now your component should update if any observable properties used during the previous render are changed. Note, that NObservable adds its own ShouldRender implementation if your component doesn't have one already, so automatic update after input events won't happen. To automatically update on local property changes decorate your local properties with [Observable]

Some kind of example app

AppModel.cs:

[Observable]
public class AppModel
{
    public int Counter { get; set; }

    public AppModel()
    {
        Tick();
    }

    async void Tick()
    {
        while (true)
        {
            Counter++;
            await Task.Delay(1000);
        }
    }
}

Startup.cs:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton<AppModel>();
    }

    public void Configure(IBlazorApplicationBuilder app)
    {
        app.UseNObservable();
        app.AddComponent<App>("app");
    }
}
@page "/"
@implements IObserverComponent
@inject AppModel model

<h1>Counter demo</h1>


Counter: @(model.Counter)

Counter should tick automatically

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].