Skip to content

Commit b87b248

Browse files
authored
Update to .NET Core 3.1 and apply all necessary changes to IAsyncEnumerable implementations that comes with this update. (#239)
1 parent 1c45db7 commit b87b248

File tree

7 files changed

+26
-43
lines changed

7 files changed

+26
-43
lines changed

RedditSharp.sln

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RedditSharpTests", "RedditS
1515
EndProject
1616
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTesting", "UnitTesting\UnitTesting.csproj", "{D164656E-D0E6-41A0-ADEA-37C55E34531B}"
1717
EndProject
18-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTesting_old", "UnitTesting_old\UnitTesting_old.csproj", "{A4640FC9-F8A5-4B11-A9FE-8FEB626B2080}"
19-
EndProject
2018
Global
2119
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2220
Debug|Any CPU = Debug|Any CPU
@@ -63,18 +61,6 @@ Global
6361
{D164656E-D0E6-41A0-ADEA-37C55E34531B}.Release|Mixed Platforms.Build.0 = Release|Any CPU
6462
{D164656E-D0E6-41A0-ADEA-37C55E34531B}.Release|x86.ActiveCfg = Release|Any CPU
6563
{D164656E-D0E6-41A0-ADEA-37C55E34531B}.Release|x86.Build.0 = Release|Any CPU
66-
{A4640FC9-F8A5-4B11-A9FE-8FEB626B2080}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
67-
{A4640FC9-F8A5-4B11-A9FE-8FEB626B2080}.Debug|Any CPU.Build.0 = Debug|Any CPU
68-
{A4640FC9-F8A5-4B11-A9FE-8FEB626B2080}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
69-
{A4640FC9-F8A5-4B11-A9FE-8FEB626B2080}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
70-
{A4640FC9-F8A5-4B11-A9FE-8FEB626B2080}.Debug|x86.ActiveCfg = Debug|Any CPU
71-
{A4640FC9-F8A5-4B11-A9FE-8FEB626B2080}.Debug|x86.Build.0 = Debug|Any CPU
72-
{A4640FC9-F8A5-4B11-A9FE-8FEB626B2080}.Release|Any CPU.ActiveCfg = Release|Any CPU
73-
{A4640FC9-F8A5-4B11-A9FE-8FEB626B2080}.Release|Any CPU.Build.0 = Release|Any CPU
74-
{A4640FC9-F8A5-4B11-A9FE-8FEB626B2080}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
75-
{A4640FC9-F8A5-4B11-A9FE-8FEB626B2080}.Release|Mixed Platforms.Build.0 = Release|Any CPU
76-
{A4640FC9-F8A5-4B11-A9FE-8FEB626B2080}.Release|x86.ActiveCfg = Release|Any CPU
77-
{A4640FC9-F8A5-4B11-A9FE-8FEB626B2080}.Release|x86.Build.0 = Release|Any CPU
7864
EndGlobalSection
7965
GlobalSection(SolutionProperties) = preSolution
8066
HideSolutionNode = FALSE

RedditSharp/CommentsEnumerable.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public CommentsEnumarable(IWebAgent agent, Post post, int limitPerRequest = 0)
3434
/// Returns <see cref="IAsyncEnumerator{T}"/> for the comments on the <see cref="Post"/>>
3535
/// </summary>
3636
/// <returns></returns>
37-
public IAsyncEnumerator<Comment> GetEnumerator()
37+
public IAsyncEnumerator<Comment> GetAsyncEnumerator(CancellationToken cancellationToken = default)
3838
{
3939
return new CommentsEnumerator(agent, post, limit);
4040
}
@@ -67,7 +67,7 @@ public Comment Current
6767
}
6868
}
6969

70-
public async Task<bool> MoveNext(CancellationToken cancellationToken)
70+
public async ValueTask<bool> MoveNextAsync()
7171
{
7272

7373
if (currentIndex == -1)
@@ -139,9 +139,9 @@ private async Task GetBaseComments()
139139
currentBranch = retrieved;
140140
}
141141

