Notification<T>
Represents a notification to an observer.
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Reactive.Concurrency;
using System.Runtime.CompilerServices;
namespace System.Reactive
{
[Serializable]
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public abstract class Notification<[System.Runtime.CompilerServices.Nullable(2)] T> : IEquatable<Notification<T>>
{
[Serializable]
[System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})]
[DebuggerDisplay("OnNext({Value})")]
internal sealed class OnNextNotification : Notification<T>
{
public override T Value { get; }
[System.Runtime.CompilerServices.Nullable(2)]
public override Exception Exception {
[System.Runtime.CompilerServices.NullableContext(2)]
get {
return null;
}
}
public override bool HasValue => true;
public override NotificationKind Kind => NotificationKind.OnNext;
public OnNextNotification(T value)
{
Value = value;
}
public override int GetHashCode()
{
T value = Value;
return value?.GetHashCode() ?? 0;
}
public override bool Equals([System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})] Notification<T> other)
{
if ((object)this == other)
return true;
if ((object)other == null)
return false;
if (other.Kind != 0)
return false;
return EqualityComparer<T>.Default.Equals(Value, other.Value);
}
public override string ToString()
{
return string.Format(CultureInfo.CurrentCulture, "OnNext({0})", Value);
}
public override void Accept(IObserver<T> observer)
{
if (observer == null)
throw new ArgumentNullException("observer");
observer.OnNext(Value);
}
public override TResult Accept<[System.Runtime.CompilerServices.Nullable(2)] TResult>(IObserver<T, TResult> observer)
{
if (observer == null)
throw new ArgumentNullException("observer");
return observer.OnNext(((Notification<T>)this).Value);
}
public override void Accept(Action<T> onNext, Action<Exception> onError, Action onCompleted)
{
if (onNext == null)
throw new ArgumentNullException("onNext");
if (onError == null)
throw new ArgumentNullException("onError");
if (onCompleted == null)
throw new ArgumentNullException("onCompleted");
onNext(Value);
}
public override TResult Accept<[System.Runtime.CompilerServices.Nullable(2)] TResult>(Func<T, TResult> onNext, Func<Exception, TResult> onError, Func<TResult> onCompleted)
{
if (onNext == null)
throw new ArgumentNullException("onNext");
if (onError == null)
throw new ArgumentNullException("onError");
if (onCompleted == null)
throw new ArgumentNullException("onCompleted");
return onNext(((Notification<T>)this).Value);
}
}
[Serializable]
[System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})]
[DebuggerDisplay("OnError({Exception})")]
internal sealed class OnErrorNotification : Notification<T>
{
public override T Value {
get {
Exception.Throw();
return default(T);
}
}
public override Exception Exception { get; }
public override bool HasValue => false;
public override NotificationKind Kind => NotificationKind.OnError;
public OnErrorNotification(Exception exception)
{
Exception = exception;
}
public override int GetHashCode()
{
return Exception.GetHashCode();
}
public override bool Equals([System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})] Notification<T> other)
{
if ((object)this == other)
return true;
if ((object)other == null)
return false;
if (other.Kind != NotificationKind.OnError)
return false;
return object.Equals(Exception, other.Exception);
}
public override string ToString()
{
return string.Format(CultureInfo.CurrentCulture, "OnError({0})", Exception.GetType().FullName);
}
public override void Accept(IObserver<T> observer)
{
if (observer == null)
throw new ArgumentNullException("observer");
observer.OnError(Exception);
}
public override TResult Accept<[System.Runtime.CompilerServices.Nullable(2)] TResult>(IObserver<T, TResult> observer)
{
if (observer == null)
throw new ArgumentNullException("observer");
return observer.OnError(((Notification<T>)this).Exception);
}
public override void Accept(Action<T> onNext, Action<Exception> onError, Action onCompleted)
{
if (onNext == null)
throw new ArgumentNullException("onNext");
if (onError == null)
throw new ArgumentNullException("onError");
if (onCompleted == null)
throw new ArgumentNullException("onCompleted");
onError(Exception);
}
public override TResult Accept<[System.Runtime.CompilerServices.Nullable(2)] TResult>(Func<T, TResult> onNext, Func<Exception, TResult> onError, Func<TResult> onCompleted)
{
if (onNext == null)
throw new ArgumentNullException("onNext");
if (onError == null)
throw new ArgumentNullException("onError");
if (onCompleted == null)
throw new ArgumentNullException("onCompleted");
return onError(((Notification<T>)this).Exception);
}
}
[Serializable]
[System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})]
[DebuggerDisplay("OnCompleted()")]
internal sealed class OnCompletedNotification : Notification<T>
{
internal static readonly Notification<T> Instance = new OnCompletedNotification();
public override T Value {
get {
throw new InvalidOperationException(Strings_Core.COMPLETED_NO_VALUE);
}
}
[System.Runtime.CompilerServices.Nullable(2)]
public override Exception Exception {
[System.Runtime.CompilerServices.NullableContext(2)]
get {
return null;
}
}
public override bool HasValue => false;
public override NotificationKind Kind => NotificationKind.OnCompleted;
private OnCompletedNotification()
{
}
public override int GetHashCode()
{
return typeof(T).GetHashCode() ^ 8510;
}
public override bool Equals([System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})] Notification<T> other)
{
if ((object)this == other)
return true;
if ((object)other == null)
return false;
return other.Kind == NotificationKind.OnCompleted;
}
public override string ToString()
{
return "OnCompleted()";
}
public override void Accept(IObserver<T> observer)
{
if (observer == null)
throw new ArgumentNullException("observer");
observer.OnCompleted();
}
public override TResult Accept<[System.Runtime.CompilerServices.Nullable(2)] TResult>(IObserver<T, TResult> observer)
{
if (observer == null)
throw new ArgumentNullException("observer");
return observer.OnCompleted();
}
public override void Accept(Action<T> onNext, Action<Exception> onError, Action onCompleted)
{
if (onNext == null)
throw new ArgumentNullException("onNext");
if (onError == null)
throw new ArgumentNullException("onError");
if (onCompleted == null)
throw new ArgumentNullException("onCompleted");
onCompleted();
}
public override TResult Accept<[System.Runtime.CompilerServices.Nullable(2)] TResult>(Func<T, TResult> onNext, Func<Exception, TResult> onError, Func<TResult> onCompleted)
{
if (onNext == null)
throw new ArgumentNullException("onNext");
if (onError == null)
throw new ArgumentNullException("onError");
if (onCompleted == null)
throw new ArgumentNullException("onCompleted");
return onCompleted();
}
}
[System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})]
private sealed class NotificationToObservable : ObservableBase<T>
{
private readonly IScheduler _scheduler;
private readonly Notification<T> _parent;
public NotificationToObservable(IScheduler scheduler, Notification<T> parent)
{
_scheduler = scheduler;
_parent = parent;
}
protected override IDisposable SubscribeCore(IObserver<T> observer)
{
return Scheduler.ScheduleAction<(Notification<T>, IObserver<T>)>(_scheduler, (_parent, observer), (Action<(Notification<T>, IObserver<T>)>)delegate((Notification<T> _parent, IObserver<T> observer) state) {
Notification<T> item = state._parent;
IObserver<T> item2 = state.observer;
item.Accept(item2);
if (item.Kind == NotificationKind.OnNext)
item2.OnCompleted();
});
}
}
public abstract T Value { get; }
public abstract bool HasValue { get; }
[System.Runtime.CompilerServices.Nullable(2)]
public abstract Exception Exception {
[System.Runtime.CompilerServices.NullableContext(2)]
get;
}
public abstract NotificationKind Kind { get; }
protected internal Notification()
{
}
public abstract bool Equals([System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})] Notification<T> other);
public static bool operator ==(Notification<T> left, Notification<T> right)
{
if ((object)left == right)
return true;
if ((object)left == null || (object)right == null)
return false;
return left.Equals(right);
}
public static bool operator !=(Notification<T> left, Notification<T> right)
{
return !(left == right);
}
[System.Runtime.CompilerServices.NullableContext(2)]
public override bool Equals(object obj)
{
return Equals(obj as Notification<T>);
}
public abstract void Accept(IObserver<T> observer);
public abstract TResult Accept<[System.Runtime.CompilerServices.Nullable(2)] TResult>(IObserver<T, TResult> observer);
public abstract void Accept(Action<T> onNext, Action<Exception> onError, Action onCompleted);
public abstract TResult Accept<[System.Runtime.CompilerServices.Nullable(2)] TResult>(Func<T, TResult> onNext, Func<Exception, TResult> onError, Func<TResult> onCompleted);
public IObservable<T> ToObservable()
{
return ToObservable(ImmediateScheduler.Instance);
}
public IObservable<T> ToObservable(IScheduler scheduler)
{
if (scheduler == null)
throw new ArgumentNullException("scheduler");
return new NotificationToObservable(scheduler, this);
}
}
}