1
0

Notification.cs 29 KB

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