Swallow.Flux
0.2.1
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package Swallow.Flux --version 0.2.1
NuGet\Install-Package Swallow.Flux -Version 0.2.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="Swallow.Flux" Version="0.2.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Swallow.Flux" Version="0.2.1" />
<PackageReference Include="Swallow.Flux" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Swallow.Flux --version 0.2.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Swallow.Flux, 0.2.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.
#:package Swallow.Flux@0.2.1
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Swallow.Flux&version=0.2.1
#tool nuget:?package=Swallow.Flux&version=0.2.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Swallow.Flux
A tiny library containing an implementation of the Flux pattern. I'm using it in two different projects, so I moved it to this shared library. No big deal, honestly.
Use at your own risk.
Usage
Register the required services and all of your stores in your service collection:
services.AddFlux()
.AddStore<OrdersStore>()
.AddStore<UnpaidBalanceStore>();
Each store can then react to commands, notifying all subscribers to changes:
public sealed class UnpaidBalanceStore(IEmitter emitter) : IStore
{
public decimal UnpaidBalance { get; private set; }
public Task Handle(ICommand command, CancellationToken cancellationToken = default)
{
if (command is OrderPaid { PaidAmount: var paidAmount })
{
UnpaidBalance -= paidAmount;
emitter.Emit<UnpaidBalanceChanged>();
}
// If we don't care about the command, we can just ignore it
return Task.CompletedTask;
}
}
Depending on the type of UI you're building, you can dispatch these commands and react to the emitted events.
public sealed class MyUiWindow : IDisposable
{
private readonly IDispatcher dispatcher;
private readonly IBinder binder;
private readonly UnpaidBalanceStore unpaidBalanceStore;
private readonly MyUiButton payButton = new UiButton();
private readonly MyUiLabel unpaidBalanceLabel = new UiLabel();
public MyUiWindow(
IDispatcher dispatcher,
IBinder binder,
UnpaidBalanceStore unpaidBalanceStore)
{
this.dispatcher = dispatcher;
this.binder = binder;
this.unpaidBalanceStore unpaidBalanceStore binder;
// Everytime the unpaid balance changes, we want to update the label to reflect that
binder.Bind(unpaidBalanceLabel).To<UnpaidBalanceChanged>(UpdateUnpaidBalance)
}
private void UpdateButton(UiLabel label)
{
// We access the store to fetch the current value and display it to the user
label.Text = $"Unpaid balance: {unpaidBalanceStore.UnpaidBalance}";
}
public async Task OnPayClicked(decimal amount)
{
// The user has paid - time to notify the stores of this fact
await dispatcher.Dispatch(new OrderPaid(PaidAmount: amount));
}
public void Dispose()
{
// Unless overriden, the IBinder is registered as transient.
// To properly dispose of all subscriptions, we should dispose the binder when disposing
// the containing object.
binder.Dipose();
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net9.0
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.