RunicModelContext class

Namespace Runic.Navigation in Runic.Navigation 0.23.0

public sealed class RunicModelContext : IRunicModelContext, IAsyncDisposable, IRunicModelHookScheduler, IRunicModelContextLifetime

Inherits or implements IRunicModelContext, IAsyncDisposable, IRunicModelHookScheduler, IRunicModelContextLifetime.

A host-neutral serial execution context for a ViewModel graph.

Remarks

Each turn runs under a SynchronizationContext that queues posted callbacks as new turns. An await in asynchronous code started in a turn, such as the body of an async command that the bridge invokes, therefore resumes in a later turn of this context, as code started on a UI thread resumes on that thread. State written after the await is applied in a turn, in the order in which the continuations became ready, and no other turn runs between two awaits of the same body. An await of work that has already completed does not wait: the code after it runs on in the current turn, ahead of continuations still queued. Code after ConfigureAwait(false), or in work started with Task.Run, leaves the context; commit its result with RunicModelContext.InvokeAsync.

A continuation posted after the context stopped accepting work, or still queued when it closed, runs on the thread pool outside any turn, so the asynchronous code still finishes and work that waits for it is not stranded. SynchronizationContext.Send runs its callback in a turn of this context, or inline on the calling thread once the context is closed. An exception that escapes a posted callback, such as one thrown by an async void method, is reported through RunicModelContext.UnhandledTurnException.

As an IRunicModelHookScheduler, it runs a navigator's hooks (target factories, departure guards, initialize, resume and arrival hooks, and the disposal of owned content) in its turns: each hook starts in a turn of its own, never inline in the caller, and its awaits resume in later turns like any code started in a turn. A hook therefore sets the state of its page directly, on every host, as it would on a UI dispatcher. No turn is held across the hook's awaits, so the navigator's commits and other turns run between them. A hook must not block its turn, for example with Wait() on work that needs another turn of this context.

Constructors

RunicModelContext

public RunicModelContext()

Creates a model context that writes unhandled turn failures to Trace.

RunicModelContext

public RunicModelContext(ILogger<RunicModelContext>? logger)

Creates a model context that logs unhandled turn failures to logger.

Parameters

logger

The logger, or null for Trace output.

Properties

Closed

public CancellationToken Closed { get; }

Gets a token cancelled after the context stops accepting work and before queued work is rejected.

Remarks

Callbacks run synchronously on the closing thread. Their exceptions are reported through RunicModelContext.UnhandledTurnException and do not interrupt shutdown. The token remains usable after disposal.

Current

public static RunicModelContext? Current { get; }

Gets the context whose turn is executing on the calling thread, or null outside a turn of a RunicModelContext. For MVVM adapters; application code takes its IRunicModelContext by injection.

Remarks

An await started in a turn resumes in a later turn, where this returns the context again. MVVM adapters use it to deliver work scheduled in a turn back to the same context, as a UI framework's main-thread scheduler does for its dispatcher.

Another IRunicModelContext implementation is not reported: under Runic.Navigation.Wpf's DispatcherModelContext it is always null. Runic has no static accessor for "the UI thread's context", so code that needs one receives it from the container or from NavigationEntryContext.ModelContext, and works with every implementation.

IsExecuting

public bool IsExecuting { get; }

Gets whether the calling code is executing one of this context's turns.

Remarks

An implementation may report true more broadly, for code that its turns are serialized with anyway: Runic.Navigation.Wpf's DispatcherModelContext reports it for any code on its UI thread.

Documentation from IRunicModelContext.IsExecuting.

Methods

DisposeAsync

public ValueTask DisposeAsync()

Rejects queued work and completes after a currently executing synchronous turn returns. A running turn cannot be interrupted. Rejected invocations fault with ObjectDisposedException; rejected posted turns are reported through RunicModelContext.UnhandledTurnException.

InvokeAsync

public ValueTask InvokeAsync(Action turn, CancellationToken cancellationToken = default)

Runs a short synchronous turn in this context.

Documentation from IRunicModelContext.InvokeAsync.

InvokeAsync

public ValueTask<T> InvokeAsync<T>(Func<T> turn, CancellationToken cancellationToken = default)

Runs a short synchronous turn in this context and returns its result.

Documentation from IRunicModelContext.InvokeAsync.

TryPost

public bool TryPost(Action turn)

Queues a short synchronous turn when the context is still accepting work.

Documentation from IRunicModelContext.TryPost.

Events

UnhandledTurnException

public event EventHandler<RunicModelTurnExceptionEventArgs>? UnhandledTurnException

Receives exceptions thrown by turns queued with IRunicModelContext.TryPost, and an ObjectDisposedException for each posted turn dropped by disposal (RunicModelTurnExceptionEventArgs.Dropped). The sender is the context.

Documentation from IRunicModelContext.UnhandledTurnException.