-
-
Notifications
You must be signed in to change notification settings - Fork 737
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Renamed existing Flatten method to FlattenAsync and added new Flatten…
… method. Also fixed ClientHelper using incorrect guild batch count. (#744)
- Loading branch information
1 parent
edfbd05
commit 5bbd9bb
Showing
4 changed files
with
63 additions
and
11 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
54 changes: 52 additions & 2 deletions
54
src/Discord.Net.Core/Extensions/AsyncEnumerableExtensions.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 |
---|---|---|
@@ -1,14 +1,64 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Discord | ||
{ | ||
public static class AsyncEnumerableExtensions | ||
{ | ||
public static async Task<IEnumerable<T>> Flatten<T>(this IAsyncEnumerable<IReadOnlyCollection<T>> source) | ||
/// <summary> | ||
/// Flattens the specified pages into one <see cref="IEnumerable{T}"/> asynchronously | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="source"></param> | ||
/// <returns></returns> | ||
public static async Task<IEnumerable<T>> FlattenAsync<T>(this IAsyncEnumerable<IEnumerable<T>> source) | ||
{ | ||
return (await source.ToArray().ConfigureAwait(false)).SelectMany(x => x); | ||
return await source.Flatten().ToArray().ConfigureAwait(false); | ||
} | ||
|
||
public static IAsyncEnumerable<T> Flatten<T>(this IAsyncEnumerable<IEnumerable<T>> source) | ||
{ | ||
return new PagedCollectionEnumerator<T>(source); | ||
} | ||
|
||
internal class PagedCollectionEnumerator<T> : IAsyncEnumerator<T>, IAsyncEnumerable<T> | ||
{ | ||
readonly IAsyncEnumerator<IEnumerable<T>> _source; | ||
IEnumerator<T> _enumerator; | ||
|
||
public IAsyncEnumerator<T> GetEnumerator() => this; | ||
|
||
internal PagedCollectionEnumerator(IAsyncEnumerable<IEnumerable<T>> source) | ||
{ | ||
_source = source.GetEnumerator(); | ||
} | ||
|
||
public T Current => _enumerator.Current; | ||
|
||
public void Dispose() | ||
{ | ||
_enumerator?.Dispose(); | ||
_source.Dispose(); | ||
} | ||
|
||
public async Task<bool> MoveNext(CancellationToken cancellationToken) | ||
{ | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
|
||
if(!_enumerator?.MoveNext() ?? true) | ||
{ | ||
if (!await _source.MoveNext(cancellationToken).ConfigureAwait(false)) | ||
return false; | ||
|
||
_enumerator?.Dispose(); | ||
_enumerator = _source.Current.GetEnumerator(); | ||
return _enumerator.MoveNext(); | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
} | ||
} |
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