Skip to content

Commit

Permalink
Merge pull request #108 from nenoNaninu/support_netstandard2.0
Browse files Browse the repository at this point in the history
support netstandard2.0
  • Loading branch information
nenoNaninu authored Jun 7, 2023
2 parents 0da0607 + 399ed3a commit f81fb37
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
17 changes: 15 additions & 2 deletions src/TypedSignalR.Client.TypeScript.Generator/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,21 @@ private async Task TranspileCore(

typeMapperProvider.AddTypeMapper(new TaskTypeMapper(compilation));
typeMapperProvider.AddTypeMapper(new GenericTaskTypeMapper(compilation));
typeMapperProvider.AddTypeMapper(new AsyncEnumerableTypeMapper(compilation));
typeMapperProvider.AddTypeMapper(new ChannelReaderTypeMapper(compilation));

// By default, netstandard2.0 does not include IAsyncEnumerable<T> and ChannelReader<T>
var asyncEnumerableTypeMapper = new AsyncEnumerableTypeMapper(compilation);

if (asyncEnumerableTypeMapper.IsSupported())
{
typeMapperProvider.AddTypeMapper(asyncEnumerableTypeMapper);
}

var channelReaderTypeMapper = new ChannelReaderTypeMapper(compilation);

if (channelReaderTypeMapper.IsSupported())
{
typeMapperProvider.AddTypeMapper(channelReaderTypeMapper);
}

var options = new TypedSignalRTranspilationOptions(
compilation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,18 @@ public static ITypeSymbol GetFeaturedType(this ITypeSymbol typeSymbol, SpecialSy
var typeArg = namedTypeSymbol.TypeArguments[0] as INamedTypeSymbol;

// Task<IAsyncEnumerable<T>> -> T
// NOTE:
// SymbolEqualityComparer.Default.Equals(null, null) return true,
// so verify that specialSymbols.AsyncEnumerableSymbol is not null first.
if (specialSymbols.AsyncEnumerableSymbol is not null
&& SymbolEqualityComparer.Default.Equals(typeArg?.OriginalDefinition, specialSymbols.AsyncEnumerableSymbol))
{
return typeArg.TypeArguments[0];
}

// Task<ChannelReader<T>> -> T
if (SymbolEqualityComparer.Default.Equals(typeArg?.OriginalDefinition, specialSymbols.AsyncEnumerableSymbol)
|| SymbolEqualityComparer.Default.Equals(typeArg?.OriginalDefinition, specialSymbols.ChannelReaderSymbol))
if (specialSymbols.ChannelReaderSymbol is not null
&& SymbolEqualityComparer.Default.Equals(typeArg?.OriginalDefinition, specialSymbols.ChannelReaderSymbol))
{
return typeArg.TypeArguments[0];
}
Expand Down
8 changes: 4 additions & 4 deletions src/TypedSignalR.Client.TypeScript/SpecialSymbols.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ internal class SpecialSymbols
{
public readonly INamedTypeSymbol TaskSymbol;
public readonly INamedTypeSymbol GenericTaskSymbol;
public readonly INamedTypeSymbol AsyncEnumerableSymbol;
public readonly INamedTypeSymbol ChannelReaderSymbol;
public readonly INamedTypeSymbol CancellationTokenSymbol;
public readonly INamedTypeSymbol? AsyncEnumerableSymbol;
public readonly INamedTypeSymbol? ChannelReaderSymbol;
public readonly ImmutableArray<INamedTypeSymbol> HubAttributeSymbols;
public readonly ImmutableArray<INamedTypeSymbol> ReceiverAttributeSymbols;
public readonly ImmutableArray<INamedTypeSymbol> TranspilationSourceAttributeSymbols;
Expand All @@ -19,9 +19,9 @@ public SpecialSymbols(Compilation compilation)
{
TaskSymbol = compilation.GetTypeByMetadataName("System.Threading.Tasks.Task")!;
GenericTaskSymbol = compilation.GetTypeByMetadataName("System.Threading.Tasks.Task`1")!;
AsyncEnumerableSymbol = compilation.GetTypeByMetadataName("System.Collections.Generic.IAsyncEnumerable`1")!;
ChannelReaderSymbol = compilation.GetTypeByMetadataName("System.Threading.Channels.ChannelReader`1")!;
CancellationTokenSymbol = compilation.GetTypeByMetadataName("System.Threading.CancellationToken")!;
AsyncEnumerableSymbol = compilation.GetTypeByMetadataName("System.Collections.Generic.IAsyncEnumerable`1");
ChannelReaderSymbol = compilation.GetTypeByMetadataName("System.Threading.Channels.ChannelReader`1");

var hubAttributeSymbol = compilation.GetTypesByMetadataName("TypedSignalR.Client.HubAttribute");
var receiverAttributeSymbol = compilation.GetTypesByMetadataName("TypedSignalR.Client.ReceiverAttribute");
Expand Down
10 changes: 10 additions & 0 deletions src/TypedSignalR.Client.TypeScript/TypeMappers/StreamTypeMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public string MapTo(ITypeSymbol typeSymbol, ITranspilationOptions options)

throw new InvalidOperationException($"GenericTaskTypeMapper is not support {typeSymbol.ToDisplayString()}.");
}

public bool IsSupported()
{
return this.Assign is not null;
}
}

public sealed class ChannelReaderTypeMapper : ITypeMapper
Expand All @@ -50,4 +55,9 @@ public string MapTo(ITypeSymbol typeSymbol, ITranspilationOptions options)

throw new InvalidOperationException($"GenericTaskTypeMapper is not support {typeSymbol.ToDisplayString()}.");
}

public bool IsSupported()
{
return this.Assign is not null;
}
}

0 comments on commit f81fb37

Please sign in to comment.