-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In certain scenarios, it might be useful to be able to create the anonymous observer outside the Subscribe overloads. For example, the official guidance on DiagnosticSource filtering recommends creating an anonymous observer to combine with the predicate to filter events. There may be other scenarios too where not having to manually create an IObserver<T> could be useful. Fixes #8
- Loading branch information
Showing
7 changed files
with
196 additions
and
68 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using System.Runtime.ExceptionServices; | ||
|
||
namespace System | ||
{ | ||
/// <summary> | ||
/// Create an <see cref="IObserver{T}"/> instance from delegate-based implementations | ||
/// of the On* methods. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the elements in the sequence.</typeparam> | ||
partial class AnonymousObserver<T> : IObserver<T> | ||
{ | ||
static readonly Action<Exception> rethrow = e => ExceptionDispatchInfo.Capture(e).Throw(); | ||
static readonly Action nop = () => { }; | ||
|
||
readonly Action<T> onNext; | ||
readonly Action<Exception> onError; | ||
readonly Action onCompleted; | ||
|
||
/// <summary> | ||
/// Creates the observable providing just the <paramref name="onNext"/> action. | ||
/// </summary> | ||
public AnonymousObserver(Action<T> onNext) | ||
: this(onNext, rethrow, nop) { } | ||
|
||
/// <summary> | ||
/// Creates the observable providing both the <paramref name="onNext"/> and | ||
/// <paramref name="onError"/> actions. | ||
/// </summary> | ||
public AnonymousObserver(Action<T> onNext, Action<Exception> onError) | ||
: this(onNext, onError, nop) { } | ||
|
||
/// <summary> | ||
/// Creates the observable providing both the <paramref name="onNext"/> and | ||
/// <paramref name="onCompleted"/> actions. | ||
/// </summary> | ||
public AnonymousObserver(Action<T> onNext, Action onCompleted) | ||
: this(onNext, rethrow, onCompleted) { } | ||
|
||
/// <summary> | ||
/// Creates the observable providing all three <paramref name="onNext"/>, | ||
/// <paramref name="onError"/> and <paramref name="onCompleted"/> actions. | ||
/// </summary> | ||
public AnonymousObserver(Action<T> onNext, Action<Exception> onError, Action onCompleted) | ||
{ | ||
this.onNext = onNext; | ||
this.onError = onError; | ||
this.onCompleted = onCompleted; | ||
} | ||
|
||
/// <summary> | ||
/// Calls the action implementing <see cref="IObserver{T}.OnCompleted()"/>. | ||
/// </summary> | ||
public void OnCompleted() => onCompleted(); | ||
|
||
/// <summary> | ||
/// Calls the action implementing <see cref="IObserver{T}.OnError(Exception)"/>. | ||
/// </summary> | ||
public void OnError(Exception error) => onError(error); | ||
|
||
/// <summary> | ||
/// Calls the action implementing <see cref="IObserver{T}.OnNext(T)"/>. | ||
/// </summary> | ||
public void OnNext(T value) => onNext(value); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using System.Runtime.ExceptionServices; | ||
|
||
namespace System | ||
{ | ||
/// <summary> | ||
/// Create an <see cref="IObserver{T}"/> instance from delegate-based implementations | ||
/// of the On* methods. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the elements in the sequence.</typeparam> | ||
partial class AnonymousObserver<T> : IObserver<T> | ||
{ | ||
static readonly Action<Exception> rethrow = e => ExceptionDispatchInfo.Capture(e).Throw(); | ||
static readonly Action nop = () => { }; | ||
|
||
readonly Action<T> onNext; | ||
readonly Action<Exception> onError; | ||
readonly Action onCompleted; | ||
|
||
/// <summary> | ||
/// Creates the observable providing just the <paramref name="onNext"/> action. | ||
/// </summary> | ||
public AnonymousObserver(Action<T> onNext) | ||
: this(onNext, rethrow, nop) { } | ||
|
||
/// <summary> | ||
/// Creates the observable providing both the <paramref name="onNext"/> and | ||
/// <paramref name="onError"/> actions. | ||
/// </summary> | ||
public AnonymousObserver(Action<T> onNext, Action<Exception> onError) | ||
: this(onNext, onError, nop) { } | ||
|
||
/// <summary> | ||
/// Creates the observable providing both the <paramref name="onNext"/> and | ||
/// <paramref name="onCompleted"/> actions. | ||
/// </summary> | ||
public AnonymousObserver(Action<T> onNext, Action onCompleted) | ||
: this(onNext, rethrow, onCompleted) { } | ||
|
||
/// <summary> | ||
/// Creates the observable providing all three <paramref name="onNext"/>, | ||
/// <paramref name="onError"/> and <paramref name="onCompleted"/> actions. | ||
/// </summary> | ||
public AnonymousObserver(Action<T> onNext, Action<Exception> onError, Action onCompleted) | ||
{ | ||
this.onNext = onNext; | ||
this.onError = onError; | ||
this.onCompleted = onCompleted; | ||
} | ||
|
||
/// <summary> | ||
/// Calls the action implementing <see cref="IObserver{T}.OnCompleted()"/>. | ||
/// </summary> | ||
public void OnCompleted() => onCompleted(); | ||
|
||
/// <summary> | ||
/// Calls the action implementing <see cref="IObserver{T}.OnError(Exception)"/>. | ||
/// </summary> | ||
public void OnError(Exception error) => onError(error); | ||
|
||
/// <summary> | ||
/// Calls the action implementing <see cref="IObserver{T}.OnNext(T)"/>. | ||
/// </summary> | ||
public void OnNext(T value) => onNext(value); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using System.Runtime.ExceptionServices; | ||
|
||
namespace System | ||
{ | ||
/// <summary> | ||
/// Create an <see cref="IObserver{T}"/> instance from delegate-based implementations | ||
/// of the On* methods. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the elements in the sequence.</typeparam> | ||
partial class AnonymousObserver<T> : IObserver<T> | ||
{ | ||
static readonly Action<Exception> rethrow = e => ExceptionDispatchInfo.Capture(e).Throw(); | ||
static readonly Action nop = () => { }; | ||
|
||
readonly Action<T> onNext; | ||
readonly Action<Exception> onError; | ||
readonly Action onCompleted; | ||
|
||
/// <summary> | ||
/// Creates the observable providing just the <paramref name="onNext"/> action. | ||
/// </summary> | ||
public AnonymousObserver(Action<T> onNext) | ||
: this(onNext, rethrow, nop) { } | ||
|
||
/// <summary> | ||
/// Creates the observable providing both the <paramref name="onNext"/> and | ||
/// <paramref name="onError"/> actions. | ||
/// </summary> | ||
public AnonymousObserver(Action<T> onNext, Action<Exception> onError) | ||
: this(onNext, onError, nop) { } | ||
|
||
/// <summary> | ||
/// Creates the observable providing both the <paramref name="onNext"/> and | ||
/// <paramref name="onCompleted"/> actions. | ||
/// </summary> | ||
public AnonymousObserver(Action<T> onNext, Action onCompleted) | ||
: this(onNext, rethrow, onCompleted) { } | ||
|
||
/// <summary> | ||
/// Creates the observable providing all three <paramref name="onNext"/>, | ||
/// <paramref name="onError"/> and <paramref name="onCompleted"/> actions. | ||
/// </summary> | ||
public AnonymousObserver(Action<T> onNext, Action<Exception> onError, Action onCompleted) | ||
{ | ||
this.onNext = onNext; | ||
this.onError = onError; | ||
this.onCompleted = onCompleted; | ||
} | ||
|
||
/// <summary> | ||
/// Calls the action implementing <see cref="IObserver{T}.OnCompleted()"/>. | ||
/// </summary> | ||
public void OnCompleted() => onCompleted(); | ||
|
||
/// <summary> | ||
/// Calls the action implementing <see cref="IObserver{T}.OnError(Exception)"/>. | ||
/// </summary> | ||
public void OnError(Exception error) => onError(error); | ||
|
||
/// <summary> | ||
/// Calls the action implementing <see cref="IObserver{T}.OnNext(T)"/>. | ||
/// </summary> | ||
public void OnNext(T value) => onNext(value); | ||
} | ||
} |
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