Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Core/Reactive/ObservableBase.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.Disposables;
5
6 namespace System.Reactive
7 {
8 /// <summary>
9 /// Abstract base class for implementations of the IObservable&lt;T&gt; interface.
10 /// </summary>
11 /// <remarks>
12 /// If you don't need a named type to create an observable sequence (i.e. you rather need
13 /// an instance rather than a reusable type), use the Observable.Create method to create
14 /// an observable sequence with specified subscription behavior.
15 /// </remarks>
16 /// <typeparam name="T">The type of the elements in the sequence.</typeparam>
17 public abstract class ObservableBase<T> : IObservable<T>
18 {
19 /// <summary>
20 /// Subscribes the given observer to the observable sequence.
21 /// </summary>
22 /// <param name="observer">Observer that will receive notifications from the observable sequence.</param>
23 /// <returns>Disposable object representing an observer's subscription to the observable sequence.</returns>
24 /// <exception cref="ArgumentNullException"><paramref name="observer"/> is null.</exception>
25 public IDisposable Subscribe(IObserver<T> observer)
26 {
27 if (observer == null)
28 throw new ArgumentNullException("observer");
29
30 var autoDetachObserver = new AutoDetachObserver<T>(observer);
31
32 if (CurrentThreadScheduler.IsScheduleRequired)
33 {
34 //
35 // Notice we don't protect this piece of code using an exception handler to
36 // redirect errors to the OnError channel. This call to Schedule will run the
37 // trampoline, so we'd be catching all exceptions, including those from user
38 // callbacks that happen to run there. For example, consider:
39 //
40 // Observable.Return(42, Scheduler.CurrentThread)
41 // .Subscribe(x => { throw new Exception(); });
42 //
43 // Here, the OnNext(42) call would be scheduled on the trampoline, so when we
44 // return from the scheduled Subscribe call, the CurrentThreadScheduler moves
45 // on to invoking this work item. Too much of protection here would cause the
46 // exception thrown in OnNext to circle back to OnError, which looks like the
47 // sequence can't make up its mind.
48 //
49 CurrentThreadScheduler.Instance.Schedule(autoDetachObserver, ScheduledSubscribe);
50 }
51 else
52 {
53 try
54 {
55 autoDetachObserver.Disposable = SubscribeCore(autoDetachObserver);
56 }
57 catch (Exception exception)
58 {
59 //
60 // This can happen when there's a synchronous callback to OnError in the
61 // implementation of SubscribeCore, which also throws. So, we're seeing
62 // an exception being thrown from a handler.
63 //
64 // For compat with v1.x, we rethrow the exception in this case, keeping
65 // in mind this should be rare but if it happens, something's totally
66 // screwed up.
67 //
68 if (!autoDetachObserver.Fail(exception))
69 throw;
70 }
71 }
72
73 return autoDetachObserver;
74 }
75
76 private IDisposable ScheduledSubscribe(IScheduler _, AutoDetachObserver<T> autoDetachObserver)
77 {
78 try
79 {
80 autoDetachObserver.Disposable = SubscribeCore(autoDetachObserver);
81 }
82 catch (Exception exception)
83 {
84 //
85 // This can happen when there's a synchronous callback to OnError in the
86 // implementation of SubscribeCore, which also throws. So, we're seeing
87 // an exception being thrown from a handler.
88 //
89 // For compat with v1.x, we rethrow the exception in this case, keeping
90 // in mind this should be rare but if it happens, something's totally
91 // screwed up.
92 //
93 if (!autoDetachObserver.Fail(exception))
94 throw;
95 }
96
97 return Disposable.Empty;
98 }
99
100 /// <summary>
101 /// Implement this method with the core subscription logic for the observable sequence.
102 /// </summary>
103 /// <param name="observer">Observer to send notifications to.</param>
104 /// <returns>Disposable object representing an observer's subscription to the observable sequence.</returns>
105 protected abstract IDisposable SubscribeCore(IObserver<T> observer);
106 }
107 }