142-
public void Dispose()
142+
public ValueTask DisposeAsync()
143143
{
144-
144+
return new ValueTask();
145145
}
146146
}
147147
}

RedditSharp/Listing.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public IAsyncEnumerator<T> GetEnumerator(int limitPerRequest, int maximumLimit =
173173
}
174174

175175
/// <inheritdoc/>
176-
public IAsyncEnumerator<T> GetEnumerator()
176+
public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)
177177
{
178178
return GetEnumerator(LimitPerRequest, MaximumLimit, IsStream);
179179
}
@@ -365,26 +365,25 @@ private void Parse(JToken json)
365365
Before = json["data"]["before"].Value<string>();
366366
}
367367

368-
public void Dispose()
368+
public ValueTask DisposeAsync()
369369
{
370-
// ...
370+
return default;
371371
}
372372

373-
public async Task<bool> MoveNext(CancellationToken cancellationToken)
373+
public async ValueTask<bool> MoveNextAsync()
374374
{
375375
if (stream)
376376
{
377-
return await MoveNextForwardAsync(cancellationToken).ConfigureAwait(false);
377+
return await MoveNextForwardAsync().ConfigureAwait(false);
378378
}
379379
else
380380
{
381-
return await MoveNextBackAsync(cancellationToken).ConfigureAwait(false);
381+
return await MoveNextBackAsync().ConfigureAwait(false);
382382
}
383383
}
384384

