LfrlAnvil.Reactive.Chrono 0.2.1

dotnet add package LfrlAnvil.Reactive.Chrono --version 0.2.1
NuGet\Install-Package LfrlAnvil.Reactive.Chrono -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.Chrono" 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.Chrono --version 0.2.1
#r "nuget: LfrlAnvil.Reactive.Chrono, 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.Chrono as a Cake Addin
#addin nuget:?package=LfrlAnvil.Reactive.Chrono&version=0.2.1

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

(root) NuGet Badge

LfrlAnvil.Reactive.Chrono

This project contains a few functionalities related to timers and schedulers.

Documentation

Technical documentation can be found here.

Examples

Following is an example of a timer that emits events:

// provider of timestamps to use for the timer
ITimestampProvider timestamps = ...;

// creates a new timer that emits events every second
// the timer will do its best to not introduce any time skew,
// however it may lead to some events being skipped,
// when an interval is very short or listeners' reactions take a relatively long time to complete
var timer = new ReactiveTimer( timestamps, interval: Duration.FromSeconds( 1 ) );

// attaches a listener to the timer
timer.Listen( EventListener.Create<WithInterval<long>>( ... ) );

// starts the timer asynchronously
timer.StartAsync();

// signals the timer to stop when it gets the chance
// after fully stopping, the timer can be restarted
timer.Stop();

// disposes the timer
timer.Dispose();

Timers can also be used to invoke timer tasks, like so:

// represents an example of a timer task identified by the 'foo' key
public class FooTask : TimerTask<string>
{
    public FooTask(Timestamp start)
        : base( key: "foo" )
    {
        // specifies that the next invocation of this task
        // should happen at the start of the next minute
        var datetime = ZonedDateTime.CreateUtc( start );
        datetime = datetime.SetTimeOfDay( datetime.TimeOfDay.TrimToMinute() ) + Duration.FromMinutes( 1 );
        NextInvocationTimestamp = datetime.Timestamp;
    }

    public override async Task InvokeAsync(
        TimerTaskCollection<string> source,
        ReactiveTaskInvocationParams parameters,
        CancellationToken cancellationToken)
    {
        // performs an asynchronous operation associated with this task
        await ...;

        // specifies that the next invocation of this task
        // should happen at the start of the next minute
        NextInvocationTimestamp += Duration.FromMinutes( 1 );
    }
}

// provider of timestamps to use for the timer
ITimestampProvider timestamps = ...;

// creates a new timer that emits events every second
var timer = new ReactiveTimer( timestamps, interval: Duration.FromSeconds( 1 ) );

// registers an instance of a timer task
var tasks = timer.RegisterTasks( new[] { new FooTask( timestamps.GetNow() ) } );

// starts the timer asynchronously
timer.StartAsync();

// disposes timer tasks
tasks.Dispose();

Following is an example of a task scheduler:

// represents an example of a schedule task identified by the 'foo' key
public class FooTask : ScheduleTask<string>
{
    public FooTask()
        : base( key: "foo" ) { }

    public override async Task InvokeAsync(
        IReactiveScheduler<string> scheduler,
        ReactiveTaskInvocationParams parameters,
        CancellationToken cancellationToken)
    {
        // performs an asynchronous operation associated with this task
        await ...;
    }
}

// provider of timestamps to use for the scheduler
ITimestampProvider timestamps = ...;

// creates a new scheduler with tasks identified by string keys
var scheduler = new ReactiveScheduler<string>( timestamps );

// registers an instance of a schedule task
// that should be invoked 3 times, at:
// 1. 'start + 1 minute'
// 2. 'start + 6 minutes'
// 3. 'start + 11 minutes'
// scheduler also allows to register one-of tasks and infinitely repeating tasks
scheduler.Schedule(
    new FooTask(),
    firstTimestamp: scheduler.StartTimestamp + Duration.FromMinutes( 1 ),
    repetitions: 3,
    interval: Duration.FromMinutes( 5 ) );

// starts the scheduler asynchronously
scheduler.StartAsync();

// disposes the scheduler
scheduler.Dispose();
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

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.2.1 75 6/16/2024
0.2.0 83 6/16/2024
0.1.1 92 5/29/2024
0.1.0 85 5/26/2024