-
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
namespace R3; | ||
|
||
public static partial class ObservableExtensions | ||
{ | ||
public static Task<bool> ContainsAsync<T>(this Observable<T> source, T value, CancellationToken cancellationToken = default) | ||
{ | ||
return ContainsAsync(source, value, EqualityComparer<T>.Default, cancellationToken); | ||
} | ||
|
||
public static Task<bool> ContainsAsync<T>(this Observable<T> source, T value, IEqualityComparer<T> equalityComparer, CancellationToken cancellationToken = default) | ||
{ | ||
var observer = new ContainsAsync<T>(value, equalityComparer, cancellationToken); | ||
source.Subscribe(observer); | ||
return observer.Task; | ||
} | ||
} | ||
|
||
internal sealed class ContainsAsync<T>(T value, IEqualityComparer<T> equalityComparer, CancellationToken cancellationToken) | ||
: TaskObserverBase<T, bool>(cancellationToken) | ||
{ | ||
protected override void OnNextCore(T value) | ||
{ | ||
if (!equalityComparer.Equals(value)) | ||
{ | ||
TrySetResult(true); | ||
} | ||
} | ||
|
||
protected override void OnErrorResumeCore(Exception error) | ||
{ | ||
TrySetException(error); | ||
} | ||
|
||
protected override void OnCompletedCore(Result result) | ||
{ | ||
if (result.IsFailure) | ||
{ | ||
TrySetException(result.Exception); | ||
return; | ||
} | ||
TrySetResult(false); | ||
} | ||
} | ||
|