1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System.Diagnostics;
using System.Globalization;
using System.Collections.Generic;
using System.Reactive.Concurrency;
#pragma warning disable 0659
#pragma warning disable 0661
namespace System.Reactive
{
/// <summary>
/// Indicates the type of a notification.
/// </summary>
public enum NotificationKind
{
/// <summary>
/// Represents an OnNext notification.
/// </summary>
OnNext,
/// <summary>
/// Represents an OnError notification.
/// </summary>
OnError,
/// <summary>
/// Represents an OnCompleted notification.
/// </summary>
OnCompleted
}
/// <summary>
/// Represents a notification to an observer.
/// </summary>
/// <typeparam name="T">The type of the elements received by the observer.</typeparam>
#if !NO_SERIALIZABLE
[Serializable]
#endif
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2218:OverrideGetHashCodeOnOverridingEquals", Justification = "Resembles a discriminated union with finite number of subclasses (external users shouldn't create their own subtypes), each of which does override GetHashCode itself.")]
public abstract class Notification<T> : IEquatable<Notification<T>>
{
/// <summary>
/// Default constructor used by derived types.
/// </summary>
protected internal Notification()
{
}
/// <summary>
/// Returns the value of an OnNext notification or throws an exception.
/// </summary>
public abstract T Value
{
get;
}
/// <summary>
/// Returns a value that indicates whether the notification has a value.
/// </summary>
public abstract bool HasValue
{
get;
}
/// <summary>
/// Returns the exception of an OnError notification or returns null.
/// </summary>
public abstract Exception Exception
{
get;
}
/// <summary>
/// Gets the kind of notification that is represented.
/// </summary>
public abstract NotificationKind Kind
{
get;
}
/// <summary>
/// Represents an OnNext notification to an observer.
/// </summary>
#if !NO_DEBUGGER_ATTRIBUTES
[DebuggerDisplay("OnNext({Value})")]
#endif
#if !NO_SERIALIZABLE
[Serializable]
#endif
internal sealed class OnNextNotification : Notification<T>
{
T value;
/// <summary>
/// Constructs a notification of a new value.
/// </summary>
public OnNextNotification(T value)
{
this.value = value;
}
/// <summary>
/// Returns the value of an OnNext notification.
/// </summary>
public override T Value { get { return value; } }
/// <summary>
/// Returns null.
/// </summary>
public override Exception Exception { get { return null; } }
/// <summary>
/// Returns true.
/// </summary>
public override bool HasValue { get { return true; } }
/// <summary>
/// Returns NotificationKind.OnNext.
/// </summary>
public override NotificationKind Kind { get { return NotificationKind.OnNext; } }
/// <summary>
/// Returns the hash code for this instance.
/// </summary>
public override int GetHashCode()
{
return EqualityComparer<T>.Default.GetHashCode(Value);
}
/// <summary>
/// Indicates whether this instance and a specified object are equal.
/// </summary>
public override bool Equals(Notification<T> other)
{
if (Object.ReferenceEquals(this, other))
return true;
if (Object.ReferenceEquals(other, null))
return false;
if (other.Kind != NotificationKind.OnNext)
return false;
return EqualityComparer<T>.Default.Equals(Value, other.Value);
}
/// <summary>
/// Returns a string representation of this instance.
/// </summary>
public override string ToString()
{
return String.Format(CultureInfo.CurrentCulture, "OnNext({0})", Value);
}
/// <summary>
/// Invokes the observer's method corresponding to the notification.
/// </summary>
/// <param name="observer">Observer to invoke the notification on.</param>
public override void Accept(IObserver<T> observer)
{
if (observer == null)
throw new ArgumentNullException("observer");
observer.OnNext(Value);
}
/// <summary>
/// Invokes the observer's method corresponding to the notification and returns the produced result.
/// </summary>
/// <param name="observer">Observer to invoke the notification on.</param>
/// <returns>Result produced by the observation.</returns>
public override TResult Accept<TResult>(IObserver<T, TResult> observer)
{
if (observer == null)
throw new ArgumentNullException("observer");
return observer.OnNext(Value);
}
/// <summary>
/// Invokes the delegate corresponding to the notification.
/// </summary>
/// <param name="onNext">Delegate to invoke for an OnNext notification.</param>
/// <param name="onError">Delegate to invoke for an OnError notification.</param>
/// <param name="onCompleted">Delegate to invoke for an OnCompleted notification.</param>
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);
}
/// <summary>
/// Invokes the delegate corresponding to the notification and returns the produced result.
/// </summary>
/// <param name="onNext">Delegate to invoke for an OnNext notification.</param>
/// <param name="onError">Delegate to invoke for an OnError notification.</param>
/// <param name="onCompleted">Delegate to invoke for an OnCompleted notification.</param>
/// <returns>Result produced by the observation.</returns>
public override TResult Accept<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(Value);
}
}
/// <summary>
/// Represents an OnError notification to an observer.
/// </summary>
#if !NO_DEBUGGER_ATTRIBUTES
[DebuggerDisplay("OnError({Exception})")]
#endif
#if !NO_SERIALIZABLE
[Serializable]
#endif
internal sealed class OnErrorNotification : Notification<T>
{
Exception exception;
/// <summary>
/// Constructs a notification of an exception.
/// </summary>
public OnErrorNotification(Exception exception)
{
this.exception = exception;
}
/// <summary>
/// Throws the exception.
/// </summary>
public override T Value { get { exception.Throw(); return default(T); } }
/// <summary>
/// Returns the exception.
/// </summary>
public override Exception Exception { get { return exception; } }
/// <summary>
/// Returns false.
/// </summary>
public override bool HasValue { get { return false; } }
/// <summary>
/// Returns NotificationKind.OnError.
/// </summary>
public override NotificationKind Kind { get { return NotificationKind.OnError; } }
/// <summary>
/// Returns the hash code for this instance.
/// </summary>
public override int GetHashCode()
{
return Exception.GetHashCode();
}
/// <summary>
/// Indicates whether this instance and other are equal.
/// </summary>
public override bool Equals(Notification<T> other)
{
if (Object.ReferenceEquals(this, other))
return true;
if (Object.ReferenceEquals(other, null))
return false;
if (other.Kind != NotificationKind.OnError)
return false;
return Object.Equals(Exception, other.Exception);
}
/// <summary>
/// Returns a string representation of this instance.
/// </summary>
public override string ToString()
{
return String.Format(CultureInfo.CurrentCulture, "OnError({0})", Exception.GetType().FullName);
}
/// <summary>
/// Invokes the observer's method corresponding to the notification.
/// </summary>
/// <param name="observer">Observer to invoke the notification on.</param>
public override void Accept(IObserver<T> observer)
{
if (observer == null)
throw new ArgumentNullException("observer");
observer.OnError(Exception);
}
/// <summary>
/// Invokes the observer's method corresponding to the notification and returns the produced result.
/// </summary>
/// <param name="observer">Observer to invoke the notification on.</param>
/// <returns>Result produced by the observation.</returns>
public override TResult Accept<TResult>(IObserver<T, TResult> observer)
{
if (observer == null)
throw new ArgumentNullException("observer");
return observer.OnError(Exception);
}
/// <summary>
/// Invokes the delegate corresponding to the notification.
/// </summary>
/// <param name="onNext">Delegate to invoke for an OnNext notification.</param>
/// <param name="onError">Delegate to invoke for an OnError notification.</param>
/// <param name="onCompleted">Delegate to invoke for an OnCompleted notification.</param>
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);
}
/// <summary>
/// Invokes the delegate corresponding to the notification and returns the produced result.
/// </summary>
/// <param name="onNext">Delegate to invoke for an OnNext notification.</param>
/// <param name="onError">Delegate to invoke for an OnError notification.</param>
/// <param name="onCompleted">Delegate to invoke for an OnCompleted notification.</param>
/// <returns>Result produced by the observation.</returns>
public override TResult Accept<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(Exception);
}
}
/// <summary>
/// Represents an OnCompleted notification to an observer.
/// </summary>
#if !NO_DEBUGGER_ATTRIBUTES
[DebuggerDisplay("OnCompleted()")]
#endif
#if !NO_SERIALIZABLE
[Serializable]
#endif
internal sealed class OnCompletedNotification : Notification<T>
{
/// <summary>
/// Constructs a notification of the end of a sequence.
/// </summary>
public OnCompletedNotification()
{
}
/// <summary>
/// Throws an InvalidOperationException.
/// </summary>
public override T Value { get { throw new InvalidOperationException(Strings_Core.COMPLETED_NO_VALUE); } }
/// <summary>
/// Returns null.
/// </summary>
public override Exception Exception { get { return null; } }
/// <summary>
/// Returns false.
/// </summary>
public override bool HasValue { get { return false; } }
/// <summary>
/// Returns NotificationKind.OnCompleted.
/// </summary>
public override NotificationKind Kind { get { return NotificationKind.OnCompleted; } }
/// <summary>
/// Returns the hash code for this instance.
/// </summary>
public override int GetHashCode()
{
return typeof(T).GetHashCode() ^ 8510;
}
/// <summary>
/// Indicates whether this instance and other are equal.
/// </summary>
public override bool Equals(Notification<T> other)
{
if (Object.ReferenceEquals(this, other))
return true;
if (Object.ReferenceEquals(other, null))
return false;
return other.Kind == NotificationKind.OnCompleted;
}
/// <summary>
/// Returns a string representation of this instance.
/// </summary>
public override string ToString()
{
return "OnCompleted()";
}
/// <summary>
/// Invokes the observer's method corresponding to the notification.
/// </summary>
/// <param name="observer">Observer to invoke the notification on.</param>
public override void Accept(IObserver<T> observer)
{
if (observer == null)
throw new ArgumentNullException("observer");
observer.OnCompleted();
}
/// <summary>
/// Invokes the observer's method corresponding to the notification and returns the produced result.
/// </summary>
/// <param name="observer">Observer to invoke the notification on.</param>
/// <returns>Result produced by the observation.</returns>
public override TResult Accept<TResult>(IObserver<T, TResult> observer)
{
if (observer == null)
throw new ArgumentNullException("observer");
return observer.OnCompleted();
}
/// <summary>
/// Invokes the delegate corresponding to the notification.
/// </summary>
/// <param name="onNext">Delegate to invoke for an OnNext notification.</param>
/// <param name="onError">Delegate to invoke for an OnError notification.</param>
/// <param name="onCompleted">Delegate to invoke for an OnCompleted notification.</param>
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();
}
/// <summary>
/// Invokes the delegate corresponding to the notification and returns the produced result.
/// </summary>
/// <param name="onNext">Delegate to invoke for an OnNext notification.</param>
/// <param name="onError">Delegate to invoke for an OnError notification.</param>
/// <param name="onCompleted">Delegate to invoke for an OnCompleted notification.</param>
/// <returns>Result produced by the observation.</returns>
public override TResult Accept<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();
}
}
/// <summary>
/// Determines whether the current Notification<T> object has the same observer message payload as a specified Notification<T> value.
/// </summary>
/// <param name="other">An object to compare to the current Notification<T> object.</param>
/// <returns>true if both Notification<T> objects have the same observer message payload; otherwise, false.</returns>
/// <remarks>
/// Equality of Notification<T> objects is based on the equality of the observer message payload they represent, including the notification Kind and the Value or Exception (if any).
/// This means two Notification<T> objects can be equal even though they don't represent the same observer method call, but have the same Kind and have equal parameters passed to the observer method.
/// In case one wants to determine whether two Notification<T> objects represent the same observer method call, use Object.ReferenceEquals identity equality instead.
/// </remarks>
public abstract bool Equals(Notification<T> other);
/// <summary>
/// Determines whether the two specified Notification<T> objects have the same observer message payload.
/// </summary>
/// <param name="left">The first Notification<T> to compare, or null.</param>
/// <param name="right">The second Notification<T> to compare, or null.</param>
/// <returns>true if the first Notification<T> value has the same observer message payload as the second Notification<T> value; otherwise, false.</returns>
/// <remarks>
/// Equality of Notification<T> objects is based on the equality of the observer message payload they represent, including the notification Kind and the Value or Exception (if any).
/// This means two Notification<T> objects can be equal even though they don't represent the same observer method call, but have the same Kind and have equal parameters passed to the observer method.
/// In case one wants to determine whether two Notification<T> objects represent the same observer method call, use Object.ReferenceEquals identity equality instead.
/// </remarks>
public static bool operator ==(Notification<T> left, Notification<T> right)
{
if (object.ReferenceEquals(left, right))
return true;
if ((object)left == null || (object)right == null)
return false;
return left.Equals(right);
}
/// <summary>
/// Determines whether the two specified Notification<T> objects have a different observer message payload.
/// </summary>
/// <param name="left">The first Notification<T> to compare, or null.</param>
/// <param name="right">The second Notification<T> to compare, or null.</param>
/// <returns>true if the first Notification<T> value has a different observer message payload as the second Notification<T> value; otherwise, false.</returns>
/// <remarks>
/// Equality of Notification<T> objects is based on the equality of the observer message payload they represent, including the notification Kind and the Value or Exception (if any).
/// This means two Notification<T> objects can be equal even though they don't represent the same observer method call, but have the same Kind and have equal parameters passed to the observer method.
/// In case one wants to determine whether two Notification<T> objects represent a different observer method call, use Object.ReferenceEquals identity equality instead.
/// </remarks>
public static bool operator !=(Notification<T> left, Notification<T> right)
{
return !(left == right);
}
/// <summary>
/// Determines whether the specified System.Object is equal to the current Notification<T>.
/// </summary>
/// <param name="obj">The System.Object to compare with the current Notification<T>.</param>
/// <returns>true if the specified System.Object is equal to the current Notification<T>; otherwise, false.</returns>
/// <remarks>
/// Equality of Notification<T> objects is based on the equality of the observer message payload they represent, including the notification Kind and the Value or Exception (if any).
/// This means two Notification<T> objects can be equal even though they don't represent the same observer method call, but have the same Kind and have equal parameters passed to the observer method.
/// In case one wants to determine whether two Notification<T> objects represent the same observer method call, use Object.ReferenceEquals identity equality instead.
/// </remarks>
public override bool Equals(object obj)
{
return Equals(obj as Notification<T>);
}
/// <summary>
/// Invokes the observer's method corresponding to the notification.
/// </summary>
/// <param name="observer">Observer to invoke the notification on.</param>
public abstract void Accept(IObserver<T> observer);
/// <summary>
/// Invokes the observer's method corresponding to the notification and returns the produced result.
/// </summary>
/// <typeparam name="TResult">The type of the result returned from the observer's notification handlers.</typeparam>
/// <param name="observer">Observer to invoke the notification on.</param>
/// <returns>Result produced by the observation.</returns>
public abstract TResult Accept<TResult>(IObserver<T, TResult> observer);
/// <summary>
/// Invokes the delegate corresponding to the notification.
/// </summary>
/// <param name="onNext">Delegate to invoke for an OnNext notification.</param>
/// <param name="onError">Delegate to invoke for an OnError notification.</param>
/// <param name="onCompleted">Delegate to invoke for an OnCompleted notification.</param>
public abstract void Accept(Action<T> onNext, Action<Exception> onError, Action onCompleted);
/// <summary>
/// Invokes the delegate corresponding to the notification and returns the produced result.
/// </summary>
/// <typeparam name="TResult">The type of the result returned from the notification handler delegates.</typeparam>
/// <param name="onNext">Delegate to invoke for an OnNext notification.</param>
/// <param name="onError">Delegate to invoke for an OnError notification.</param>
/// <param name="onCompleted">Delegate to invoke for an OnCompleted notification.</param>
/// <returns>Result produced by the observation.</returns>
public abstract TResult Accept<TResult>(Func<T, TResult> onNext, Func<Exception, TResult> onError, Func<TResult> onCompleted);
/// <summary>
/// Returns an observable sequence with a single notification, using the immediate scheduler.
/// </summary>
/// <returns>The observable sequence that surfaces the behavior of the notification upon subscription.</returns>
public IObservable<T> ToObservable()
{
return this.ToObservable(ImmediateScheduler.Instance);
}
/// <summary>
/// Returns an observable sequence with a single notification.
/// </summary>
/// <param name="scheduler">Scheduler to send out the notification calls on.</param>
/// <returns>The observable sequence that surfaces the behavior of the notification upon subscription.</returns>
public IObservable<T> ToObservable(IScheduler scheduler)
{
if (scheduler == null)
throw new ArgumentNullException("scheduler");
return new AnonymousObservable<T>(observer => scheduler.Schedule(() =>
{
this.Accept(observer);
if (this.Kind == NotificationKind.OnNext)
observer.OnCompleted();
}));
}
}
/// <summary>
/// Provides a set of static methods for constructing notifications.
/// </summary>
public static class Notification
{
/// <summary>
/// Creates an object that represents an OnNext notification to an observer.
/// </summary>
/// <typeparam name="T">The type of the elements received by the observer. Upon dematerialization of the notifications into an observable sequence, this type is used as the element type for the sequence.</typeparam>
/// <param name="value">The value contained in the notification.</param>
/// <returns>The OnNext notification containing the value.</returns>
public static Notification<T> CreateOnNext<T>(T value)
{
return new Notification<T>.OnNextNotification(value);
}
/// <summary>
/// Creates an object that represents an OnError notification to an observer.
/// </summary>
/// <typeparam name="T">The type of the elements received by the observer. Upon dematerialization of the notifications into an observable sequence, this type is used as the element type for the sequence.</typeparam>
/// <param name="error">The exception contained in the notification.</param>
/// <returns>The OnError notification containing the exception.</returns>
/// <exception cref="ArgumentNullException"><paramref name="error"/> is null.</exception>
public static Notification<T> CreateOnError<T>(Exception error)
{
if (error == null)
throw new ArgumentNullException("error");
return new Notification<T>.OnErrorNotification(error);
}
/// <summary>
/// Creates an object that represents an OnCompleted notification to an observer.
/// </summary>
/// <typeparam name="T">The type of the elements received by the observer. Upon dematerialization of the notifications into an observable sequence, this type is used as the element type for the sequence.</typeparam>
/// <returns>The OnCompleted notification.</returns>
public static Notification<T> CreateOnCompleted<T>()
{
return new Notification<T>.OnCompletedNotification();
}
}
}
#pragma warning restore 0659
#pragma warning restore 0661
|