-
Notifications
You must be signed in to change notification settings - Fork 751
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the TakeUntil(Func<T, bool>) operator (#612)
- Loading branch information
1 parent
55e967e
commit ae8c989
Showing
7 changed files
with
327 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
Rx.NET/Source/src/System.Reactive/Linq/Observable/TakeUntilPredicate.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// 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; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace System.Reactive.Linq.ObservableImpl | ||
{ | ||
/// <summary> | ||
/// Relays items to the downstream until the predicate returns <code>true</code>. | ||
/// </summary> | ||
/// <typeparam name="TSource">The element type of the sequence</typeparam> | ||
internal sealed class TakeUntilPredicate<TSource> : | ||
Producer<TSource, TakeUntilPredicate<TSource>.TakeUntilPredicateObserver> | ||
{ | ||
readonly IObservable<TSource> _source; | ||
|
||
readonly Func<TSource, bool> _stopPredicate; | ||
|
||
public TakeUntilPredicate(IObservable<TSource> source, Func<TSource, bool> stopPredicate) | ||
{ | ||
this._source = source; | ||
this._stopPredicate = stopPredicate; | ||
} | ||
|
||
protected override TakeUntilPredicateObserver CreateSink(IObserver<TSource> observer) => new TakeUntilPredicateObserver(observer, _stopPredicate); | ||
|
||
protected override void Run(TakeUntilPredicateObserver sink) => sink.Run(_source); | ||
|
||
internal sealed class TakeUntilPredicateObserver : IdentitySink<TSource> | ||
{ | ||
readonly Func<TSource, bool> _stopPredicate; | ||
|
||
public TakeUntilPredicateObserver(IObserver<TSource> downstream, | ||
Func<TSource, bool> predicate) : base (downstream) | ||
{ | ||
this._stopPredicate = predicate; | ||
} | ||
|
||
public override void OnCompleted() | ||
{ | ||
ForwardOnCompleted(); | ||
} | ||
|
||
public override void OnError(Exception error) | ||
{ | ||
ForwardOnError(error); | ||
} | ||
|
||
public override void OnNext(TSource value) | ||
{ | ||
ForwardOnNext(value); | ||
|
||
var shouldStop = false; | ||
try | ||
{ | ||
shouldStop = _stopPredicate(value); | ||
} | ||
catch (Exception ex) | ||
{ | ||
ForwardOnError(ex); | ||
return; | ||
} | ||
if (shouldStop) | ||
{ | ||
ForwardOnCompleted(); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters