@@ -4,17 +4,20 @@ | |||||
{ | { | ||||
public int Page { get; set; } | public int Page { get; set; } | ||||
public ulong? Position { get; set; } | public ulong? Position { get; set; } | ||||
public uint? Count { get; set; } | |||||
public int? Count { get; set; } | |||||
public int PageSize { get; set; } | public int PageSize { get; set; } | ||||
public uint? Remaining { get; set; } | |||||
public int? Remaining { get; set; } | |||||
internal PageInfo(ulong? pos, uint? count, int pageSize) | |||||
internal PageInfo(ulong? pos, int? count, int pageSize) | |||||
{ | { | ||||
Page = 1; | Page = 1; | ||||
Position = pos; | Position = pos; | ||||
Count = count; | Count = count; | ||||
Remaining = count; | Remaining = count; | ||||
PageSize = pageSize; | PageSize = pageSize; | ||||
if (Count != null && Count.Value < PageSize) | |||||
PageSize = Count.Value; | |||||
} | } | ||||
} | } | ||||
} | } |
@@ -11,12 +11,12 @@ namespace Discord | |||||
public int PageSize { get; } | public int PageSize { get; } | ||||
private readonly ulong? _start; | private readonly ulong? _start; | ||||
private readonly uint? _count; | |||||
private readonly int? _count; | |||||
private readonly Func<PageInfo, CancellationToken, Task<IReadOnlyCollection<T>>> _getPage; | private readonly Func<PageInfo, CancellationToken, Task<IReadOnlyCollection<T>>> _getPage; | ||||
private readonly Action<PageInfo, IReadOnlyCollection<T>> _nextPage; | private readonly Action<PageInfo, IReadOnlyCollection<T>> _nextPage; | ||||
public PagedAsyncEnumerable(int pageSize, Func<PageInfo, CancellationToken, Task<IReadOnlyCollection<T>>> getPage, Action<PageInfo, IReadOnlyCollection<T>> nextPage = null, | public PagedAsyncEnumerable(int pageSize, Func<PageInfo, CancellationToken, Task<IReadOnlyCollection<T>>> getPage, Action<PageInfo, IReadOnlyCollection<T>> nextPage = null, | ||||
ulong? start = null, uint? count = null) | |||||
ulong? start = null, int? count = null) | |||||
{ | { | ||||
PageSize = pageSize; | PageSize = pageSize; | ||||
_start = start; | _start = start; | ||||
@@ -42,15 +42,31 @@ namespace Discord | |||||
public async Task<bool> MoveNext(CancellationToken cancelToken) | public async Task<bool> MoveNext(CancellationToken cancelToken) | ||||
{ | { | ||||
if (_info.Remaining == 0) | |||||
return false; | |||||
var data = await _source._getPage(_info, cancelToken); | var data = await _source._getPage(_info, cancelToken); | ||||
Current = new Page<T>(_info, data); | Current = new Page<T>(_info, data); | ||||
_info.Page++; | _info.Page++; | ||||
_info.Remaining -= (uint)Current.Count; | |||||
_info.PageSize = _info.Remaining != null ? (int)Math.Min(_info.Remaining.Value, (ulong)_source.PageSize) : _source.PageSize; | |||||
_source?._nextPage(_info, data); | |||||
if (_info.Remaining != null) | |||||
{ | |||||
if (Current.Count >= _info.Remaining) | |||||
_info.Remaining = 0; | |||||
else | |||||
_info.Remaining -= Current.Count; | |||||
} | |||||
else | |||||
{ | |||||
if (Current.Count == 0) | |||||
_info.Remaining = 0; | |||||
} | |||||
_info.PageSize = _info.Remaining != null ? (int)Math.Min(_info.Remaining.Value, _source.PageSize) : _source.PageSize; | |||||
if (_info.Remaining != 0) | |||||
_source?._nextPage(_info, data); | |||||
return _info.Remaining > 0; | |||||
return true; | |||||
} | } | ||||
public void Dispose() { Current = null; } | public void Dispose() { Current = null; } | ||||
@@ -99,7 +99,7 @@ namespace Discord.Rest | |||||
info.Remaining = 0; | info.Remaining = 0; | ||||
}, | }, | ||||
start: fromMessageId, | start: fromMessageId, | ||||
count: (uint)limit | |||||
count: limit | |||||
); | ); | ||||
} | } | ||||
public static async Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsync(IChannel channel, BaseDiscordClient client) | public static async Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsync(IChannel channel, BaseDiscordClient client) | ||||
@@ -176,7 +176,7 @@ namespace Discord.Rest | |||||
return user; | return user; | ||||
} | } | ||||
public static IAsyncEnumerable<IReadOnlyCollection<RestGuildUser>> GetUsersAsync(IGuildChannel channel, IGuild guild, BaseDiscordClient client, | public static IAsyncEnumerable<IReadOnlyCollection<RestGuildUser>> GetUsersAsync(IGuildChannel channel, IGuild guild, BaseDiscordClient client, | ||||
ulong? froUserId = null, uint? limit = DiscordConfig.MaxUsersPerBatch) | |||||
ulong? froUserId = null, int? limit = DiscordConfig.MaxUsersPerBatch) | |||||
{ | { | ||||
return new PagedAsyncEnumerable<RestGuildUser>( | return new PagedAsyncEnumerable<RestGuildUser>( | ||||
DiscordConfig.MaxUsersPerBatch, | DiscordConfig.MaxUsersPerBatch, | ||||
@@ -185,7 +185,7 @@ namespace Discord.Rest | |||||
info.Remaining = 0; | info.Remaining = 0; | ||||
}, | }, | ||||
start: fromUserId, | start: fromUserId, | ||||
count: (uint)limit | |||||
count: limit | |||||
); | ); | ||||
} | } | ||||
public static async Task<int> PruneUsersAsync(IGuild guild, BaseDiscordClient client, | public static async Task<int> PruneUsersAsync(IGuild guild, BaseDiscordClient client, | ||||