Browse Source

Add updated AsyncEnumerable implementation

pull/1382/head
Hawx 6 years ago
parent
commit
1a78a86fe6
13 changed files with 30 additions and 25 deletions
  1. +1
    -1
      src/Discord.Net.Analyzers/Discord.Net.Analyzers.csproj
  2. +2
    -2
      src/Discord.Net.Commands/Discord.Net.Commands.csproj
  3. +1
    -1
      src/Discord.Net.Commands/Readers/UserTypeReader.cs
  4. +3
    -3
      src/Discord.Net.Core/Discord.Net.Core.csproj
  5. +1
    -1
      src/Discord.Net.Core/Extensions/AsyncEnumerableExtensions.cs
  6. +11
    -5
      src/Discord.Net.Core/Utils/Paging/PagedEnumerator.cs
  7. +2
    -2
      src/Discord.Net.Rest/Discord.Net.Rest.csproj
  8. +2
    -2
      src/Discord.Net.WebSocket/Discord.Net.WebSocket.csproj
  9. +1
    -1
      src/Discord.Net.Webhook/Discord.Net.Webhook.csproj
  10. +4
    -4
      src/Discord.Net/Discord.Net.nuspec
  11. +1
    -1
      test/Discord.Net.Tests.Unit/Discord.Net.Tests.Unit.csproj
  12. +1
    -1
      test/Discord.Net.Tests.Unit/EmbedBuilderTests.cs
  13. +0
    -1
      test/Discord.Net.Tests.Unit/MockedEntities/MockedGroupChannel.cs

+ 1
- 1
src/Discord.Net.Analyzers/Discord.Net.Analyzers.csproj View File

@@ -4,7 +4,7 @@
<AssemblyName>Discord.Net.Analyzers</AssemblyName> <AssemblyName>Discord.Net.Analyzers</AssemblyName>
<RootNamespace>Discord.Analyzers</RootNamespace> <RootNamespace>Discord.Analyzers</RootNamespace>
<Description>A Discord.Net extension adding support for design-time analysis of the API usage.</Description> <Description>A Discord.Net extension adding support for design-time analysis of the API usage.</Description>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>netstandard2.0;netstandard2.1</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis" Version="3.3.1" /> <PackageReference Include="Microsoft.CodeAnalysis" Version="3.3.1" />


+ 2
- 2
src/Discord.Net.Commands/Discord.Net.Commands.csproj View File

@@ -4,8 +4,8 @@
<AssemblyName>Discord.Net.Commands</AssemblyName> <AssemblyName>Discord.Net.Commands</AssemblyName>
<RootNamespace>Discord.Commands</RootNamespace> <RootNamespace>Discord.Commands</RootNamespace>
<Description>A Discord.Net extension adding support for bot commands.</Description> <Description>A Discord.Net extension adding support for bot commands.</Description>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">net461;netstandard2.0</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">netstandard2.0</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">net461;netstandard2.0;netstandard2.1</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">netstandard2.0;netstandard2.1</TargetFrameworks>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Discord.Net.Core\Discord.Net.Core.csproj" /> <ProjectReference Include="..\Discord.Net.Core\Discord.Net.Core.csproj" />


+ 1
- 1
src/Discord.Net.Commands/Readers/UserTypeReader.cs View File

@@ -49,7 +49,7 @@ namespace Discord.Commands
string username = input.Substring(0, index); string username = input.Substring(0, index);
if (ushort.TryParse(input.Substring(index + 1), out ushort discriminator)) if (ushort.TryParse(input.Substring(index + 1), out ushort discriminator))
{ {
var channelUser = await channelUsers.FirstOrDefault(x => x.DiscriminatorValue == discriminator &&
var channelUser = await channelUsers.FirstOrDefaultAsync(x => x.DiscriminatorValue == discriminator &&
string.Equals(username, x.Username, StringComparison.OrdinalIgnoreCase)).ConfigureAwait(false); string.Equals(username, x.Username, StringComparison.OrdinalIgnoreCase)).ConfigureAwait(false);
AddResult(results, channelUser as T, channelUser?.Username == username ? 0.85f : 0.75f); AddResult(results, channelUser as T, channelUser?.Username == username ? 0.85f : 0.75f);




+ 3
- 3
src/Discord.Net.Core/Discord.Net.Core.csproj View File

@@ -4,13 +4,13 @@
<AssemblyName>Discord.Net.Core</AssemblyName> <AssemblyName>Discord.Net.Core</AssemblyName>
<RootNamespace>Discord</RootNamespace> <RootNamespace>Discord</RootNamespace>
<Description>The core components for the Discord.Net library.</Description> <Description>The core components for the Discord.Net library.</Description>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">net461;netstandard2.0</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">netstandard2.0</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">net461;netstandard2.0;netstandard2.1</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">netstandard2.0;netstandard2.1</TargetFrameworks>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" /> <PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="System.Collections.Immutable" Version="1.3.1" /> <PackageReference Include="System.Collections.Immutable" Version="1.3.1" />
<PackageReference Include="System.Interactive.Async" Version="3.2.0" />
<PackageReference Include="System.Interactive.Async" Version="4.0.0" />
<PackageReference Include="IDisposableAnalyzers" Version="2.1.2"> <PackageReference Include="IDisposableAnalyzers" Version="2.1.2">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </PackageReference>


+ 1
- 1
src/Discord.Net.Core/Extensions/AsyncEnumerableExtensions.cs View File

@@ -15,7 +15,7 @@ namespace Discord
/// <summary> Flattens the specified pages into one <see cref="IEnumerable{T}"/> asynchronously. </summary> /// <summary> Flattens the specified pages into one <see cref="IEnumerable{T}"/> asynchronously. </summary>
public static async Task<IEnumerable<T>> FlattenAsync<T>(this IAsyncEnumerable<IEnumerable<T>> source) public static async Task<IEnumerable<T>> FlattenAsync<T>(this IAsyncEnumerable<IEnumerable<T>> source)
{ {
return await source.Flatten().ToArray().ConfigureAwait(false);
return await source.Flatten().ToArrayAsync().ConfigureAwait(false);
} }
/// <summary> Flattens the specified pages into one <see cref="IAsyncEnumerable{T}"/>. </summary> /// <summary> Flattens the specified pages into one <see cref="IAsyncEnumerable{T}"/>. </summary>
public static IAsyncEnumerable<T> Flatten<T>(this IAsyncEnumerable<IEnumerable<T>> source) public static IAsyncEnumerable<T> Flatten<T>(this IAsyncEnumerable<IEnumerable<T>> source)


+ 11
- 5
src/Discord.Net.Core/Utils/Paging/PagedEnumerator.cs View File

@@ -25,26 +25,28 @@ namespace Discord
_nextPage = nextPage; _nextPage = nextPage;
} }


