1
0

Observable.Creation.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. using System.Collections.Generic;
  3. using System.Reactive.Concurrency;
  4. using System.Threading;
  5. #if !NO_TPL
  6. using System.Threading.Tasks;
  7. #endif
  8. namespace System.Reactive.Linq
  9. {
  10. public static partial class Observable
  11. {
  12. #region + Create +
  13. /// <summary>
  14. /// Creates an observable sequence from a specified Subscribe method implementation.
  15. /// </summary>
  16. /// <typeparam name="TResult">The type of the elements in the produced sequence.</typeparam>
  17. /// <param name="subscribe">Implementation of the resulting observable sequence's Subscribe method.</param>
  18. /// <returns>The observable sequence with the specified implementation for the Subscribe method.</returns>
  19. /// <exception cref="ArgumentNullException"><paramref name="subscribe"/> is null.</exception>
  20. /// <remarks>
  21. /// Use of this operator is preferred over manual implementation of the IObservable&lt;T&gt; interface. In case
  22. /// you need a type implementing IObservable&lt;T&gt; rather than an anonymous implementation, consider using
  23. /// the <see cref="System.Reactive.ObservableBase&lt;T&gt;"/> abstract base class.
  24. /// </remarks>
  25. public static IObservable<TResult> Create<TResult>(Func<IObserver<TResult>, IDisposable> subscribe)
  26. {
  27. if (subscribe == null)
  28. throw new ArgumentNullException("subscribe");
  29. return s_impl.Create<TResult>(subscribe);
  30. }
  31. /// <summary>
  32. /// Creates an observable sequence from a specified Subscribe method implementation.
  33. /// </summary>
  34. /// <typeparam name="TResult">The type of the elements in the produced sequence.</typeparam>
  35. /// <param name="subscribe">Implementation of the resulting observable sequence's Subscribe method, returning an Action delegate that will be wrapped in an IDisposable.</param>
  36. /// <returns>The observable sequence with the specified implementation for the Subscribe method.</returns>
  37. /// <exception cref="ArgumentNullException"><paramref name="subscribe"/> is null.</exception>
  38. /// <remarks>
  39. /// Use of this operator is preferred over manual implementation of the IObservable&lt;T&gt; interface. In case
  40. /// you need a type implementing IObservable&lt;T&gt; rather than an anonymous implementation, consider using
  41. /// the <see cref="System.Reactive.ObservableBase&lt;T&gt;"/> abstract base class.
  42. /// </remarks>
  43. public static IObservable<TResult> Create<TResult>(Func<IObserver<TResult>, Action> subscribe)
  44. {
  45. if (subscribe == null)
  46. throw new ArgumentNullException("subscribe");
  47. return s_impl.Create<TResult>(subscribe);
  48. }
  49. #endregion
  50. #region + CreateAsync +
  51. #if !NO_TPL
  52. /// <summary>
  53. /// Creates an observable sequence from a specified cancellable asynchronous Subscribe method.
  54. /// The CancellationToken passed to the asynchronous Subscribe method is tied to the returned disposable subscription, allowing best-effort cancellation.
  55. /// </summary>
  56. /// <typeparam name="TResult">The type of the elements in the produced sequence.</typeparam>
  57. /// <param name="subscribeAsync">Asynchronous method used to produce elements.</param>
  58. /// <returns>The observable sequence surfacing the elements produced by the asynchronous method.</returns>
  59. /// <exception cref="ArgumentNullException"><paramref name="subscribeAsync"/> is null.</exception>
  60. /// <remarks>This operator is especially useful in conjunction with the asynchronous programming features introduced in C# 5.0 and Visual Basic 11.</remarks>
  61. /// <remarks>When a subscription to the resulting sequence is disposed, the CancellationToken that was fed to the asynchronous subscribe function will be signaled.</remarks>
  62. public static IObservable<TResult> Create<TResult>(Func<IObserver<TResult>, CancellationToken, Task> subscribeAsync)
  63. {
  64. if (subscribeAsync == null)
  65. throw new ArgumentNullException("subscribeAsync");
  66. return s_impl.Create<TResult>(subscribeAsync);
  67. }
  68. /// <summary>
  69. /// Creates an observable sequence from a specified asynchronous Subscribe method.
  70. /// </summary>
  71. /// <typeparam name="TResult">The type of the elements in the produced sequence.</typeparam>
  72. /// <param name="subscribeAsync">Asynchronous method used to produce elements.</param>
  73. /// <returns>The observable sequence surfacing the elements produced by the asynchronous method.</returns>
  74. /// <exception cref="ArgumentNullException"><paramref name="subscribeAsync"/> is null.</exception>
  75. /// <remarks>This operator is especially useful in conjunction with the asynchronous programming features introduced in C# 5.0 and Visual Basic 11.</remarks>
  76. public static IObservable<TResult> Create<TResult>(Func<IObserver<TResult>, Task> subscribeAsync)
  77. {
  78. if (subscribeAsync == null)
  79. throw new ArgumentNullException("subscribeAsync");
  80. return s_impl.Create<TResult>(subscribeAsync);
  81. }
  82. /// <summary>
  83. /// Creates an observable sequence from a specified cancellable asynchronous Subscribe method.
  84. /// The CancellationToken passed to the asynchronous Subscribe method is tied to the returned disposable subscription, allowing best-effort cancellation.
  85. /// </summary>
  86. /// <typeparam name="TResult">The type of the elements in the produced sequence.</typeparam>
  87. /// <param name="subscribeAsync">Asynchronous method used to implement the resulting sequence's Subscribe method.</param>
  88. /// <returns>The observable sequence with the specified implementation for the Subscribe method.</returns>
  89. /// <exception cref="ArgumentNullException"><paramref name="subscribeAsync"/> is null.</exception>
  90. /// <remarks>This operator is especially useful in conjunction with the asynchronous programming features introduced in C# 5.0 and Visual Basic 11.</remarks>
  91. /// <remarks>When a subscription to the resulting sequence is disposed, the CancellationToken that was fed to the asynchronous subscribe function will be signaled.</remarks>
  92. public static IObservable<TResult> Create<TResult>(Func<IObserver<TResult>, CancellationToken, Task<IDisposable>> subscribeAsync)
  93. {
  94. if (subscribeAsync == null)
  95. throw new ArgumentNullException("subscribeAsync");
  96. return s_impl.Create<TResult>(subscribeAsync);
  97. }
  98. /// <summary>
  99. /// Creates an observable sequence from a specified asynchronous Subscribe method.
  100. /// </summary>
  101. /// <typeparam name="TResult">The type of the elements in the produced sequence.</typeparam>
  102. /// <param name="subscribeAsync">Asynchronous method used to implement the resulting sequence's Subscribe method.</param>
  103. /// <returns>The observable sequence with the specified implementation for the Subscribe method.</returns>
  104. /// <exception cref="ArgumentNullException"><paramref name="subscribeAsync"/> is null.</exception>
  105. /// <remarks>This operator is especially useful in conjunction with the asynchronous programming features introduced in C# 5.0 and Visual Basic 11.</remarks>
  106. public static IObservable<TResult> Create<TResult>(Func<IObserver<TResult>, Task<IDisposable>> subscribeAsync)
  107. {
  108. if (subscribeAsync == null)
  109. throw new ArgumentNullException("subscribeAsync");
  110. return s_impl.Create<TResult>(subscribeAsync);
  111. }
  112. /// <summary>
  113. /// Creates an observable sequence from a specified cancellable asynchronous Subscribe method.
  114. /// The CancellationToken passed to the asynchronous Subscribe method is tied to the returned disposable subscription, allowing best-effort cancellation.
  115. /// </summary>
  116. /// <typeparam name="TResult">The type of the elements in the produced sequence.</typeparam>
  117. /// <param name="subscribeAsync">Asynchronous method used to implement the resulting sequence's Subscribe method, returning an Action delegate that will be wrapped in an IDisposable.</param>
  118. /// <returns>The observable sequence with the specified implementation for the Subscribe method.</returns>
  119. /// <exception cref="ArgumentNullException"><paramref name="subscribeAsync"/> is null.</exception>
  120. /// <remarks>This operator is especially useful in conjunction with the asynchronous programming features introduced in C# 5.0 and Visual Basic 11.</remarks>
  121. /// <remarks>When a subscription to the resulting sequence is disposed, the CancellationToken that was fed to the asynchronous subscribe function will be signaled.</remarks>
  122. public static IObservable<TResult> Create<TResult>(Func<IObserver<TResult>, CancellationToken, Task<Action>> subscribeAsync)
  123. {
  124. if (subscribeAsync == null)
  125. throw new ArgumentNullException("subscribeAsync");
  126. return s_impl.Create<TResult>(subscribeAsync);
  127. }
  128. /// <summary>
  129. /// Creates an observable sequence from a specified asynchronous Subscribe method.
  130. /// </summary>
  131. /// <typeparam name="TResult">The type of the elements in the produced sequence.</typeparam>
  132. /// <param name="subscribeAsync">Asynchronous method used to implement the resulting sequence's Subscribe method, returning an Action delegate that will be wrapped in an IDisposable.</param>
  133. /// <returns>The observable sequence with the specified implementation for the Subscribe method.</returns>
  134. /// <exception cref="ArgumentNullException"><paramref name="subscribeAsync"/> is null.</exception>
  135. /// <remarks>This operator is especially useful in conjunction with the asynchronous programming features introduced in C# 5.0 and Visual Basic 11.</remarks>
  136. public static IObservable<TResult> Create<TResult>(Func<IObserver<TResult>, Task<Action>> subscribeAsync)
  137. {
  138. if (subscribeAsync == null)
  139. throw new ArgumentNullException("subscribeAsync");
  140. return s_impl.Create<TResult>(subscribeAsync);
  141. }
  142. #endif
  143. #endregion
  144. #region + Defer +
  145. /// <summary>
  146. /// Returns an observable sequence that invokes the specified factory function whenever a new observer subscribes.
  147. /// </summary>
  148. /// <typeparam name="TResult">The type of the elements in the sequence returned by the factory function, and in the resulting sequence.</typeparam>
  149. /// <param name="observableFactory">Observable factory function to invoke for each observer that subscribes to the resulting sequence.</param>
  150. /// <returns>An observable sequence whose observers trigger an invocation of the given observable factory function.</returns>
  151. /// <exception cref="ArgumentNullException"><paramref name="observableFactory"/> is null.</exception>
  152. public static IObservable<TResult> Defer<TResult>(Func<IObservable<TResult>> observableFactory)
  153. {
  154. if (observableFactory == null)
  155. throw new ArgumentNullException("observableFactory");
  156. return s_impl.Defer<TResult>(observableFactory);
  157. }
  158. #endregion
  159. #region + DeferAsync +
  160. #if !NO_TPL
  161. /// <summary>
  162. /// Returns an observable sequence that starts the specified asynchronous factory function whenever a new observer subscribes.
  163. /// </summary>
  164. /// <typeparam name="TResult">The type of the elements in the sequence returned by the factory function, and in the resulting sequence.</typeparam>
  165. /// <param name="observableFactoryAsync">Asynchronous factory function to start for each observer that subscribes to the resulting sequence.</param>
  166. /// <returns>An observable sequence whose observers trigger the given asynchronous observable factory function to be started.</returns>
  167. /// <exception cref="ArgumentNullException"><paramref name="observableFactoryAsync"/> is null.</exception>
  168. /// <remarks>This operator is especially useful in conjunction with the asynchronous programming features introduced in C# 5.0 and Visual Basic 11.</remarks>
  169. public static IObservable<TResult> Defer<TResult>(Func<Task<IObservable<TResult>>> observableFactoryAsync)
  170. {
  171. if (observableFactoryAsync == null)
  172. throw new ArgumentNullException("observableFactoryAsync");
  173. return s_impl.Defer<TResult>(observableFactoryAsync);
  174. }
  175. /// <summary>
  176. /// Returns an observable sequence that starts the specified cancellable asynchronous factory function whenever a new observer subscribes.
  177. /// The CancellationToken passed to the asynchronous factory function is tied to the returned disposable subscription, allowing best-effort cancellation.
  178. /// </summary>
  179. /// <typeparam name="TResult">The type of the elements in the sequence returned by the factory function, and in the resulting sequence.</typeparam>
  180. /// <param name="observableFactoryAsync">Asynchronous factory function to start for each observer that subscribes to the resulting sequence.</param>
  181. /// <returns>An observable sequence whose observers trigger the given asynchronous observable factory function to be started.</returns>
  182. /// <exception cref="ArgumentNullException"><paramref name="observableFactoryAsync"/> is null.</exception>
  183. /// <remarks>This operator is especially useful in conjunction with the asynchronous programming features introduced in C# 5.0 and Visual Basic 11.</remarks>
  184. /// <remarks>When a subscription to the resulting sequence is disposed, the CancellationToken that was fed to the asynchronous observable factory function will be signaled.</remarks>
  185. public static IObservable<TResult> DeferAsync<TResult>(Func<CancellationToken, Task<IObservable<TResult>>> observableFactoryAsync)
  186. {
  187. if (observableFactoryAsync == null)
  188. throw new ArgumentNullException("observableFactoryAsync");
  189. return s_impl.Defer<TResult>(observableFactoryAsync);
  190. }
  191. #endif
  192. #endregion
  193. #region + Empty +
  194. /// <summary>
  195. /// Returns an empty observable sequence.
  196. /// </summary>
  197. /// <typeparam name="TResult">The type used for the IObservable&lt;T&gt; type parameter of the resulting sequence.</typeparam>
  198. /// <returns>An observable sequence with no elements.</returns>
  199. public static IObservable<TResult> Empty<TResult>()
  200. {
  201. return s_impl.Empty<TResult>();
  202. }
  203. /// <summary>
  204. /// Returns an empty observable sequence.
  205. /// </summary>
  206. /// <typeparam name="TResult">The type used for the IObservable&lt;T&gt; type parameter of the resulting sequence.</typeparam>
  207. /// <param name="witness">Object solely used to infer the type of the <typeparamref name="TResult"/> type parameter. This parameter is typically used when creating a sequence of anonymously typed elements.</param>
  208. /// <returns>An observable sequence with no elements.</returns>
  209. public static IObservable<TResult> Empty<TResult>(TResult witness)
  210. {
  211. return s_impl.Empty<TResult>(); // Pure inference - no specialized target method.
  212. }
  213. /// <summary>
  214. /// Returns an empty observable sequence, using the specified scheduler to send out the single OnCompleted message.
  215. /// </summary>
  216. /// <typeparam name="TResult">The type used for the IObservable&lt;T&gt; type parameter of the resulting sequence.</typeparam>
  217. /// <param name="scheduler">Scheduler to send the termination call on.</param>
  218. /// <returns>An observable sequence with no elements.</returns>
  219. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> is null.</exception>
  220. public static IObservable<TResult> Empty<TResult>(IScheduler scheduler)
  221. {
  222. if (scheduler == null)
  223. throw new ArgumentNullException("scheduler");
  224. return s_impl.Empty<TResult>(scheduler);
  225. }
  226. /// <summary>
  227. /// Returns an empty observable sequence, using the specified scheduler to send out the single OnCompleted message.
  228. /// </summary>
  229. /// <typeparam name="TResult">The type used for the IObservable&lt;T&gt; type parameter of the resulting sequence.</typeparam>
  230. /// <param name="scheduler">Scheduler to send the termination call on.</param>
  231. /// <param name="witness">Object solely used to infer the type of the <typeparamref name="TResult"/> type parameter. This parameter is typically used when creating a sequence of anonymously typed elements.</param>
  232. /// <returns>An observable sequence with no elements.</returns>
  233. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> is null.</exception>
  234. public static IObservable<TResult> Empty<TResult>(IScheduler scheduler, TResult witness)
  235. {
  236. if (scheduler == null)
  237. throw new ArgumentNullException("scheduler");
  238. return s_impl.Empty<TResult>(scheduler); // Pure inference - no specialized target method.
  239. }
  240. #endregion
  241. #region + Generate +
  242. /// <summary>
  243. /// Generates an observable sequence by running a state-driven loop producing the sequence's elements.
  244. /// </summary>
  245. /// <typeparam name="TState">The type of the state used in the generator loop.</typeparam>
  246. /// <typeparam name="TResult">The type of the elements in the produced sequence.</typeparam>
  247. /// <param name="initialState">Initial state.</param>
  248. /// <param name="condition">Condition to terminate generation (upon returning false).</param>
  249. /// <param name="iterate">Iteration step function.</param>
  250. /// <param name="resultSelector">Selector function for results produced in the sequence.</param>
  251. /// <returns>The generated sequence.</returns>
  252. /// <exception cref="ArgumentNullException"><paramref name="condition"/> or <paramref name="iterate"/> or <paramref name="resultSelector"/> is null.</exception>
  253. public static IObservable<TResult> Generate<TState, TResult>(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector)
  254. {
  255. if (condition == null)
  256. throw new ArgumentNullException("condition");
  257. if (iterate == null)
  258. throw new ArgumentNullException("iterate");
  259. if (resultSelector == null)
  260. throw new ArgumentNullException("resultSelector");
  261. return s_impl.Generate<TState, TResult>(initialState, condition, iterate, resultSelector);
  262. }
  263. /// <summary>
  264. /// Generates an observable sequence by running a state-driven loop producing the sequence's elements, using the specified scheduler to send out observer messages.
  265. /// </summary>
  266. /// <typeparam name="TState">The type of the state used in the generator loop.</typeparam>
  267. /// <typeparam name="TResult">The type of the elements in the produced sequence.</typeparam>
  268. /// <param name="initialState">Initial state.</param>
  269. /// <param name="condition">Condition to terminate generation (upon returning false).</param>
  270. /// <param name="iterate">Iteration step function.</param>
  271. /// <param name="resultSelector">Selector function for results produced in the sequence.</param>
  272. /// <param name="scheduler">Scheduler on which to run the generator loop.</param>
  273. /// <returns>The generated sequence.</returns>
  274. /// <exception cref="ArgumentNullException"><paramref name="condition"/> or <paramref name="iterate"/> or <paramref name="resultSelector"/> or <paramref name="scheduler"/> is null.</exception>
  275. public static IObservable<TResult> Generate<TState, TResult>(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector, IScheduler scheduler)
  276. {
  277. if (condition == null)
  278. throw new ArgumentNullException("condition");
  279. if (iterate == null)
  280. throw new ArgumentNullException("iterate");
  281. if (resultSelector == null)
  282. throw new ArgumentNullException("resultSelector");
  283. if (scheduler == null)
  284. throw new ArgumentNullException("scheduler");
  285. return s_impl.Generate<TState, TResult>(initialState, condition, iterate, resultSelector, scheduler);
  286. }
  287. #endregion
  288. #region + Never +
  289. /// <summary>
  290. /// Returns a non-terminating observable sequence, which can be used to denote an infinite duration (e.g. when using reactive joins).
  291. /// </summary>
  292. /// <typeparam name="TResult">The type used for the IObservable&lt;T&gt; type parameter of the resulting sequence.</typeparam>
  293. /// <returns>An observable sequence whose observers will never get called.</returns>
  294. public static IObservable<TResult> Never<TResult>()
  295. {
  296. return s_impl.Never<TResult>();
  297. }
  298. /// <summary>
  299. /// Returns a non-terminating observable sequence, which can be used to denote an infinite duration (e.g. when using reactive joins).
  300. /// </summary>
  301. /// <typeparam name="TResult">The type used for the IObservable&lt;T&gt; type parameter of the resulting sequence.</typeparam>
  302. /// <param name="witness">Object solely used to infer the type of the <typeparamref name="TResult"/> type parameter. This parameter is typically used when creating a sequence of anonymously typed elements.</param>
  303. /// <returns>An observable sequence whose observers will never get called.</returns>
  304. public static IObservable<TResult> Never<TResult>(TResult witness)
  305. {
  306. return s_impl.Never<TResult>(); // Pure inference - no specialized target method.
  307. }
  308. #endregion
  309. #region + Range +
  310. /// <summary>
  311. /// Generates an observable sequence of integral numbers within a specified range.
  312. /// </summary>
  313. /// <param name="start">The value of the first integer in the sequence.</param>
  314. /// <param name="count">The number of sequential integers to generate.</param>
  315. /// <returns>An observable sequence that contains a range of sequential integral numbers.</returns>
  316. /// <exception cref="ArgumentOutOfRangeException"><paramref name="count"/> is less than zero. -or- <paramref name="start"/> + <paramref name="count"/> - 1 is larger than <see cref="M:System.Int32.MaxValue"/>.</exception>
  317. public static IObservable<int> Range(int start, int count)
  318. {
  319. var max = ((long)start) + count - 1;
  320. if (count < 0 || max > int.MaxValue)
  321. throw new ArgumentOutOfRangeException("count");
  322. return s_impl.Range(start, count);
  323. }
  324. /// <summary>
  325. /// Generates an observable sequence of integral numbers within a specified range, using the specified scheduler to send out observer messages.
  326. /// </summary>
  327. /// <param name="start">The value of the first integer in the sequence.</param>
  328. /// <param name="count">The number of sequential integers to generate.</param>
  329. /// <param name="scheduler">Scheduler to run the generator loop on.</param>
  330. /// <returns>An observable sequence that contains a range of sequential integral numbers.</returns>
  331. /// <exception cref="ArgumentOutOfRangeException"><paramref name="count"/> is less than zero. -or- <paramref name="start"/> + <paramref name="count"/> - 1 is larger than <see cref="M:System.Int32.MaxValue"/>.</exception>
  332. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> is null.</exception>
  333. public static IObservable<int> Range(int start, int count, IScheduler scheduler)
  334. {
  335. var max = ((long)start) + count - 1;
  336. if (count < 0 || max > int.MaxValue)
  337. throw new ArgumentOutOfRangeException("count");
  338. if (scheduler == null)
  339. throw new ArgumentNullException("scheduler");
  340. return s_impl.Range(start, count, scheduler);
  341. }
  342. #endregion
  343. #region + Repeat +
  344. /// <summary>
  345. /// Generates an observable sequence that repeats the given element infinitely.
  346. /// </summary>
  347. /// <typeparam name="TResult">The type of the element that will be repeated in the produced sequence.</typeparam>
  348. /// <param name="value">Element to repeat.</param>
  349. /// <returns>An observable sequence that repeats the given element infinitely.</returns>
  350. public static IObservable<TResult> Repeat<TResult>(TResult value)
  351. {
  352. return s_impl.Repeat<TResult>(value);
  353. }
  354. /// <summary>
  355. /// Generates an observable sequence that repeats the given element infinitely, using the specified scheduler to send out observer messages.
  356. /// </summary>
  357. /// <typeparam name="TResult">The type of the element that will be repeated in the produced sequence.</typeparam>
  358. /// <param name="value">Element to repeat.</param>
  359. /// <param name="scheduler">Scheduler to run the producer loop on.</param>
  360. /// <returns>An observable sequence that repeats the given element infinitely.</returns>
  361. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> is null.</exception>
  362. public static IObservable<TResult> Repeat<TResult>(TResult value, IScheduler scheduler)
  363. {
  364. if (scheduler == null)
  365. throw new ArgumentNullException("scheduler");
  366. return s_impl.Repeat<TResult>(value, scheduler);
  367. }
  368. /// <summary>
  369. /// Generates an observable sequence that repeats the given element the specified number of times.
  370. /// </summary>
  371. /// <typeparam name="TResult">The type of the element that will be repeated in the produced sequence.</typeparam>
  372. /// <param name="value">Element to repeat.</param>
  373. /// <param name="repeatCount">Number of times to repeat the element.</param>
  374. /// <returns>An observable sequence that repeats the given element the specified number of times.</returns>
  375. /// <exception cref="ArgumentOutOfRangeException"><paramref name="repeatCount"/> is less than zero.</exception>
  376. public static IObservable<TResult> Repeat<TResult>(TResult value, int repeatCount)
  377. {
  378. if (repeatCount < 0)
  379. throw new ArgumentOutOfRangeException("repeatCount");
  380. return s_impl.Repeat<TResult>(value, repeatCount);
  381. }
  382. /// <summary>
  383. /// Generates an observable sequence that repeats the given element the specified number of times, using the specified scheduler to send out observer messages.
  384. /// </summary>
  385. /// <typeparam name="TResult">The type of the element that will be repeated in the produced sequence.</typeparam>
  386. /// <param name="value">Element to repeat.</param>
  387. /// <param name="repeatCount">Number of times to repeat the element.</param>
  388. /// <param name="scheduler">Scheduler to run the producer loop on.</param>
  389. /// <returns>An observable sequence that repeats the given element the specified number of times.</returns>
  390. /// <exception cref="ArgumentOutOfRangeException"><paramref name="repeatCount"/> is less than zero.</exception>
  391. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> is null.</exception>
  392. public static IObservable<TResult> Repeat<TResult>(TResult value, int repeatCount, IScheduler scheduler)
  393. {
  394. if (repeatCount < 0)
  395. throw new ArgumentOutOfRangeException("repeatCount");
  396. if (scheduler == null)
  397. throw new ArgumentNullException("scheduler");
  398. return s_impl.Repeat<TResult>(value, repeatCount, scheduler);
  399. }
  400. #endregion
  401. #region + Return +
  402. /// <summary>
  403. /// Returns an observable sequence that contains a single element.
  404. /// </summary>
  405. /// <typeparam name="TResult">The type of the element that will be returned in the produced sequence.</typeparam>
  406. /// <param name="value">Single element in the resulting observable sequence.</param>
  407. /// <returns>An observable sequence containing the single specified element.</returns>
  408. public static IObservable<TResult> Return<TResult>(TResult value)
  409. {
  410. return s_impl.Return<TResult>(value);
  411. }
  412. /// <summary>
  413. /// Returns an observable sequence that contains a single element, using the specified scheduler to send out observer messages.
  414. /// </summary>
  415. /// <typeparam name="TResult">The type of the element that will be returned in the produced sequence.</typeparam>
  416. /// <param name="value">Single element in the resulting observable sequence.</param>
  417. /// <param name="scheduler">Scheduler to send the single element on.</param>
  418. /// <returns>An observable sequence containing the single specified element.</returns>
  419. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> is null.</exception>
  420. public static IObservable<TResult> Return<TResult>(TResult value, IScheduler scheduler)
  421. {
  422. if (scheduler == null)
  423. throw new ArgumentNullException("scheduler");
  424. return s_impl.Return<TResult>(value, scheduler);
  425. }
  426. #endregion
  427. #region + Throw +
  428. /// <summary>
  429. /// Returns an observable sequence that terminates with an exception.
  430. /// </summary>
  431. /// <typeparam name="TResult">The type used for the IObservable&lt;T&gt; type parameter of the resulting sequence.</typeparam>
  432. /// <param name="exception">Exception object used for the sequence's termination.</param>
  433. /// <returns>The observable sequence that terminates exceptionally with the specified exception object.</returns>
  434. /// <exception cref="ArgumentNullException"><paramref name="exception"/> is null.</exception>
  435. public static IObservable<TResult> Throw<TResult>(Exception exception)
  436. {
  437. if (exception == null)
  438. throw new ArgumentNullException("exception");
  439. return s_impl.Throw<TResult>(exception);
  440. }
  441. /// <summary>
  442. /// Returns an observable sequence that terminates with an exception.
  443. /// </summary>
  444. /// <typeparam name="TResult">The type used for the IObservable&lt;T&gt; type parameter of the resulting sequence.</typeparam>
  445. /// <param name="exception">Exception object used for the sequence's termination.</param>
  446. /// <param name="witness">Object solely used to infer the type of the <typeparamref name="TResult"/> type parameter. This parameter is typically used when creating a sequence of anonymously typed elements.</param>
  447. /// <returns>The observable sequence that terminates exceptionally with the specified exception object.</returns>
  448. /// <exception cref="ArgumentNullException"><paramref name="exception"/> is null.</exception>
  449. public static IObservable<TResult> Throw<TResult>(Exception exception, TResult witness)
  450. {
  451. if (exception == null)
  452. throw new ArgumentNullException("exception");
  453. return s_impl.Throw<TResult>(exception); // Pure inference - no specialized target method.
  454. }
  455. /// <summary>
  456. /// Returns an observable sequence that terminates with an exception, using the specified scheduler to send out the single OnError message.
  457. /// </summary>
  458. /// <typeparam name="TResult">The type used for the IObservable&lt;T&gt; type parameter of the resulting sequence.</typeparam>
  459. /// <param name="exception">Exception object used for the sequence's termination.</param>
  460. /// <param name="scheduler">Scheduler to send the exceptional termination call on.</param>
  461. /// <returns>The observable sequence that terminates exceptionally with the specified exception object.</returns>
  462. /// <exception cref="ArgumentNullException"><paramref name="exception"/> or <paramref name="scheduler"/> is null.</exception>
  463. public static IObservable<TResult> Throw<TResult>(Exception exception, IScheduler scheduler)
  464. {
  465. if (exception == null)
  466. throw new ArgumentNullException("exception");
  467. if (scheduler == null)
  468. throw new ArgumentNullException("scheduler");
  469. return s_impl.Throw<TResult>(exception, scheduler);
  470. }
  471. /// <summary>
  472. /// Returns an observable sequence that terminates with an exception, using the specified scheduler to send out the single OnError message.
  473. /// </summary>
  474. /// <typeparam name="TResult">The type used for the IObservable&lt;T&gt; type parameter of the resulting sequence.</typeparam>
  475. /// <param name="exception">Exception object used for the sequence's termination.</param>
  476. /// <param name="scheduler">Scheduler to send the exceptional termination call on.</param>
  477. /// <param name="witness">Object solely used to infer the type of the <typeparamref name="TResult"/> type parameter. This parameter is typically used when creating a sequence of anonymously typed elements.</param>
  478. /// <returns>The observable sequence that terminates exceptionally with the specified exception object.</returns>
  479. /// <exception cref="ArgumentNullException"><paramref name="exception"/> or <paramref name="scheduler"/> is null.</exception>
  480. public static IObservable<TResult> Throw<TResult>(Exception exception, IScheduler scheduler, TResult witness)
  481. {
  482. if (exception == null)
  483. throw new ArgumentNullException("exception");
  484. if (scheduler == null)
  485. throw new ArgumentNullException("scheduler");
  486. return s_impl.Throw<TResult>(exception, scheduler); // Pure inference - no specialized target method.
  487. }
  488. #endregion
  489. #region + Using +
  490. /// <summary>
  491. /// Constructs an observable sequence that depends on a resource object, whose lifetime is tied to the resulting observable sequence's lifetime.
  492. /// </summary>
  493. /// <typeparam name="TResult">The type of the elements in the produced sequence.</typeparam>
  494. /// <typeparam name="TResource">The type of the resource used during the generation of the resulting sequence. Needs to implement <see cref="System.IDisposable"/>.</typeparam>
  495. /// <param name="resourceFactory">Factory function to obtain a resource object.</param>
  496. /// <param name="observableFactory">Factory function to obtain an observable sequence that depends on the obtained resource.</param>
  497. /// <returns>An observable sequence whose lifetime controls the lifetime of the dependent resource object.</returns>
  498. /// <exception cref="ArgumentNullException"><paramref name="resourceFactory"/> or <paramref name="observableFactory"/> is null.</exception>
  499. public static IObservable<TResult> Using<TResult, TResource>(Func<TResource> resourceFactory, Func<TResource, IObservable<TResult>> observableFactory) where TResource : IDisposable
  500. {
  501. if (resourceFactory == null)
  502. throw new ArgumentNullException("resourceFactory");
  503. if (observableFactory == null)
  504. throw new ArgumentNullException("observableFactory");
  505. return s_impl.Using<TResult, TResource>(resourceFactory, observableFactory);
  506. }
  507. #endregion
  508. #region + UsingAsync +
  509. #if !NO_TPL
  510. /// <summary>
  511. /// Constructs an observable sequence that depends on a resource object, whose lifetime is tied to the resulting observable sequence's lifetime. The resource is obtained and used through asynchronous methods.
  512. /// The CancellationToken passed to the asynchronous methods is tied to the returned disposable subscription, allowing best-effort cancellation at any stage of the resource acquisition or usage.
  513. /// </summary>
  514. /// <typeparam name="TResult">The type of the elements in the produced sequence.</typeparam>
  515. /// <typeparam name="TResource">The type of the resource used during the generation of the resulting sequence. Needs to implement <see cref="System.IDisposable"/>.</typeparam>
  516. /// <param name="resourceFactoryAsync">Asynchronous factory function to obtain a resource object.</param>
  517. /// <param name="observableFactoryAsync">Asynchronous factory function to obtain an observable sequence that depends on the obtained resource.</param>
  518. /// <returns>An observable sequence whose lifetime controls the lifetime of the dependent resource object.</returns>
  519. /// <exception cref="ArgumentNullException"><paramref name="resourceFactoryAsync"/> or <paramref name="observableFactoryAsync"/> is null.</exception>
  520. /// <remarks>This operator is especially useful in conjunction with the asynchronous programming features introduced in C# 5.0 and Visual Basic 11.</remarks>
  521. /// <remarks>When a subscription to the resulting sequence is disposed, the CancellationToken that was fed to the asynchronous resource factory and observable factory functions will be signaled.</remarks>
  522. public static IObservable<TResult> Using<TResult, TResource>(Func<CancellationToken, Task<TResource>> resourceFactoryAsync, Func<TResource, CancellationToken, Task<IObservable<TResult>>> observableFactoryAsync) where TResource : IDisposable
  523. {
  524. if (resourceFactoryAsync == null)
  525. throw new ArgumentNullException("resourceFactoryAsync");
  526. if (observableFactoryAsync == null)
  527. throw new ArgumentNullException("observableFactoryAsync");
  528. return s_impl.Using<TResult, TResource>(resourceFactoryAsync, observableFactoryAsync);
  529. }
  530. #endif
  531. #endregion
  532. }
  533. }