Notification.cs 29 KB

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