LfrlAnvil.Reactive.Core 0.2.1

dotnet add package LfrlAnvil.Reactive.Core --version 0.2.1
NuGet\Install-Package LfrlAnvil.Reactive.Core -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="LfrlAnvil.Reactive.Core" Version="0.2.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add LfrlAnvil.Reactive.Core --version 0.2.1
#r "nuget: LfrlAnvil.Reactive.Core, 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.
// Install LfrlAnvil.Reactive.Core as a Cake Addin
#addin nuget:?package=LfrlAnvil.Reactive.Core&version=0.2.1

// Install LfrlAnvil.Reactive.Core as a Cake Tool
#tool nuget:?package=LfrlAnvil.Reactive.Core&version=0.2.1

(root) NuGet Badge

LfrlAnvil.Reactive.Core

This project contains a few reactive programming functionalities.

Documentation

Technical documentation can be found here.

Examples

Following is an example of an event publisher usage:

// creates a new event publisher with events of 'string' type
var publisher = new EventPublisher<string>();

// attaches a new event listener to the publisher
var subscriber = publisher.Listen( EventListener.Create<string>( e => Console.WriteLine( $"First: '{e}'" ) ) );

// attaches another event listener to decorated version of the publisher
// this listener will be invoked only for events that contain
// at least two characters and that can be parsed to 32-bit integers
publisher
    .Where( e => e.Length > 1 )
    .Select( e => int.TryParse( e, out var r ) ? r : ( int? )null )
    .WhereNotNull()
    .Listen( EventListener.Create<int>( e => Console.WriteLine( $"Second: {e}" ) ) );

// publishes a few events
publisher.Publish( "foo" );
publisher.Publish( "1" );
publisher.Publish( "123" );
publisher.Publish( "1r" );
publisher.Publish( "bar" );
publisher.Publish( "42" );

// expected console output:
// First: 'foo'
// First: '1'
// First: '123'
// Second: 123
// First: '1r'
// First: 'bar'
// First: '42'
// Second: 42

// removes the first event listener from the publisher
subscriber.Dispose();

// publishes a few more events
publisher.Publish( "qux" );
publisher.Publish( "10" );

// expected console output:
// Second: 10

// removes all listeners from the publisher and disposes it
publisher.Dispose();

There exist plenty of other built-in event source decorators. It's also possible to create entirely new decorators.

Following is an example of conversions between event streams and dotnet tasks:

// creates a new event publisher with events of 'string' type
var publisher = new EventPublisher<string>();

// creates a new task from the event stream
// this task will complete with the last emitted event,
// once the underlying event stream is disposed
var task = publisher.ToTask( CancellationToken.None );

// publishes a few events
publisher.Publish( "foo" );
publisher.Publish( "bar" );
publisher.Publish( "qux" );

// at this point, the task is still running
// disposing the publisher will cause the task to complete
publisher.Dispose();

// gets the task's result, which should be equal to 'qux'
var result = await task;

// ----------

// creates a new task
var taskSource = new TaskCompletionSource<string>();
var task = taskSource.Task;

// creates a new event source based on the task
// provided task factory will be invoked for each listener
// and listeners will react to task's result, once it completes
var source = EventSource.FromTask( _ => task );

// attaches a new listener to the task event source
source.Listen( EventListener.Create<FromTask<string>>( e => Console.WriteLine( $"Task: '{e.Result}'" ) ) );

// completes the task with a result
taskSource.SetResult( "foo" );

// expected console output:
// Task: 'foo'

Following is an example of an in-memory event exchange:

// creates a new empty exchange, where publishers are identified by their event type
var exchange = new EventExchange();

// registers a new publisher with events of 'string' type
exchange.RegisterPublisher<string>();

// registers a new publisher with events of 'string' type
exchange.RegisterPublisher<int>();

// attaches listeners to registered publishers
exchange.Listen( EventListener.Create<string>( e => Console.WriteLine( $"String: '{e}'" ) ) );
exchange.Listen( EventListener.Create<int>( e => Console.WriteLine( $"Int: '{e}'" ) ) );

// publishes a few events
exchange.Publish( "foo" );
exchange.Publish( 42 );
exchange.Publish( "bar" );
exchange.Publish( "qux" );
exchange.Publish( 123 );

// expected console output:
// String: 'foo'
// Int: 42
// String: 'bar'
// String: 'qux'
// Int: 123
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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 (2)

Showing the top 2 NuGet packages that depend on LfrlAnvil.Reactive.Core:

Package Downloads
LfrlAnvil.Reactive.Queues

This project contains a few functionalities related to event queues.

LfrlAnvil.Reactive.State

This project contains a few functionalities related to state management.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.2.1 89 6/16/2024
0.2.0 84 6/16/2024
0.1.1 105 5/29/2024
0.1.0 102 5/26/2024