Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Linq/Reactive/Subjects/Subject.Extensions.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.Reactive.Concurrency;
4 using System.Reactive.Linq;
5
6 namespace System.Reactive.Subjects
7 {
8 /// <summary>
9 /// Provides a set of static methods for creating subjects.
10 /// </summary>
11 public static class Subject
12 {
13 /// <summary>
14 /// Creates a subject from the specified observer and observable.
15 /// </summary>
16 /// <typeparam name="TSource">The type of the elements received by the observer.</typeparam>
17 /// <typeparam name="TResult">The type of the elements produced by the observable sequence.</typeparam>
18 /// <param name="observer">The observer used to send messages to the subject.</param>
19 /// <param name="observable">The observable used to subscribe to messages sent from the subject.</param>
20 /// <returns>Subject implemented using the given observer and observable.</returns>
21 /// <exception cref="ArgumentNullException"><paramref name="observer"/> or <paramref name="observable"/> is null.</exception>
22 public static ISubject<TSource, TResult> Create<TSource, TResult>(IObserver<TSource> observer, IObservable<TResult> observable)
23 {
24 if (observer == null)
25 throw new ArgumentNullException("observer");
26 if (observable == null)
27 throw new ArgumentNullException("observable");
28
29 return new AnonymousSubject<TSource, TResult>(observer, observable);
30 }
31
32 /// <summary>
33 /// Synchronizes the messages sent to the subject.
34 /// </summary>
35 /// <typeparam name="TSource">The type of the elements received by the subject.</typeparam>
36 /// <typeparam name="TResult">The type of the elements produced by the subject.</typeparam>
37 /// <param name="subject">The subject to synchronize.</param>
38 /// <returns>Subject whose messages are synchronized.</returns>
39 /// <exception cref="ArgumentNullException"><paramref name="subject"/> is null.</exception>
40 public static ISubject<TSource, TResult> Synchronize<TSource, TResult>(ISubject<TSource, TResult> subject)
41 {
42 if (subject == null)
43 throw new ArgumentNullException("subject");
44
45 return new AnonymousSubject<TSource, TResult>(Observer.Synchronize(subject), subject);
46 }
47
48 /// <summary>
49 /// Synchronizes the messages sent to the subject and notifies observers on the specified scheduler.
50 /// </summary>
51 /// <typeparam name="TSource">The type of the elements received by the subject.</typeparam>
52 /// <typeparam name="TResult">The type of the elements produced by the subject.</typeparam>
53 /// <param name="subject">The subject to synchronize.</param>
54 /// <param name="scheduler">Scheduler to notify observers on.</param>
55 /// <returns>Subject whose messages are synchronized and whose observers are notified on the given scheduler.</returns>
56 /// <exception cref="ArgumentNullException"><paramref name="subject"/> or <paramref name="scheduler"/> is null.</exception>
57 public static ISubject<TSource, TResult> Synchronize<TSource, TResult>(ISubject<TSource, TResult> subject, IScheduler scheduler)
58 {
59 if (subject == null)
60 throw new ArgumentNullException("subject");
61 if (scheduler == null)
62 throw new ArgumentNullException("scheduler");
63
64 return new AnonymousSubject<TSource, TResult>(Observer.Synchronize(subject), subject.ObserveOn(scheduler));
65 }
66
67 class AnonymousSubject<T, U> : ISubject<T, U>
68 {
69 private readonly IObserver<T> _observer;
70 private readonly IObservable<U> _observable;
71
72 public AnonymousSubject(IObserver<T> observer, IObservable<U> observable)
73 {
74 _observer = observer;
75 _observable = observable;
76 }
77
78 public void OnCompleted()
79 {
80 _observer.OnCompleted();
81 }
82
83 public void OnError(Exception error)
84 {
85 if (error == null)
86 throw new ArgumentNullException("error");
87
88 _observer.OnError(error);
89 }
90
91 public void OnNext(T value)
92 {
93 _observer.OnNext(value);
94 }
95
96 public IDisposable Subscribe(IObserver<U> observer)
97 {
98 if (observer == null)
99 throw new ArgumentNullException("observer");
100
101 //
102 // [OK] Use of unsafe Subscribe: non-pretentious wrapping of an observable sequence.
103 //
104 return _observable.Subscribe/*Unsafe*/(observer);
105 }
106 }
107 }
108 }