BlazingState 6.0.0
See the version list below for details.
dotnet add package BlazingState --version 6.0.0
NuGet\Install-Package BlazingState -Version 6.0.0
<PackageReference Include="BlazingState" Version="6.0.0" />
paket add BlazingState --version 6.0.0
#r "nuget: BlazingState, 6.0.0"
// Install BlazingState as a Cake Addin #addin nuget:?package=BlazingState&version=6.0.0 // Install BlazingState as a Cake Tool #tool nuget:?package=BlazingState&version=6.0.0
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:
// 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.
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 | Versions 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. |
-
net6.0
- Microsoft.AspNetCore.Components.Web (>= 6.0.0)
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.