PagedAsyncEnumerator's nextPage should return false if there are no more pages.

This commit is contained in:
RogueException
2016-10-17 16:11:35 -03:00
parent 3ca0067f1b
commit dfe654af3b
3 changed files with 16 additions and 11 deletions

View File

@@ -1,6 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -13,9 +12,9 @@ namespace Discord
private readonly ulong? _start; private readonly ulong? _start;
private readonly int? _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 Func<PageInfo, IReadOnlyCollection<T>, bool> _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, Func<PageInfo, IReadOnlyCollection<T>, bool> nextPage = null,
ulong? start = null, int? count = null) ulong? start = null, int? count = null)
{ {
PageSize = pageSize; PageSize = pageSize;
@@ -64,7 +63,10 @@ namespace Discord
_info.PageSize = _info.Remaining != null ? (int)Math.Min(_info.Remaining.Value, _source.PageSize) : _source.PageSize; _info.PageSize = _info.Remaining != null ? (int)Math.Min(_info.Remaining.Value, _source.PageSize) : _source.PageSize;
if (_info.Remaining != 0) if (_info.Remaining != 0)
_source?._nextPage(_info, data); {
if (!_source._nextPage(_info, data))
_info.Remaining = 0;
}
return true; return true;
} }

View File

@@ -86,16 +86,17 @@ namespace Discord.Rest
if (info.Position != null) if (info.Position != null)
args.RelativeMessageId = info.Position.Value; args.RelativeMessageId = info.Position.Value;
var models = await client.ApiClient.GetChannelMessagesAsync(channel.Id, args, options).ConfigureAwait(false); var models = await client.ApiClient.GetChannelMessagesAsync(channel.Id, args, options).ConfigureAwait(false);
return models.Select(x => RestMessage.Create(client, guild, x)).ToImmutableArray(); ; return models.Select(x => RestMessage.Create(client, guild, x)).ToImmutableArray();
}, },
nextPage: (info, lastPage) => nextPage: (info, lastPage) =>
{ {
if (lastPage.Count != DiscordConfig.MaxMessagesPerBatch)
return false;
if (dir == Direction.Before) if (dir == Direction.Before)
info.Position = lastPage.Min(x => x.Id); info.Position = lastPage.Min(x => x.Id);
else else
info.Position = lastPage.Max(x => x.Id); info.Position = lastPage.Max(x => x.Id);
if (lastPage.Count != DiscordConfig.MaxMessagesPerBatch) return true;
info.Remaining = 0;
}, },
start: fromMessageId, start: fromMessageId,
count: limit count: limit
@@ -196,9 +197,10 @@ namespace Discord.Rest
}, },
nextPage: (info, lastPage) => nextPage: (info, lastPage) =>
{ {
info.Position = lastPage.Max(x => x.Id);
if (lastPage.Count != DiscordConfig.MaxMessagesPerBatch) if (lastPage.Count != DiscordConfig.MaxMessagesPerBatch)
info.Remaining = 0; return false;
info.Position = lastPage.Max(x => x.Id);
return true;
}, },
start: fromUserId, start: fromUserId,
count: limit count: limit

View File

@@ -187,9 +187,10 @@ namespace Discord.Rest
}, },
nextPage: (info, lastPage) => nextPage: (info, lastPage) =>
{ {
info.Position = lastPage.Max(x => x.Id);
if (lastPage.Count != DiscordConfig.MaxMessagesPerBatch) if (lastPage.Count != DiscordConfig.MaxMessagesPerBatch)
info.Remaining = 0; return false;
info.Position = lastPage.Max(x => x.Id);
return true;
}, },
start: fromUserId, start: fromUserId,
count: limit count: limit