Swallow.Flux 0.2.1

There is a newer version of this package available.
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" />
                    
Directory.Packages.props
<PackageReference Include="Swallow.Flux" />
                    
Project file
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
                    
#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
                    
Install as a Cake Addin
#tool nuget:?package=Swallow.Flux&version=0.2.1
                    
Install as a Cake Tool

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 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.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.3.0 114 6/8/2025
0.2.2 137 5/29/2025
0.2.1 134 5/29/2025
0.2.0 238 5/13/2025
0.1.0 240 5/12/2025