ReactiveSchedulers class

Namespace Runic.Application.Views.ReactiveUI.Reactive in Runic.Application.Views.ReactiveUI.Reactive 0.23.0

public static class ReactiveSchedulers

Creates schedulers that deliver work through a model context, and makes ReactiveUI's main-thread scheduler deliver work scheduled in a model turn to that turn's context.

Remarks

Every call returns the same scheduler for one context for as long as the context is alive, and the main-thread scheduler that ReactiveSchedulers.InstallMainThreadScheduler installs uses that scheduler too.

Methods

For

public static IScheduler For(IRunicModelContext context)

Returns the scheduler that runs work on context.

Parameters

context

The model context.

For

public static IScheduler For(object viewModel)

Returns a scheduler that runs work on the model context that viewModel is bound to when the work is scheduled, for a ViewModel that observes a source outside model turns, such as a service's events or a timer, without taking the context in its constructor.

Parameters

viewModel

The ViewModel, or any object a window or navigator binds to a model context.

Remarks

Each call looks up viewModel in RunicModelContextRegistry.Shared, where a window binds the ViewModels it presents and a navigator the entries it owns, and schedules on that context's scheduler, the one ReactiveSchedulers.For returns, so work is ordered with the context's other work. While the ViewModel is not bound, for example in its constructor before the window binds it or after it was released, or when its context has closed, the work goes to RxSchedulers.MainThreadScheduler, read at that time: with ReactiveSchedulers.InstallMainThreadScheduler, the context of the running turn, or the replaced scheduler with event 1043 (ReactiveSchedulerOutsideModelTurn). A source that replays when the constructor subscribes therefore needs ReactiveSchedulers.For with the injected context instead.

Every call returns the same scheduler for one ViewModel for as long as the ViewModel is alive; it does not keep the ViewModel alive by itself. When viewModel is an IRunicModelContext, this is ReactiveSchedulers.For.

InstallMainThreadScheduler

public static bool InstallMainThreadScheduler(ILoggerFactory? loggerFactory = null)

Sets RxSchedulers.MainThreadScheduler, the default output scheduler of every ReactiveUI command, to a scheduler that runs work scheduled in a turn of a RunicModelContext in that context, and other work on the scheduler it replaces. A command created without an output scheduler then delivers its results and IsExecuting on the model context of the turn that executes it, which for a bridged command is the window's context, so two windows never share one. ReactiveServiceCollectionExtensions.AddRunicReactiveModelContext calls it.

Parameters

loggerFactory

Creates the logger of event 1043, in the RunicViewsTelemetry.LogCategory category; without it, the warning goes to Trace until a window opens. A later call with a logger factory replaces it.

Returns

true when the model-context main-thread scheduler is installed, also by an earlier call; false when another scheduler was kept.

Remarks

The scheduler is installed only while ReactiveUI's main-thread scheduler is a System.Reactive default (DefaultScheduler, TaskPoolScheduler, ThreadPoolScheduler, CurrentThreadScheduler or ImmediateScheduler). A scheduler that an application or UI platform set, such as a WPF dispatcher scheduler or a test scheduler, is kept; pass the context's scheduler explicitly there. To opt out, set RxSchedulers.MainThreadScheduler before this call, or replace it afterwards. Call this before ViewModels create their commands.

Work that a turn's async flow schedules after it left the turn, for example after an await with ConfigureAwait(false), in Task.Run or from a timer created in the turn, runs in that turn's context too, as do the later notifications of a command that a turn executed with a plain Execute. Work whose flow never ran in a model turn, for example from a timer or service started outside any turn, and work whose context has closed, runs on the replaced scheduler, and event 1043 (ReactiveSchedulerOutsideModelTurn) is logged once per ViewModel type that scheduled it, as found on the call stack, and once for work that no ViewModel on the stack scheduled.

When a window opens, Runic Desktop, CS-WebUI and RunicWindowTestHost.Create pass the application's ILoggerFactory to the installed scheduler, as a call with loggerFactory does. When another scheduler is in effect then, they log event 1090 (ReactiveSchedulerKept) or 1091 (ReactiveSchedulerReplaced) naming it, unless it is a UI dispatcher's scheduler.