public IAsyncEnumerator<IReadOnlyCollection<T>> GetEnumerator() => new Enumerator(this);
public IAsyncEnumerator<IReadOnlyCollection<T>> GetAsyncEnumerator(CancellationToken cancellationToken = new CancellationToken()) => new Enumerator(this, cancellationToken);
internal class Enumerator : IAsyncEnumerator<IReadOnlyCollection<T>> internal class Enumerator : IAsyncEnumerator<IReadOnlyCollection<T>>
{ {
private readonly PagedAsyncEnumerable<T> _source; private readonly PagedAsyncEnumerable<T> _source;
private readonly CancellationToken _token;
private readonly PageInfo _info; private readonly PageInfo _info;


public IReadOnlyCollection<T> Current { get; private set; } public IReadOnlyCollection<T> Current { get; private set; }


public Enumerator(PagedAsyncEnumerable<T> source)
public Enumerator(PagedAsyncEnumerable<T> source, CancellationToken token)
{ {
_source = source; _source = source;
_token = token;
_info = new PageInfo(source._start, source._count, source.PageSize); _info = new PageInfo(source._start, source._count, source.PageSize);
} }


public async Task<bool> MoveNext(CancellationToken cancelToken)
public async ValueTask<bool> MoveNextAsync()
{ {
if (_info.Remaining == 0) if (_info.Remaining == 0)
return false; return false;


var data = await _source._getPage(_info, cancelToken).ConfigureAwait(false);
var data = await _source._getPage(_info, _token).ConfigureAwait(false);
Current = new Page<T>(_info, data); Current = new Page<T>(_info, data);


_info.Page++; _info.Page++;
@@ -71,7 +73,11 @@ namespace Discord
return true; return true;
} }
public void Dispose() { Current = null; }
public ValueTask DisposeAsync()
{
Current = null;
return default;
}
} }
} }
} }

+ 2
- 2
src/Discord.Net.Rest/Discord.Net.Rest.csproj View File

@@ -4,8 +4,8 @@
<AssemblyName>Discord.Net.Rest</AssemblyName> <AssemblyName>Discord.Net.Rest</AssemblyName>
<RootNamespace>Discord.Rest</RootNamespace> <RootNamespace>Discord.Rest</RootNamespace>
<Description>A core Discord.Net library containing the REST client and models.</Description> <Description>A core Discord.Net library containing the REST client and models.</Description>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">net461;netstandard2.0</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">netstandard2.0</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">net461;netstandard2.0;netstandard2.1</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">netstandard2.0;netstandard2.1</TargetFrameworks>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Discord.Net.Core\Discord.Net.Core.csproj" /> <ProjectReference Include="..\Discord.Net.Core\Discord.Net.Core.csproj" />


+ 2
- 2
src/Discord.Net.WebSocket/Discord.Net.WebSocket.csproj View File

@@ -4,8 +4,8 @@
<AssemblyName>Discord.Net.WebSocket</AssemblyName> <AssemblyName>Discord.Net.WebSocket</AssemblyName>
<RootNamespace>Discord.WebSocket</RootNamespace> <RootNamespace>Discord.WebSocket</RootNamespace>
<Description>A core Discord.Net library containing the WebSocket client and models.</Description> <Description>A core Discord.Net library containing the WebSocket client and models.</Description>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">net461;netstandard2.0</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">netstandard2.0</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">net461;netstandard2.0;netstandard2.1</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">netstandard2.0;netstandard2.1</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>


