Observable.Creation.cs 39 KB

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