123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the Apache 2.0 License.
- // See the LICENSE file in the project root for more information.
- using System.Reactive.Disposables;
- namespace System.Reactive
- {
- internal interface ISink<in TTarget>
- {
- void ForwardOnNext(TTarget value);
- void ForwardOnCompleted();
- void ForwardOnError(Exception error);
- }
- internal abstract class Sink<TTarget> : ISink<TTarget>, IDisposable
- {
- private IDisposable _upstream;
- private volatile IObserver<TTarget> _observer;
- protected Sink(IObserver<TTarget> observer)
- {
- _observer = observer;
- }
- public void Dispose()
- {
- Dispose(true);
- }
- protected virtual void Dispose(bool disposing)
- {
- _observer = NopObserver<TTarget>.Instance;
- Disposable.TryDispose(ref _upstream);
- }
- public void ForwardOnNext(TTarget value)
- {
- _observer.OnNext(value);
- }
- public void ForwardOnCompleted()
- {
- _observer.OnCompleted();
- Dispose();
- }
- public void ForwardOnError(Exception error)
- {
- _observer.OnError(error);
- Dispose();
- }
- protected void SetUpstream(IDisposable upstream)
- {
- Disposable.SetSingle(ref _upstream, upstream);
- }
- protected void DisposeUpstream()
- {
- Disposable.TryDispose(ref _upstream);
- }
- }
- /// <summary>
- /// Base class for implementation of query operators, providing a lightweight sink that can be disposed to mute the outgoing observer.
- /// </summary>
- /// <typeparam name="TTarget">Type of the resulting sequence's elements.</typeparam>
- /// <typeparam name="TSource"></typeparam>
- /// <remarks>Implementations of sinks are responsible to enforce the message grammar on the associated observer. Upon sending a terminal message, a pairing Dispose call should be made to trigger cancellation of related resources and to mute the outgoing observer.</remarks>
- internal abstract class Sink<TSource, TTarget> : Sink<TTarget>, IObserver<TSource>
- {
- protected Sink(IObserver<TTarget> observer) : base(observer)
- {
- }
- public virtual void Run(IObservable<TSource> source)
- {
- SetUpstream(source.SubscribeSafe(this));
- }
- public abstract void OnNext(TSource value);
- public virtual void OnError(Exception error)
- {
- ForwardOnError(error);
- }
- public virtual void OnCompleted()
- {
- ForwardOnCompleted();
- }
- public IObserver<TTarget> GetForwarder() => new _(this);
- private sealed class _ : IObserver<TTarget>
- {
- private readonly Sink<TSource, TTarget> _forward;
- public _(Sink<TSource, TTarget> forward)
- {
- _forward = forward;
- }
- public void OnNext(TTarget value)
- {
- _forward.ForwardOnNext(value);
- }
- public void OnError(Exception error)
- {
- _forward.ForwardOnError(error);
- }
- public void OnCompleted()
- {
- _forward.ForwardOnCompleted();
- }
- }
- }
- }
|