Skip to content

Commit e1ae6e1

Browse files
committed
Implement real async instead of Task.Run wrappers
1 parent 035025d commit e1ae6e1

13 files changed

+171
-157
lines changed

src/MiniExcel/Csv/CsvReader.cs

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
using System.Text.RegularExpressions;
99
using System.Threading;
1010
using System.Threading.Tasks;
11+
using System.Runtime.CompilerServices;
1112

1213
namespace MiniExcelLibs.Csv
1314
{
14-
internal class CsvReader : IExcelReader
15+
internal partial class CsvReader : IExcelReader
1516
{
1617
private Stream _stream;
1718
private CsvConfiguration _config;
@@ -22,7 +23,8 @@ public CsvReader(Stream stream, IConfiguration configuration)
2223
_config = configuration == null ? CsvConfiguration.DefaultConfiguration : (CsvConfiguration)configuration;
2324
}
2425

25-
public IEnumerable<IDictionary<string, object>> Query(bool useHeaderRow, string sheetName, string startCell)
26+
[Zomp.SyncMethodGenerator.CreateSyncVersion]
27+
public async IAsyncEnumerable<IDictionary<string, object>> QueryAsync(bool useHeaderRow, string sheetName, string startCell, [EnumeratorCancellation] CancellationToken ct = default)
2628
{
2729
if (startCell != "A1")
2830
throw new NotImplementedException("CSV does not implement parameter startCell");
@@ -35,14 +37,22 @@ public IEnumerable<IDictionary<string, object>> Query(bool useHeaderRow, string
3537
var headRows = new Dictionary<int, string>();
3638

3739
string row;
38-
for (var rowIndex = 1; (row = reader.ReadLine()) != null; rowIndex++)
40+
for (var rowIndex = 1; (row = await reader.ReadLineAsync(
41+
#if NET7_0_OR_GREATER
42+
ct
43+
#endif
44+
)) != null; rowIndex++)
3945
{
4046
string finalRow = row;
4147
if (_config.ReadLineBreaksWithinQuotes)
4248
{
4349
while (finalRow.Count(c => c == '"') % 2 != 0)
4450
{
45-
var nextPart = reader.ReadLine();
51+
var nextPart = await reader.ReadLineAsync(
52+
#if NET7_0_OR_GREATER
53+
ct
54+
#endif
55+
);
4656
if (nextPart == null)
4757
{
4858
break;
@@ -107,10 +117,11 @@ public IEnumerable<IDictionary<string, object>> Query(bool useHeaderRow, string
107117
}
108118
}
109119

110-
public IEnumerable<T> Query<T>(string sheetName, string startCell, bool hasHeader) where T : class, new()
120+
[Zomp.SyncMethodGenerator.CreateSyncVersion]
121+
public IAsyncEnumerable<T> QueryAsync<T>(string sheetName, string startCell, bool hasHeader, CancellationToken ct = default) where T : class, new()
111122
{
112-
var dynamicRecords = Query(false, sheetName, startCell);
113-
return ExcelOpenXmlSheetReader.QueryImpl<T>(dynamicRecords, startCell, hasHeader, _config);
123+
var dynamicRecords = QueryAsync(false, sheetName, startCell, ct);
124+
return ExcelOpenXmlSheetReader.QueryImplAsync<T>(dynamicRecords, startCell, hasHeader, _config);
114125
}
115126

116127
public IEnumerable<IDictionary<string, object>> QueryRange(bool useHeaderRow, string sheetName, string startCell, string endCell)
@@ -135,36 +146,6 @@ public IEnumerable<IDictionary<string, object>> QueryRange(bool useHeaderRow, st
135146
return ExcelOpenXmlSheetReader.QueryImpl<T>(dynamicRecords, ReferenceHelper.ConvertXyToCell(startRowIndex, startColumnIndex), hasHeader, this._config);
136147
}
137148

138-
public Task<IEnumerable<IDictionary<string, object>>> QueryAsync(bool useHeaderRow, string sheetName, string startCell, CancellationToken cancellationToken = default)
139-
{
140-
return Task.Run(() => Query(useHeaderRow, sheetName, startCell), cancellationToken);
141-
}
142-
143-
public async Task<IEnumerable<T>> QueryAsync<T>(string sheetName, string startCell, bool hasHeader, CancellationToken cancellationToken = default) where T : class, new()
144-
{
145-
return await Task.Run(() => Query<T>(sheetName, startCell, hasHeader), cancellationToken).ConfigureAwait(false);
146-
}
147-
148-
public Task<IEnumerable<IDictionary<string, object>>> QueryRangeAsync(bool useHeaderRow, string sheetName, string startCell, string endCel, CancellationToken cancellationToken = default)
149-
{
150-
return Task.Run(() => QueryRange(useHeaderRow, sheetName, startCell, endCel), cancellationToken);
151-
}
152-
153-
public async Task<IEnumerable<T>> QueryRangeAsync<T>(string sheetName, string startCell, string endCel, bool hasHeader, CancellationToken cancellationToken = default) where T : class, new()
154-
{
155-
return await Task.Run(() => QueryRange<T>(sheetName, startCell, endCel, hasHeader), cancellationToken).ConfigureAwait(false);
156-
}
157-
158-
public Task<IEnumerable<IDictionary<string, object>>> QueryRangeAsync(bool useHeaderRow, string sheetName, int startRowIndex, int startColumnIndex, int? endRowIndex, int? endColumnIndex, CancellationToken cancellationToken = default)
159-
{
160-
return Task.Run(() => QueryRange(useHeaderRow, sheetName, startRowIndex, startColumnIndex, endRowIndex, endColumnIndex), cancellationToken);
161-
}
162-
163-
public async Task<IEnumerable<T>> QueryRangeAsync<T>(string sheetName, int startRowIndex, int startColumnIndex, int? endRowIndex, int? endColumnIndex, bool hasHeader, CancellationToken cancellationToken = default) where T : class, new()
164-
{
165-
return await Task.Run(() => QueryRange<T>(sheetName, startRowIndex, startColumnIndex, endRowIndex, endColumnIndex, hasHeader), cancellationToken).ConfigureAwait(false);
166-
}
167-
168149
private string[] Split(string row)
169150
{
170151
if (_config.SplitFn != null)

src/MiniExcel/IExcelReader.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,11 @@ internal interface IExcelReader : IDisposable
99
{
1010
IEnumerable<IDictionary<string, object>> Query(bool useHeaderRow, string sheetName, string startCell);
1111
IEnumerable<T> Query<T>(string sheetName, string startCell, bool hasHeader) where T : class, new();
12-
Task<IEnumerable<IDictionary<string, object>>> QueryAsync(bool useHeaderRow, string sheetName, string startCell, CancellationToken cancellationToken = default);
13-
Task<IEnumerable<T>> QueryAsync<T>(string sheetName, string startCell, bool hasHeader, CancellationToken cancellationToken = default) where T : class, new();
12+
IAsyncEnumerable<IDictionary<string, object>> QueryAsync(bool useHeaderRow, string sheetName, string startCell, CancellationToken cancellationToken = default);
13+
IAsyncEnumerable<T> QueryAsync<T>(string sheetName, string startCell, bool hasHeader, CancellationToken cancellationToken = default) where T : class, new();
1414
IEnumerable<IDictionary<string, object>> QueryRange(bool useHeaderRow, string sheetName, string startCell, string endCell);
1515
IEnumerable<T> QueryRange<T>(string sheetName, string startCell, string endCell, bool hasHeader) where T : class, new();
16-
Task<IEnumerable<IDictionary<string, object>>> QueryRangeAsync(bool useHeaderRow, string sheetName, string startCell, string endCell, CancellationToken cancellationToken = default);
17-
Task<IEnumerable<T>> QueryRangeAsync<T>(string sheetName, string startCell, string endCell, bool hasHeader, CancellationToken cancellationToken = default) where T : class, new();
1816
IEnumerable<IDictionary<string, object>> QueryRange(bool useHeaderRow, string sheetName, int startRowIndex, int startColumnIndex, int? endRowIndex, int? endColumnIndex);
1917
IEnumerable<T> QueryRange<T>(string sheetName, int startRowIndex, int startColumnIndex, int? endRowIndex, int? endColumnIndex, bool hasHeader) where T : class, new();
20-
Task<IEnumerable<IDictionary<string, object>>> QueryRangeAsync(bool useHeaderRow, string sheetName, int startRowIndex, int startColumnIndex, int? endRowIndex, int? endColumnIndex, CancellationToken cancellationToken = default);
21-
Task<IEnumerable<T>> QueryRangeAsync<T>(string sheetName, int startRowIndex, int startColumnIndex, int? endRowIndex, int? endColumnIndex, bool hasHeader, CancellationToken cancellationToken = default) where T : class, new();
2218
}
2319
}

src/MiniExcel/MiniExcel.Async.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ public static async Task<IEnumerable<dynamic>> QueryAsync(string path, bool useH
8686
return await Task.Run(() => Query(path, useHeaderRow, sheetName, excelType, startCell, configuration), cancellationToken);
8787
}
8888

89-
public static async Task<IEnumerable<T>> QueryAsync<T>(this Stream stream, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null, CancellationToken cancellationToken = default, bool hasHeader = true) where T : class, new()
89+
public static IAsyncEnumerable<T> QueryAsync<T>(this Stream stream, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null, bool hasHeader = true, CancellationToken cancellationToken = default) where T : class, new()
9090
{
91-
return await ExcelReaderFactory.GetProvider(stream, ExcelTypeHelper.GetExcelType(stream, excelType), configuration).QueryAsync<T>(sheetName, startCell, hasHeader, cancellationToken).ConfigureAwait(false);
91+
return ExcelReaderFactory.GetProvider(stream, ExcelTypeHelper.GetExcelType(stream, excelType), configuration).QueryAsync<T>(sheetName, startCell, hasHeader, cancellationToken);
9292
}
9393

9494
public static async Task<IEnumerable<T>> QueryAsync<T>(string path, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null, CancellationToken cancellationToken = default, bool hasHeader = true) where T : class, new()

src/MiniExcel/MiniExcelLibs.csproj

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFrameworks>net45;netstandard2.0;net8.0;net9.0</TargetFrameworks>
3+
<TargetFrameworks>net462;netstandard2.0;net8.0;net9.0</TargetFrameworks>
44
<Version>1.41.2</Version>
55
</PropertyGroup>
6-
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
6+
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'net462'">
77
<LangVersion>8</LangVersion>
88
</PropertyGroup>
99
<PropertyGroup>
@@ -38,7 +38,7 @@ Todo : https://github.com/mini-software/MiniExcel/projects/1?fullscreen=true</De
3838
<PackageReadmeFile>README.md</PackageReadmeFile>
3939
</PropertyGroup>
4040

41-
<ItemGroup Condition=" '$(TargetFramework)' == 'net45'">
41+
<ItemGroup Condition=" '$(TargetFramework)' == 'net462'">
4242
<Reference Include="System.IO.Compression" />
4343
</ItemGroup>
4444
<ItemGroup>
@@ -52,7 +52,13 @@ Todo : https://github.com/mini-software/MiniExcel/projects/1?fullscreen=true</De
5252
<ItemGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
5353
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
5454
</ItemGroup>
55-
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
55+
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'net462'">
5656
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="9.0.0" />
5757
</ItemGroup>
58+
<ItemGroup>
59+
<PackageReference Include="Zomp.SyncMethodGenerator" Version="1.4.27">
60+
<PrivateAssets>all</PrivateAssets>
61+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
62+
</PackageReference>
63+
</ItemGroup>
5864
</Project>

src/MiniExcel/MiniExcelTask.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ namespace MiniExcelLibs
66
{
77
internal class MiniExcelTask
88
{
9-
#if NET45
9+
#if NET462
1010
public static Task CompletedTask = Task.FromResult(0);
1111
#else
1212
public static Task CompletedTask = Task.CompletedTask;
1313
#endif
1414

1515
public static Task FromException(Exception exception)
1616
{
17-
#if NET45
17+
#if NET462
1818
var tcs = new TaskCompletionSource<object>();
1919
tcs.SetException(exception);
2020
return tcs.Task;
@@ -25,7 +25,7 @@ public static Task FromException(Exception exception)
2525

2626
public static Task<T> FromException<T>(Exception exception)
2727
{
28-
#if NET45
28+
#if NET462
2929
var tcs = new TaskCompletionSource<T>();
3030
tcs.SetException(exception);
3131
return tcs.Task;
@@ -36,7 +36,7 @@ public static Task<T> FromException<T>(Exception exception)
3636

3737
public static Task FromCanceled(CancellationToken cancellationToken)
3838
{
39-
#if NET45
39+
#if NET462
4040
var tcs = new TaskCompletionSource<object>();
4141
cancellationToken.Register(() => tcs.SetCanceled());
4242
return tcs.Task;
@@ -47,7 +47,7 @@ public static Task FromCanceled(CancellationToken cancellationToken)
4747

4848
public static Task<T> FromCanceled<T>(CancellationToken cancellationToken)
4949
{
50-
#if NET45
50+
#if NET462
5151
var tcs = new TaskCompletionSource<T>();
5252
cancellationToken.Register(() => tcs.SetCanceled());
5353
return tcs.Task;

src/MiniExcel/OpenXml/ExcelOpenXmlSheetReader.Async.cs

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)