385-
private async Task<bool> MoveNextBackAsync(CancellationToken cancellationToken)
385+
private async Task<bool> MoveNextBackAsync()
386386
{
387-
cancellationToken.ThrowIfCancellationRequested();
388387
if (CurrentIndex == -1)
389388
{
390389
//first call, get a page and set CurrentIndex
@@ -421,7 +420,7 @@ private async Task<bool> MoveNextBackAsync(CancellationToken cancellationToken)
421420
return true;
422421
}
423422

424-
private async Task<bool> MoveNextForwardAsync(CancellationToken cancellationToken)
423+
private async Task<bool> MoveNextForwardAsync()
425424
{
426425
CurrentIndex++;
427426

@@ -435,7 +434,6 @@ private async Task<bool> MoveNextForwardAsync(CancellationToken cancellationToke
435434
int tries = 0;
436435
while (true)
437436
{
438-
cancellationToken.ThrowIfCancellationRequested();
439437
tries++;
440438

441439
try
@@ -446,7 +444,7 @@ private async Task<bool> MoveNextForwardAsync(CancellationToken cancellationToke
446444
catch (Exception ex)
447445
{
448446
// sleep for a while to see if we can recover
449-
await Sleep(tries, cancellationToken, ex).ConfigureAwait(false);
447+
await Sleep(tries, ex).ConfigureAwait(false);
450448
}
451449

452450
// the page is only populated if there are *new* items to yielded from the listing.
@@ -456,14 +454,14 @@ private async Task<bool> MoveNextForwardAsync(CancellationToken cancellationToke
456454
}
457455

458456
// No listings were returned in the page.
459-
await Sleep(tries, cancellationToken).ConfigureAwait(false);
457+
await Sleep(tries).ConfigureAwait(false);
460458
}
461459
}
462460
Count++;
463461
return true;
464462
}
465463

466-
private async Task Sleep(int tries, CancellationToken cancellationToken, Exception ex = null)
464+
private async Task Sleep(int tries, Exception ex = null)
467465
{
468466
// wait up to 3 minutes between tries
469467
// TODO: Make this configurable
@@ -481,7 +479,7 @@ private async Task Sleep(int tries, CancellationToken cancellationToken, Excepti
481479
{
482480
seconds = tries * 5;
483481
}
484-
await Task.Delay(seconds * 1000, cancellationToken).ConfigureAwait(false);
482+
await Task.Delay(seconds * 1000).ConfigureAwait(false);
485483
}
486484
}
487485
#pragma warning restore

RedditSharp/RedditSharp.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
<PropertyGroup>
44
<AssemblyTitle>RedditSharp</AssemblyTitle>
55
<VersionPrefix>2.0.0</VersionPrefix>
6-
<TargetFrameworks Condition="'$(LibraryFrameworks)'==''">net462;netstandard1.3;netstandard2.0;</TargetFrameworks>
7-
<TargetFrameworks Condition="'$(LibraryFrameworks)'!=''">$(LibraryFrameworks)</TargetFrameworks>
6+
<TargetFramework>netcoreapp3.1</TargetFramework>
87
<AssemblyName>RedditSharp</AssemblyName>
98
<PackageId>RedditSharp</PackageId>
109
<PackageProjectUrl>https://github.com/CrustyJew/RedditSharp</PackageProjectUrl>
@@ -21,7 +20,7 @@
2120
<PackageReference Include="System.Runtime.Serialization.Formatters" Version="4.3.0" />
2221
<PackageReference Include="System.Threading.Thread" Version="4.3.0" />
2322
<PackageReference Include="System.Reactive.Linq" Version="3.1.1" />
24-
<PackageReference Include="System.Interactive.Async" Version="3.1.1" />
23+
<PackageReference Include="System.Interactive.Async" Version="4.1.1" />
2524
</ItemGroup>
2625

2726
<ItemGroup Condition=" '$(TargetFramework)' == 'net462' ">

RedditSharp/Things/PrivateMessage.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ public PrivateMessage GetParent()
131131
return null;
132132
}
133133
//TODO: Convert this into an async function
134-
var firstPage = thread.First();
135-
firstPage.Wait();
134+
var firstPage = thread.FirstAsync();
135+
firstPage.GetAwaiter();
136136
var firstMessage = firstPage.Result;
137137
if (firstMessage?.FullName == ParentID)
138138
return firstMessage;

RedditSharpTests/RedditSharpTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp2.2</TargetFramework>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
55
<AssemblyName>RedditSharpTests</AssemblyName>
66
<PackageId>RedditSharpTests</PackageId>
77
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>

RedditSharpTests/Things/SubredditTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public async Task GetContributors()
2020
RedditSharp.WebAgent agent = new RedditSharp.WebAgent(authFixture.AccessToken);
2121
RedditSharp.Reddit reddit = new RedditSharp.Reddit(agent);
2222
var sub = await reddit.GetSubredditAsync(authFixture.Config["TestSubreddit"]);
23-
var contribs = await sub.GetContributors().ToList();
23+
var contribs = await sub.GetContributors().ToListAsync();
2424

2525
Assert.NotEmpty(contribs);
2626
Assert.Contains<string>(authFixture.TestUserName.ToLower(), contribs.Select(c => c.Name.ToLower()));
@@ -56,8 +56,8 @@ public async Task GetRALLComments()
5656
RedditSharp.WebAgent agent = new RedditSharp.WebAgent(authFixture.AccessToken);
5757
RedditSharp.Reddit reddit = new RedditSharp.Reddit(agent, true);
5858

59-
var comments = reddit.RSlashAll.GetComments(5);
60-
Assert.Equal(5, await comments.Count());
59+
var comments = await reddit.RSlashAll.GetComments().Take(5).CountAsync();
60+
Assert.Equal(5, comments);
6161
}
6262

6363
[Fact]
@@ -66,7 +66,7 @@ public async Task PageComments()
6666
RedditSharp.WebAgent agent = new RedditSharp.WebAgent(authFixture.AccessToken);
6767
RedditSharp.Reddit reddit = new RedditSharp.Reddit(agent, true);
6868

69-
var comments = await reddit.RSlashAll.GetComments().Take(55).ToList();
69+
var comments = await reddit.RSlashAll.GetComments().Take(55).ToListAsync();
7070

7171
Assert.Equal(55, comments.Count);
7272

@@ -80,7 +80,7 @@ public async Task StreamComments()
8080

8181
var count = 0;
8282
var comments = reddit.RSlashAll.GetComments().GetEnumerator(50, 100, true);
83-
while (await comments.MoveNext(CancellationToken.None))
83+
while (await comments.MoveNextAsync())
8484
{
8585
count++;
8686
}

0 commit comments

Comments
 (0)