-
-
Notifications
You must be signed in to change notification settings - Fork 733
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow specifying list of user ids in SocketGuild.DownloadUsersAsync #2676
Open
compujuckel
wants to merge
6
commits into
discord-net:dev
Choose a base branch
from
compujuckel:dev
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
26cb939
Allow specifying list of user ids in SocketGuild.DownloadUsersAsync
compujuckel f8ace79
use Optional for optional parameters and (hopefully) fix CI errors
compujuckel df48372
await download task in ProcessUserDownloadsAsync
compujuckel 3f599c8
add new DownloadUsersAsync to BaseSocketClient and DiscordShardedClient
compujuckel 0d06290
small fix
compujuckel 1aad00b
- allow cancellation of DownloadUsersAsync
compujuckel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
103 changes: 103 additions & 0 deletions
103
src/Discord.Net.Core/Extensions/EnumerableExtensions.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 |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Based on https://github.com/dotnet/runtime/blob/main/src/libraries/System.Linq/src/System/Linq/Chunk.cs (only available on .NET 6+) | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
|
||
namespace Discord | ||
{ | ||
internal static class EnumerableExtensions | ||
{ | ||
/// <summary> | ||
/// Split the elements of a sequence into chunks of size at most <paramref name="size"/>. | ||
/// </summary> | ||
/// <remarks> | ||
/// Every chunk except the last will be of size <paramref name="size"/>. | ||
/// The last chunk will contain the remaining elements and may be of a smaller size. | ||
/// </remarks> | ||
/// <param name="source"> | ||
/// An <see cref="IEnumerable{T}"/> whose elements to chunk. | ||
/// </param> | ||
/// <param name="size"> | ||
/// Maximum size of each chunk. | ||
/// </param> | ||
/// <typeparam name="TSource"> | ||
/// The type of the elements of source. | ||
/// </typeparam> | ||
/// <returns> | ||
/// An <see cref="IEnumerable{T}"/> that contains the elements the input sequence split into chunks of size <paramref name="size"/>. | ||
/// </returns> | ||
/// <exception cref="ArgumentNullException"> | ||
/// <paramref name="source"/> is null. | ||
/// </exception> | ||
/// <exception cref="ArgumentOutOfRangeException"> | ||
/// <paramref name="size"/> is below 1. | ||
/// </exception> | ||
public static IEnumerable<TSource[]> Chunk<TSource>(this IEnumerable<TSource> source, int size) | ||
{ | ||
Preconditions.NotNull(source, nameof(source)); | ||
Preconditions.GreaterThan(size, 0, nameof(size)); | ||
|
||
return ChunkIterator(source, size); | ||
} | ||
|
||
private static IEnumerable<TSource[]> ChunkIterator<TSource>(IEnumerable<TSource> source, int size) | ||
{ | ||
using IEnumerator<TSource> e = source.GetEnumerator(); | ||
|
||
// Before allocating anything, make sure there's at least one element. | ||
if (e.MoveNext()) | ||
{ | ||
// Now that we know we have at least one item, allocate an initial storage array. This is not | ||
// the array we'll yield. It starts out small in order to avoid significantly overallocating | ||
// when the source has many fewer elements than the chunk size. | ||
int arraySize = Math.Min(size, 4); | ||
int i; | ||
do | ||
{ | ||
var array = new TSource[arraySize]; | ||
|
||
// Store the first item. | ||
array[0] = e.Current; | ||
i = 1; | ||
|
||
if (size != array.Length) | ||
{ | ||
// This is the first chunk. As we fill the array, grow it as needed. | ||
for (; i < size && e.MoveNext(); i++) | ||
{ | ||
if (i >= array.Length) | ||
{ | ||
arraySize = (int)Math.Min((uint)size, 2 * (uint)array.Length); | ||
Array.Resize(ref array, arraySize); | ||
} | ||
|
||
array[i] = e.Current; | ||
} | ||
} | ||
else | ||
{ | ||
// For all but the first chunk, the array will already be correctly sized. | ||
// We can just store into it until either it's full or MoveNext returns false. | ||
TSource[] local = array; // avoid bounds checks by using cached local (`array` is lifted to iterator object as a field) | ||
Debug.Assert(local.Length == size); | ||
for (; (uint)i < (uint)local.Length && e.MoveNext(); i++) | ||
{ | ||
local[i] = e.Current; | ||
} | ||
} | ||
|
||
if (i != array.Length) | ||
{ | ||
Array.Resize(ref array, i); | ||
} | ||
|
||
yield return array; | ||
} | ||
while (i >= size && e.MoveNext()); | ||
} | ||
} | ||
} | ||
} |
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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of defining 2 methods with the only difference being a cancel token, why not make the token parameter optional?