Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Linq/Reactive/Linq/Observable.Creation.cs

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