Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Core/Reactive/Internal/Producer.cs

raw
1 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2
3 #if !NO_PERF
4 using System.Reactive.Concurrency;
5 using System.Reactive.Disposables;
6
7 namespace System.Reactive
8 {
9 /// <summary>
10 /// Interface with variance annotation; allows for better type checking when detecting capabilities in SubscribeSafe.
11 /// </summary>
12 /// <typeparam name="TSource">Type of the resulting sequence's elements.</typeparam>
13 internal interface IProducer<
14 #if !NO_VARIANCE
15 out
16 #endif
17 TSource> : IObservable<TSource>
18 {
19 IDisposable SubscribeRaw(IObserver<TSource> observer, bool enableSafeguard);
20 }
21
22 /// <summary>
23 /// Base class for implementation of query operators, providing performance benefits over the use of Observable.Create.
24 /// </summary>
25 /// <typeparam name="TSource">Type of the resulting sequence's elements.</typeparam>
26 internal abstract class Producer<TSource> : IProducer<TSource>
27 {
28 /// <summary>
29 /// Publicly visible Subscribe method.
30 /// </summary>
31 /// <param name="observer">Observer to send notifications on. The implementation of a producer must ensure the correct message grammar on the observer.</param>
32 /// <returns>IDisposable to cancel the subscription. This causes the underlying sink to be notified of unsubscription, causing it to prevent further messages from being sent to the observer.</returns>
33 public IDisposable Subscribe(IObserver<TSource> observer)
34 {
35 if (observer == null)
36 throw new ArgumentNullException("observer");
37
38 return SubscribeRaw(observer, true);
39 }
40
41 public IDisposable SubscribeRaw(IObserver<TSource> observer, bool enableSafeguard)
42 {
43 var state = new State();
44 state.observer = observer;
45 state.sink = new SingleAssignmentDisposable();
46 state.subscription = new SingleAssignmentDisposable();
47
48 var d = new CompositeDisposable(2) { state.sink, state.subscription };
49
50 //
51 // See AutoDetachObserver.cs for more information on the safeguarding requirement and
52 // its implementation aspects.
53 //
54 if (enableSafeguard)
55 {
56 state.observer = SafeObserver<TSource>.Create(state.observer, d);
57 }
58
59 if (CurrentThreadScheduler.IsScheduleRequired)
60 {
61 CurrentThreadScheduler.Instance.Schedule(state, Run);
62 }
63 else
64 {
65 state.subscription.Disposable = this.Run(state.observer, state.subscription, state.Assign);
66 }
67
68 return d;
69 }
70
71 struct State
72 {
73 public SingleAssignmentDisposable sink;
74 public SingleAssignmentDisposable subscription;
75 public IObserver<TSource> observer;
76
77 public void Assign(IDisposable s)
78 {
79 sink.Disposable = s;
80 }
81 }
82
83 private IDisposable Run(IScheduler _, State x)
84 {
85 x.subscription.Disposable = this.Run(x.observer, x.subscription, x.Assign);
86 return Disposable.Empty;
87 }
88
89 /// <summary>
90 /// Core implementation of the query operator, called upon a new subscription to the producer object.
91 /// </summary>
92 /// <param name="observer">Observer to send notifications on. The implementation of a producer must ensure the correct message grammar on the observer.</param>
93 /// <param name="cancel">The subscription disposable object returned from the Run call, passed in such that it can be forwarded to the sink, allowing it to dispose the subscription upon sending a final message (or prematurely for other reasons).</param>
94 /// <param name="setSink">Callback to communicate the sink object to the subscriber, allowing consumers to tunnel a Dispose call into the sink, which can stop the processing.</param>
95 /// <returns>Disposable representing all the resources and/or subscriptions the operator uses to process events.</returns>
96 /// <remarks>The <paramref name="observer">observer</paramref> passed in to this method is not protected using auto-detach behavior upon an OnError or OnCompleted call. The implementation must ensure proper resource disposal and enforce the message grammar.</remarks>
97 protected abstract IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink);
98 }
99 }
100 #endif