Skip to content

Commit 64eb015

Browse files
authored
Merge pull request #186 from kostya9/use_full_fetched_page_for_streaming_listing
Fix Streaming Listing fetching new page for each next item
2 parents 4d226e0 + 05239b3 commit 64eb015

File tree

2 files changed

+46
-25
lines changed

2 files changed

+46
-25
lines changed

RedditSharp/Listing.cs

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -424,36 +424,40 @@ private async Task<bool> MoveNextBackAsync(CancellationToken cancellationToken)
424424
private async Task<bool> MoveNextForwardAsync(CancellationToken cancellationToken)
425425
{
426426
CurrentIndex++;
427-
int tries = 0;
428-
while (true)
429-
{
430-
cancellationToken.ThrowIfCancellationRequested();
431-
tries++;
432427

433-
if (MaximumLimit != -1 && Count >= MaximumLimit)
428+
if (MaximumLimit != -1 && Count >= MaximumLimit)
429+
{
430+
return false;
431+
}
432+
433+
if (CurrentIndex == 0 || CurrentIndex >= CurrentPage.Count)
434+
{
435+
int tries = 0;
436+
while (true)
434437
{
435-
return false;
436-
}
438+
cancellationToken.ThrowIfCancellationRequested();
439+
tries++;
437440

438-
try
439-
{
440-
await FetchNextPageAsync().ConfigureAwait(false);
441-
CurrentIndex = 0;
442-
}
443-
catch (Exception ex)
444-
{
445-
// sleep for a while to see if we can recover
446-
await Sleep(tries, cancellationToken, ex).ConfigureAwait(false);
447-
}
441+
try
442+
{
443+
await FetchNextPageAsync().ConfigureAwait(false);
444+
CurrentIndex = 0;
445+
}
446+
catch (Exception ex)
447+
{
448+
// sleep for a while to see if we can recover
449+
await Sleep(tries, cancellationToken, ex).ConfigureAwait(false);
450+
}
448451

449-
// the page is only populated if there are *new* items to yielded from the listing.
450-
if (CurrentPage.Count > 0)
451-
{
452-
break;
453-
}
452+
// the page is only populated if there are *new* items to yielded from the listing.
453+
if (CurrentPage.Count > 0)
454+
{
455+
break;
456+
}
454457

455-
// No listings were returned in the page.
456-
await Sleep(tries, cancellationToken).ConfigureAwait(false);
458+
// No listings were returned in the page.
459+
await Sleep(tries, cancellationToken).ConfigureAwait(false);
460+
}
457461
}
458462
Count++;
459463
return true;

RedditSharpTests/Things/SubredditTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Linq;
2+
using System.Threading;
23
using System.Threading.Tasks;
34
using Xunit;
45

@@ -57,5 +58,21 @@ public async Task PageComments() {
5758
Assert.Equal(55, comments.Count);
5859

5960
}
61+
62+
[Fact]
63+
public async Task StreamComments()
64+
{
65+
RedditSharp.WebAgent agent = new RedditSharp.WebAgent(authFixture.AccessToken);
66+
RedditSharp.Reddit reddit = new RedditSharp.Reddit(agent, true);
67+
68+
var count = 0;
69+
var comments = reddit.RSlashAll.GetComments().GetEnumerator(50, 100, true);
70+
while (await comments.MoveNext(CancellationToken.None))
71+
{
72+
count++;
73+
}
74+
75+
Assert.Equal(100, count);
76+
}
6077
}
6178
}

0 commit comments

Comments
 (0)