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