Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions AsyncRx.NET/System.Reactive.Async/AsyncObservableBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT License.
// See the LICENSE file in the project root for more information.

using System.Reactive.Internal;
using System.Threading.Tasks;

namespace System.Reactive
Expand Down Expand Up @@ -124,7 +125,7 @@ protected override async ValueTask OnCompletedAsyncCore()
return;
}

_task = _observer.OnCompletedAsync();
_task = _observer.OnCompletedAsync_EnsureAsync();
}

try
Expand All @@ -146,7 +147,7 @@ protected override async ValueTask OnErrorAsyncCore(Exception error)
return;
}

_task = _observer.OnErrorAsync(error);
_task = _observer.OnErrorAsync_EnsureAsync(error);
}

try
Expand All @@ -168,7 +169,7 @@ protected override async ValueTask OnNextAsyncCore(T value)
return;
}

_task = _observer.OnNextAsync(value);
_task = _observer.OnNextAsync_EnsureAsync(value);
}

try
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Threading.Tasks;

namespace System.Reactive.Internal;

// Helpers methods that ensure that calls to IAsyncObserver methods don't throw synchronously.
// Those methods will always return a ValueTask, and any exception will be propagated through that ValueTask.
internal static class AsyncObserverEnsureAsyncHelpers
{
public static ValueTask OnNextAsync_EnsureAsync<T>(this IAsyncObserver<T> source, T value)
{
try
{
return source.OnNextAsync(value);
}
catch (Exception e)
{
return new ValueTask(Task.FromException(e));
}
}

public static ValueTask OnErrorAsync_EnsureAsync<T>(this IAsyncObserver<T> source, Exception error)
{
try
{
return source.OnErrorAsync(error);
}
catch (Exception e)
{
return new ValueTask(Task.FromException(e));
}
}

public static ValueTask OnCompletedAsync_EnsureAsync<T>(this IAsyncObserver<T> source)
{
try
{
return source.OnCompletedAsync();
}
catch (Exception e)
{
return new ValueTask(Task.FromException(e));
}
}
}
Loading