TakeUntilCancellationToken<TSource>
Relays items to the downstream until the CancellationToken is cancelled.
using System.
Reactive.
Disposables;
using System.
Runtime.
CompilerServices;
using System.
Threading;
namespace System.
Reactive.
Linq.
ObservableImpl
{
[
System.
Runtime.
CompilerServices.
NullableContext(
1)]
[
System.
Runtime.
CompilerServices.
Nullable(
new byte[] {
0,
1,
1,
1
})]
internal sealed class TakeUntilCancellationToken<[
System.
Runtime.
CompilerServices.
Nullable(
2)]
TSource> :
Producer<
TSource,
TakeUntilCancellationToken<
TSource>.
_>
{
[
System.
Runtime.
CompilerServices.
Nullable(
new byte[] {
0,
1
})]
internal sealed class _ :
IdentitySink<
TSource>
{
private SingleAssignmentDisposableValue _cancellationTokenRegistration;
private int _wip;
[
System.
Runtime.
CompilerServices.
Nullable(
2)]
private Exception _error;
public _(
IObserver<
TSource>
observer)
:
base(
observer)
{
}
public void Run(
TakeUntilCancellationToken<
TSource>
parent)
{
CancellationToken token =
parent.
_token;
if (
token.
IsCancellationRequested)
OnCompleted();
else {
ref SingleAssignmentDisposableValue cancellationTokenRegistration =
ref _cancellationTokenRegistration;
token =
parent.
_token;
cancellationTokenRegistration.
Disposable =
token.
Register(((
Sink<
TSource,
TSource>)
this).
OnCompleted);
Run(
parent.
_source);
}
}
protected override void Dispose(
bool disposing)
{
if (
disposing)
_cancellationTokenRegistration.
Dispose();
base.
Dispose(
disposing);
}
public override void OnNext(
TSource value)
{
HalfSerializer.
ForwardOnNext<
TSource>((
ISink<
TSource>)
this,
value,
ref _wip,
ref _error);
}
public override void OnError(
Exception error)
{
HalfSerializer.
ForwardOnError<
TSource>((
ISink<
TSource>)
this,
error,
ref _wip,
ref _error);
}
public override void OnCompleted()
{
HalfSerializer.
ForwardOnCompleted<
TSource>((
ISink<
TSource>)
this,
ref _wip,
ref _error);
}
}
private readonly IObservable<
TSource>
_source;
private readonly CancellationToken _token;
public TakeUntilCancellationToken(
IObservable<
TSource>
source,
CancellationToken token)
{
_source =
source;
_token =
token;
}
[
return:
System.
Runtime.
CompilerServices.
Nullable(
new byte[] {
1,
0
})]
protected override _ CreateSink(
IObserver<
TSource>
observer)
{
return new _(
observer);
}
protected override void Run([
System.
Runtime.
CompilerServices.
Nullable(
new byte[] {
1,
0
})]
_ sink)
{
sink.
Run(
this);
}
}
}