BlazingState 6.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package BlazingState --version 6.0.1
NuGet\Install-Package BlazingState -Version 6.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BlazingState" Version="6.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add BlazingState --version 6.0.1
#r "nuget: BlazingState, 6.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install BlazingState as a Cake Addin
#addin nuget:?package=BlazingState&version=6.0.1

// Install BlazingState as a Cake Tool
#tool nuget:?package=BlazingState&version=6.0.1

BlazingState

This library provides powerful, robust, fast and yet simple state management with almost 0 boilerplate code.

You can use this library for any project, not just with Blazor.

Setup

Download the latest release from NuGet.

Usage

Registering state observers

Start by registering your tracked type to your services.

Note: You can only register 1 state observer per type.

// Sample class for the examples
public class MyData
{
    public string SomeString { get; set; }
}

// Registers a state observer of this type with no initial value
builder.Services.AddStateObserver<MyData>();

// You can also initialize MyData with an initial value
builder.Services.AddStateObserver<MyData>(new MyData());

// And even with an implementation factory
builder.Services.AddStateObserver<MyData>(sp =>
{
    var service = sp.GetRequiredService<SomeOtherService>();
    return new MyData { SomeString = service.SomeText };
});

If you don't use DI or another DI framework you can also manually instantiate your state observers:

var myStateObserver = new StateObserver<MyData>();

// And the same with an optional initial value
var myStateObserver = new StateObserver<MyData>(initialValue);

Reading the value from state observers

Add the following property to your razor component (or if using a normal class, just use normal di):

[Inject]
protected StateObserver<MyData> MyData { get; set; } = null!;

Subscribe using the OnParametersSet lifecycle event in Blazor (or use the ctor for normal classes):

protected override void OnParametersSet()
{
    // Now if another component changed the value property of MyData, this callback gets executed
    MyData.Register(this, () =>
    {
        // Rerender the UI
        StateHasChanged();
    });

    // You can also use async callbacks (actually faster since the sync version gets wrapped)
    // Keep in mind that you can only register 1 event per instance, so the previous callback gets overriden by the async one
    MyData.Register(this, async () =>
    {
        await SomeMethodAsync();
    });
}

You don't have to unsubscribe from the event, as soon as the GC collects the instance, the event is removed.
If you really need the memory you should implement IDisposable and remove the event by yourself in the dispose method to cleanup resources immediately but that's not needed. An alternative would be to call GC.Collect(), as that forces the GC to perform a collection which removes all unused events but I wouldn't recommend this.

public void Dispose()
{
    MyData.Unregister(this);
}

Display the value:

<p>Current value of MyData: @MyData.Value.SomeString</p>

Updating the value of state observers

To update the current value of the observer you can just set the Value property of your observer.

// Assuming you have a method which gets called when you want to perform an update (e.g. clicking on a button):
public void UpdateValue(string newValue)
{
    MyData.Value = new MyData { SomeString = newValue };
}

That's all! All subscribers are recieving your update automatically.

You can also just set some properties of the value instance and then manually notify all subscribers:

public async Task UpdateValue(string newValue)
{
    MyData.Value.SomeString = newValue;
    await MyData.NotifyStateChangedAsync();
}

This way you don't have to reallocate the object every time which can build up a lot of pressure on GC with bigger objects.

As mentioned at the start you can use an implementation factory for initialization. However sometimes you need to retrieve data from a data source (e.g. some web api) to initialize your value.
For that use case you could create your own component like InitMyData with following code:

@code {
	[Inject]
	protected StateObserver<MyData> MyData { get; set; } = null!;

	protected override async Task OnParametersSetAsync()
	{
		// Getting data from server
		var name = await GetName();
		MyData.Value = new MyData { SomeString = name };
	}

	private async Task<string> GetName()
	{
		await Task.Delay(2000);
		return "Name from Server";
	}
}

And add this component at the top of your used layout.

<InitMyData />

Obviously you can do the same with non Blazor projects by getting the your observer instance, retrieving the needed data and setting the Value property of the state observer.

Sample projects

More samples can be found in the Samples directory.

License

BlazingState is licensed under Apache License 2.0, see LICENSE.txt for more information.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on BlazingState:

Package Downloads
BlazingState.WebAssembly

Powerful, robust, fast and yet simple (automatic!) state management with almost 0 boilerplate code.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
7.0.0 359 11/10/2022
6.1.1 482 8/8/2022
6.1.0 463 8/8/2022
6.0.1 401 5/11/2022
6.0.0 410 5/11/2022