Skip to main content

AsyncAtom<T> class

Namespace: Arlecchino.Atoms · Assembly: Arlecchino

A value produced by background work, with its progress exposed as state for a view to draw. Results are handed back on the drawing thread, and a new load cancels the one before it.

public sealed class AsyncAtom<T> : IReadableAtom<T>

Implements IReadableAtom<T>

Constructors

MemberSummary
AsyncAtom(T)Creates the state, without starting anything.

Properties

MemberSummary
ErrorWhat the last load threw, or null when it did not fail.
IsLoadingWhether a load is running right now.
StatusProgress of the last load, for showing a spinner or an error.
ValueThe last loaded value. It stays put while a new load runs, so the view keeps its content.

Methods

MemberSummary
Cancel()Abandons the running load, keeping whatever was loaded before it. The state stops reporting itself as loading, so a spinner bound to it stops.
Load(Func<CancellationToken, Task<T>>)Starts work in the background, canceling whatever was already running. Returns at once; the result, or the failure, arrives later on the UI thread.
Subscribe(Action)Watches for a new value. Progress changes on their own do not notify.
SubscribeToStatus(Action)Watches progress, which is what a spinner or an error line needs.

Constructors in detail

AsyncAtom(T)

public AsyncAtom(T initial);

Creates the state, without starting anything.

Parameters

NameTypeDescription
initialTWhat to hold until the first load finishes.

Properties in detail

Error

public IReadableAtom<Exception> Error { get; }

What the last load threw, or null when it did not fail.

Type IReadableAtom<Exception>

IsLoading

public bool IsLoading { get; }

Whether a load is running right now.

Type bool

Status

public IReadableAtom<LoadStatus> Status { get; }

Progress of the last load, for showing a spinner or an error.

Type IReadableAtom<LoadStatus>

Value

public T Value { get; }

The last loaded value. It stays put while a new load runs, so the view keeps its content.

Type T

Methods in detail

Cancel()

public void Cancel();

Abandons the running load, keeping whatever was loaded before it. The state stops reporting itself as loading, so a spinner bound to it stops.

Load(Func<CancellationToken, Task<T>>)

public void Load(Func<CancellationToken, Task<T>> load);

Starts work in the background, canceling whatever was already running. Returns at once; the result, or the failure, arrives later on the UI thread.

Parameters

NameTypeDescription
loadFunc<T, TResult><CancellationToken, Task<TResult><T>>The work to run, given a token that is canceled when a newer load starts.

Subscribe(Action)

public IDisposable Subscribe(Action listener);

Watches for a new value. Progress changes on their own do not notify.

Parameters

NameTypeDescription
listenerActionCalled after the value changes.

Returns IDisposable — Dispose to stop listening.

SubscribeToStatus(Action)

public IDisposable SubscribeToStatus(Action listener);

Watches progress, which is what a spinner or an error line needs.

Parameters

NameTypeDescription
listenerActionCalled after the status changes.

Returns IDisposable — Dispose to stop listening.