+ 1
- 1
src/Discord.Net.Webhook/Discord.Net.Webhook.csproj View File

@@ -4,7 +4,7 @@
<AssemblyName>Discord.Net.Webhook</AssemblyName> <AssemblyName>Discord.Net.Webhook</AssemblyName>
<RootNamespace>Discord.Webhook</RootNamespace> <RootNamespace>Discord.Webhook</RootNamespace>
<Description>A core Discord.Net library containing the Webhook client and models.</Description> <Description>A core Discord.Net library containing the Webhook client and models.</Description>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Discord.Net.Core\Discord.Net.Core.csproj" /> <ProjectReference Include="..\Discord.Net.Core\Discord.Net.Core.csproj" />


+ 4
- 4
src/Discord.Net/Discord.Net.nuspec View File

@@ -13,21 +13,21 @@
<requireLicenseAcceptance>false</requireLicenseAcceptance> <requireLicenseAcceptance>false</requireLicenseAcceptance>
<iconUrl>https://github.com/RogueException/Discord.Net/raw/dev/docs/marketing/logo/PackageLogo.png</iconUrl> <iconUrl>https://github.com/RogueException/Discord.Net/raw/dev/docs/marketing/logo/PackageLogo.png</iconUrl>
<dependencies> <dependencies>
<group targetFramework="net46">
<group targetFramework="net461">
<dependency id="Discord.Net.Core" version="2.2.0-dev$suffix$" /> <dependency id="Discord.Net.Core" version="2.2.0-dev$suffix$" />
<dependency id="Discord.Net.Rest" version="2.2.0-dev$suffix$" /> <dependency id="Discord.Net.Rest" version="2.2.0-dev$suffix$" />
<dependency id="Discord.Net.WebSocket" version="2.2.0-dev$suffix$" /> <dependency id="Discord.Net.WebSocket" version="2.2.0-dev$suffix$" />
<dependency id="Discord.Net.Commands" version="2.2.0-dev$suffix$" /> <dependency id="Discord.Net.Commands" version="2.2.0-dev$suffix$" />
<dependency id="Discord.Net.Webhook" version="2.2.0-dev$suffix$" /> <dependency id="Discord.Net.Webhook" version="2.2.0-dev$suffix$" />
</group>
<group targetFramework="netstandard1.3">
</group>
<group targetFramework="netstandard2.0">
<dependency id="Discord.Net.Core" version="2.2.0-dev$suffix$" /> <dependency id="Discord.Net.Core" version="2.2.0-dev$suffix$" />
<dependency id="Discord.Net.Rest" version="2.2.0-dev$suffix$" /> <dependency id="Discord.Net.Rest" version="2.2.0-dev$suffix$" />
<dependency id="Discord.Net.WebSocket" version="2.2.0-dev$suffix$" /> <dependency id="Discord.Net.WebSocket" version="2.2.0-dev$suffix$" />
<dependency id="Discord.Net.Commands" version="2.2.0-dev$suffix$" /> <dependency id="Discord.Net.Commands" version="2.2.0-dev$suffix$" />
<dependency id="Discord.Net.Webhook" version="2.2.0-dev$suffix$" /> <dependency id="Discord.Net.Webhook" version="2.2.0-dev$suffix$" />
</group> </group>
<group targetFramework="netstandard2.0">
<group targetFramework="netstandard2.1">
<dependency id="Discord.Net.Core" version="2.2.0-dev$suffix$" /> <dependency id="Discord.Net.Core" version="2.2.0-dev$suffix$" />
<dependency id="Discord.Net.Rest" version="2.2.0-dev$suffix$" /> <dependency id="Discord.Net.Rest" version="2.2.0-dev$suffix$" />
<dependency id="Discord.Net.WebSocket" version="2.2.0-dev$suffix$" /> <dependency id="Discord.Net.WebSocket" version="2.2.0-dev$suffix$" />


+ 1
- 1
test/Discord.Net.Tests.Unit/Discord.Net.Tests.Unit.csproj View File

@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">


<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework> <TargetFramework>netcoreapp2.1</TargetFramework>


+ 1
- 1
test/Discord.Net.Tests.Unit/EmbedBuilderTests.cs View File

@@ -250,7 +250,7 @@ namespace Discord
.WithFooter("This is the footer", url) .WithFooter("This is the footer", url)
.WithImageUrl(url) .WithImageUrl(url)
.WithThumbnailUrl(url) .WithThumbnailUrl(url)
.WithTimestamp(DateTime.MinValue)
.WithTimestamp(DateTimeOffset.MinValue)
.WithTitle("This is the title") .WithTitle("This is the title")
.WithUrl(url) .WithUrl(url)
.AddField("Field 1", "Inline", true) .AddField("Field 1", "Inline", true)


+ 0
- 1
test/Discord.Net.Tests.Unit/MockedEntities/MockedGroupChannel.cs View File

@@ -1,7 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord.Audio; using Discord.Audio;




Loading…
Cancel
Save