Class BaseSubjectAsync<T>
- Namespace
- ReactiveUI.Extensions.Async.Subjects
- Assembly
- ReactiveUI.Extensions.dll
Provides a base class for asynchronous subjects that support both publishing values to observers and receiving values asynchronously.
public abstract class BaseSubjectAsync<T> : ObservableAsync<T>, ISubjectAsync<T>, IObserverAsync<T>, IAsyncDisposable, IObservableAsync<T>
Type Parameters
TThe type of elements processed by the subject and observed by subscribers.
- Inheritance
-
BaseSubjectAsync<T>
- Implements
- Derived
- Inherited Members
- Extension Methods
Remarks
This class enables the implementation of asynchronous subjects that can broadcast values, errors, and completion notifications to multiple observers. It manages observer registration, notification, and completion in a thread-safe manner. Derived classes should override the core notification methods to customize how observers are notified asynchronously. The subject supports asynchronous subscription and notification patterns, making it suitable for reactive and event-driven programming scenarios.
Constructors
BaseSubjectAsync()
protected BaseSubjectAsync()
Methods
DisposeAsync()
Asynchronously releases the unmanaged resources used by the object.
public ValueTask DisposeAsync()
Returns
- ValueTask
A ValueTask that represents the asynchronous dispose operation.
OnCompletedAsync(Result)
Notifies all registered observers that the asynchronous operation has completed and provides the final result.
public ValueTask OnCompletedAsync(Result result)
Parameters
resultResultThe result to deliver to observers upon completion. Cannot be null.
Returns
- ValueTask
A ValueTask that represents the asynchronous notification operation. The task completes when all observers have been notified.
Remarks
If the operation has already completed, this method returns immediately without notifying observers again. This method is thread-safe and ensures that observers are notified only once.
OnCompletedAsyncCore(IReadOnlyList<IObserverAsync<T>>, Result)
Invoked to asynchronously notify all observers of the completion event with the specified result.
protected abstract ValueTask OnCompletedAsyncCore(IReadOnlyList<IObserverAsync<T>> observers, Result result)
Parameters
observersIReadOnlyList<IObserverAsync<T>>A read-only list of observers to be notified. Cannot be null.
resultResultThe result to provide to each observer upon completion.
Returns
- ValueTask
A ValueTask that represents the asynchronous notification operation.
Remarks
Implementations should ensure that all observers are notified, and handle any exceptions that may occur during notification according to the desired error-handling policy.
OnErrorResumeAsync(Exception, CancellationToken)
Notifies all observers of an error and allows asynchronous error handling to resume observation.
public ValueTask OnErrorResumeAsync(Exception error, CancellationToken cancellationToken)
Parameters
errorExceptionThe exception that occurred and will be sent to observers. Cannot be null.
cancellationTokenCancellationTokenA cancellation token that can be used to cancel the asynchronous error notification operation.
Returns
- ValueTask
A ValueTask that represents the asynchronous operation of notifying observers of the error.
Remarks
If the sequence has already completed or an error has previously been signaled, this method has no effect.
OnErrorResumeAsyncCore(IReadOnlyList<IObserverAsync<T>>, Exception, CancellationToken)
Handles error recovery for the specified observers by resuming asynchronous processing after an error occurs.
protected abstract ValueTask OnErrorResumeAsyncCore(IReadOnlyList<IObserverAsync<T>> observers, Exception error, CancellationToken cancellationToken)
Parameters
observersIReadOnlyList<IObserverAsync<T>>A read-only list of observers that are to be notified or resumed following the error. Cannot be null.
errorExceptionThe exception that triggered the error handling logic. Cannot be null.
cancellationTokenCancellationTokenA cancellation token that can be used to cancel the asynchronous error recovery operation.
Returns
- ValueTask
A ValueTask that represents the asynchronous error recovery operation.
Remarks
Override this method to implement custom error recovery strategies for observers. The method is called when an error occurs during asynchronous processing, allowing derived classes to determine how to resume or notify observers. If the operation is canceled via the provided cancellation token, the returned task should reflect the cancellation.
OnNextAsync(T, CancellationToken)
Asynchronously notifies all subscribed observers of a new value.
public ValueTask OnNextAsync(T value, CancellationToken cancellationToken)
Parameters
valueTThe value to send to each observer.
cancellationTokenCancellationTokenA cancellation token that can be used to cancel the notification operation.
Returns
- ValueTask
A ValueTask that represents the asynchronous notification operation.
Remarks
If the sequence has already completed, this method does not notify observers.
OnNextAsyncCore(IReadOnlyList<IObserverAsync<T>>, T, CancellationToken)
Asynchronously notifies the specified observers with the provided value.
protected abstract ValueTask OnNextAsyncCore(IReadOnlyList<IObserverAsync<T>> observers, T value, CancellationToken cancellationToken)
Parameters
observersIReadOnlyList<IObserverAsync<T>>A read-only list of observers to be notified. Cannot be null.
valueTThe value to send to each observer.
cancellationTokenCancellationTokenA cancellation token that can be used to cancel the notification operation.
Returns
- ValueTask
A ValueTask that represents the asynchronous notification operation.
SubscribeAsyncCore(IObserverAsync<T>, CancellationToken)
Subscribes the specified asynchronous observer to receive notifications from the observable sequence.
protected override ValueTask<IAsyncDisposable> SubscribeAsyncCore(IObserverAsync<T> observer, CancellationToken cancellationToken)
Parameters
observerIObserverAsync<T>The asynchronous observer that will receive notifications from the observable sequence.
cancellationTokenCancellationTokenA cancellation token that can be used to cancel the subscription operation.
Returns
- ValueTask<IAsyncDisposable>
A task that represents the asynchronous subscription operation. The result contains a disposable object that can be used to unsubscribe the observer.
Remarks
If the observable sequence has already completed, the observer receives the completion notification immediately and the returned disposable is a no-op. Otherwise, the observer is added to the list of active observers and will receive future notifications until unsubscribed or the sequence completes.