Skip to content
Merged
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: 7 additions & 0 deletions Funcky.Test/Extensions/AsyncEnumerableExtensions/MergeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,12 @@ public Task MergeASequenceWithADifferentComparer()

return AsyncAssert.Equal(expected, sequence1.Merge(sequence2, DescendingIntComparer.Create()));
}

[Fact]
public async Task CancellationIsPropagated()
{
var canceledToken = new CancellationToken(canceled: true);
_ = await new AssertIsCancellationRequestedAsyncSequence<Unit>().Merge(AsyncEnumerable.Empty<Unit>()).ToListAsync(canceledToken);
}
}
#endif
18 changes: 13 additions & 5 deletions Funcky/Extensions/AsyncEnumerableExtensions/Merge.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#if INTEGRATED_ASYNC
using System.Collections.Immutable;
using System.Runtime.CompilerServices;

namespace Funcky.Extensions;

Expand Down Expand Up @@ -56,13 +57,20 @@ public static IAsyncEnumerable<TSource> Merge<TSource>(this IAsyncEnumerable<TSo
/// <typeparam name="TSource">The type elements in the sequences.</typeparam>
/// <returns>The merged sequences in the same order as the given sequences.</returns>
[Pure]
public static async IAsyncEnumerable<TSource> Merge<TSource>(this IEnumerable<IAsyncEnumerable<TSource>> sources, Option<IComparer<TSource>> comparer = default)
public static IAsyncEnumerable<TSource> Merge<TSource>(this IEnumerable<IAsyncEnumerable<TSource>> sources, Option<IComparer<TSource>> comparer = default)
=> MergeInternal(sources, comparer);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static async IAsyncEnumerable<TSource> MergeInternal<TSource>(
IEnumerable<IAsyncEnumerable<TSource>> sources,
Option<IComparer<TSource>> comparer,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
var enumerators = GetMergeEnumerators(sources);
var enumerators = GetMergeEnumerators(sources, cancellationToken);

try
{
await foreach (var element in MergeEnumerators(enumerators.RemoveRange(await enumerators.ToAsyncEnumerable().Where(async (f, _) => await HasMoreElements(f).ConfigureAwait(false)).ToListAsync().ConfigureAwait(false)), GetMergeComparer(comparer)).ConfigureAwait(false))
await foreach (var element in MergeEnumerators(enumerators.RemoveRange(await enumerators.ToAsyncEnumerable().Where(async (f, _) => await HasMoreElements(f).ConfigureAwait(false)).ToListAsync(cancellationToken).ConfigureAwait(false)), GetMergeComparer(comparer)).ConfigureAwait(false))
{
yield return element;
}
Expand All @@ -76,8 +84,8 @@ public static async IAsyncEnumerable<TSource> Merge<TSource>(this IEnumerable<IA
}
}

private static ImmutableList<IAsyncEnumerator<TSource>> GetMergeEnumerators<TSource>(IEnumerable<IAsyncEnumerable<TSource>> sources)
=> ImmutableList.Create<IAsyncEnumerator<TSource>>().AddRange(sources.Select(s => s.GetAsyncEnumerator()));
private static ImmutableList<IAsyncEnumerator<TSource>> GetMergeEnumerators<TSource>(IEnumerable<IAsyncEnumerable<TSource>> sources, CancellationToken cancellationToken)
=> ImmutableList.Create<IAsyncEnumerator<TSource>>().AddRange(sources.Select(s => s.GetAsyncEnumerator(cancellationToken)));

private static IComparer<TSource> GetMergeComparer<TSource>(Option<IComparer<TSource>> comparer = default)
=> comparer.GetOrElse(Comparer<TSource>.Default);
Expand Down
Loading