mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2026-03-12 09:53:04 +00:00
Use ValueTask instead of Task where possible
This commit is contained in:
@@ -18,7 +18,7 @@ namespace DiscordChatExporter.Domain.Exporting
|
||||
|
||||
public ChannelExporter(AuthToken token) : this(new DiscordClient(token)) {}
|
||||
|
||||
public async Task ExportChannelAsync(ExportRequest request, IProgress<double>? progress = null)
|
||||
public async ValueTask ExportChannelAsync(ExportRequest request, IProgress<double>? progress = null)
|
||||
{
|
||||
// Build context
|
||||
var contextMembers = new HashSet<Member>(IdBasedEqualityComparer.Instance);
|
||||
|
||||
@@ -55,7 +55,7 @@ namespace DiscordChatExporter.Domain.Exporting
|
||||
}
|
||||
|
||||
// HACK: ConfigureAwait() is crucial here to enable sync-over-async in HtmlMessageWriter
|
||||
public async Task<string> ResolveMediaUrlAsync(string url)
|
||||
public async ValueTask<string> ResolveMediaUrlAsync(string url)
|
||||
{
|
||||
if (!Request.ShouldDownloadMedia)
|
||||
return url;
|
||||
|
||||
@@ -22,7 +22,7 @@ namespace DiscordChatExporter.Domain.Exporting
|
||||
}
|
||||
|
||||
// HACK: ConfigureAwait() is crucial here to enable sync-over-async in HtmlMessageWriter
|
||||
public async Task<string> DownloadAsync(string url)
|
||||
public async ValueTask<string> DownloadAsync(string url)
|
||||
{
|
||||
if (_mediaPathMap.TryGetValue(url, out var cachedFilePath))
|
||||
return cachedFilePath;
|
||||
|
||||
@@ -25,7 +25,7 @@ namespace DiscordChatExporter.Domain.Exporting
|
||||
_context.Request.PartitionLimit != 0 &&
|
||||
_messageCount % _context.Request.PartitionLimit == 0;
|
||||
|
||||
private async Task ResetWriterAsync()
|
||||
private async ValueTask ResetWriterAsync()
|
||||
{
|
||||
if (_writer != null)
|
||||
{
|
||||
@@ -35,7 +35,7 @@ namespace DiscordChatExporter.Domain.Exporting
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<MessageWriter> GetWriterAsync()
|
||||
private async ValueTask<MessageWriter> GetWriterAsync()
|
||||
{
|
||||
// Ensure partition limit has not been exceeded
|
||||
if (IsPartitionLimitReached())
|
||||
@@ -60,7 +60,7 @@ namespace DiscordChatExporter.Domain.Exporting
|
||||
return _writer = writer;
|
||||
}
|
||||
|
||||
public async Task ExportMessageAsync(Message message)
|
||||
public async ValueTask ExportMessageAsync(Message message)
|
||||
{
|
||||
var writer = await GetWriterAsync();
|
||||
await writer.WriteMessageAsync(message);
|
||||
|
||||
@@ -21,10 +21,10 @@ namespace DiscordChatExporter.Domain.Exporting.Writers
|
||||
private string FormatMarkdown(string? markdown) =>
|
||||
PlainTextMarkdownVisitor.Format(Context, markdown ?? "");
|
||||
|
||||
public override async Task WritePreambleAsync() =>
|
||||
public override async ValueTask WritePreambleAsync() =>
|
||||
await _writer.WriteLineAsync("AuthorID,Author,Date,Content,Attachments,Reactions");
|
||||
|
||||
private async Task WriteAttachmentsAsync(IReadOnlyList<Attachment> attachments)
|
||||
private async ValueTask WriteAttachmentsAsync(IReadOnlyList<Attachment> attachments)
|
||||
{
|
||||
var buffer = new StringBuilder();
|
||||
|
||||
@@ -38,7 +38,7 @@ namespace DiscordChatExporter.Domain.Exporting.Writers
|
||||
await _writer.WriteAsync(CsvEncode(buffer.ToString()));
|
||||
}
|
||||
|
||||
private async Task WriteReactionsAsync(IReadOnlyList<Reaction> reactions)
|
||||
private async ValueTask WriteReactionsAsync(IReadOnlyList<Reaction> reactions)
|
||||
{
|
||||
var buffer = new StringBuilder();
|
||||
|
||||
@@ -56,7 +56,7 @@ namespace DiscordChatExporter.Domain.Exporting.Writers
|
||||
await _writer.WriteAsync(CsvEncode(buffer.ToString()));
|
||||
}
|
||||
|
||||
public override async Task WriteMessageAsync(Message message)
|
||||
public override async ValueTask WriteMessageAsync(Message message)
|
||||
{
|
||||
// Author ID
|
||||
await _writer.WriteAsync(CsvEncode(message.Author.Id));
|
||||
|
||||
@@ -102,7 +102,7 @@ namespace DiscordChatExporter.Domain.Exporting.Writers
|
||||
private string FormatMarkdown(string? markdown, bool isJumboAllowed = true) =>
|
||||
HtmlMarkdownVisitor.Format(Context, markdown ?? "", isJumboAllowed);
|
||||
|
||||
private async Task WriteCurrentMessageGroupAsync()
|
||||
private async ValueTask WriteCurrentMessageGroupAsync()
|
||||
{
|
||||
var templateContext = CreateTemplateContext(new Dictionary<string, object>
|
||||
{
|
||||
@@ -112,13 +112,13 @@ namespace DiscordChatExporter.Domain.Exporting.Writers
|
||||
await templateContext.EvaluateAsync(_messageGroupTemplate.Page);
|
||||
}
|
||||
|
||||
public override async Task WritePreambleAsync()
|
||||
public override async ValueTask WritePreambleAsync()
|
||||
{
|
||||
var templateContext = CreateTemplateContext();
|
||||
await templateContext.EvaluateAsync(_preambleTemplate.Page);
|
||||
}
|
||||
|
||||
public override async Task WriteMessageAsync(Message message)
|
||||
public override async ValueTask WriteMessageAsync(Message message)
|
||||
{
|
||||
// If message group is empty or the given message can be grouped, buffer the given message
|
||||
if (!_messageGroupBuffer.Any() || MessageGroup.CanJoin(_messageGroupBuffer.Last(), message))
|
||||
@@ -138,7 +138,7 @@ namespace DiscordChatExporter.Domain.Exporting.Writers
|
||||
_messageCount++;
|
||||
}
|
||||
|
||||
public override async Task WritePostambleAsync()
|
||||
public override async ValueTask WritePostambleAsync()
|
||||
{
|
||||
// Flush current message group
|
||||
if (_messageGroupBuffer.Any())
|
||||
|
||||
@@ -25,7 +25,7 @@ namespace DiscordChatExporter.Domain.Exporting.Writers
|
||||
private string FormatMarkdown(string? markdown) =>
|
||||
PlainTextMarkdownVisitor.Format(Context, markdown ?? "");
|
||||
|
||||
private async Task WriteAttachmentAsync(Attachment attachment)
|
||||
private async ValueTask WriteAttachmentAsync(Attachment attachment)
|
||||
{
|
||||
_writer.WriteStartObject();
|
||||
|
||||
@@ -38,7 +38,7 @@ namespace DiscordChatExporter.Domain.Exporting.Writers
|
||||
await _writer.FlushAsync();
|
||||
}
|
||||
|
||||
private async Task WriteEmbedAuthorAsync(EmbedAuthor embedAuthor)
|
||||
private async ValueTask WriteEmbedAuthorAsync(EmbedAuthor embedAuthor)
|
||||
{
|
||||
_writer.WriteStartObject("author");
|
||||
|
||||
@@ -52,7 +52,7 @@ namespace DiscordChatExporter.Domain.Exporting.Writers
|
||||
await _writer.FlushAsync();
|
||||
}
|
||||
|
||||
private async Task WriteEmbedThumbnailAsync(EmbedImage embedThumbnail)
|
||||
private async ValueTask WriteEmbedThumbnailAsync(EmbedImage embedThumbnail)
|
||||
{
|
||||
_writer.WriteStartObject("thumbnail");
|
||||
|
||||
@@ -66,7 +66,7 @@ namespace DiscordChatExporter.Domain.Exporting.Writers
|
||||
await _writer.FlushAsync();
|
||||
}
|
||||
|
||||
private async Task WriteEmbedImageAsync(EmbedImage embedImage)
|
||||
private async ValueTask WriteEmbedImageAsync(EmbedImage embedImage)
|
||||
{
|
||||
_writer.WriteStartObject("image");
|
||||
|
||||
@@ -80,7 +80,7 @@ namespace DiscordChatExporter.Domain.Exporting.Writers
|
||||
await _writer.FlushAsync();
|
||||
}
|
||||
|
||||
private async Task WriteEmbedFooterAsync(EmbedFooter embedFooter)
|
||||
private async ValueTask WriteEmbedFooterAsync(EmbedFooter embedFooter)
|
||||
{
|
||||
_writer.WriteStartObject("footer");
|
||||
|
||||
@@ -93,7 +93,7 @@ namespace DiscordChatExporter.Domain.Exporting.Writers
|
||||
await _writer.FlushAsync();
|
||||
}
|
||||
|
||||
private async Task WriteEmbedFieldAsync(EmbedField embedField)
|
||||
private async ValueTask WriteEmbedFieldAsync(EmbedField embedField)
|
||||
{
|
||||
_writer.WriteStartObject();
|
||||
|
||||
@@ -105,7 +105,7 @@ namespace DiscordChatExporter.Domain.Exporting.Writers
|
||||
await _writer.FlushAsync();
|
||||
}
|
||||
|
||||
private async Task WriteEmbedAsync(Embed embed)
|
||||
private async ValueTask WriteEmbedAsync(Embed embed)
|
||||
{
|
||||
_writer.WriteStartObject();
|
||||
|
||||
@@ -138,7 +138,7 @@ namespace DiscordChatExporter.Domain.Exporting.Writers
|
||||
await _writer.FlushAsync();
|
||||
}
|
||||
|
||||
private async Task WriteReactionAsync(Reaction reaction)
|
||||
private async ValueTask WriteReactionAsync(Reaction reaction)
|
||||
{
|
||||
_writer.WriteStartObject();
|
||||
|
||||
@@ -156,7 +156,7 @@ namespace DiscordChatExporter.Domain.Exporting.Writers
|
||||
await _writer.FlushAsync();
|
||||
}
|
||||
|
||||
public override async Task WritePreambleAsync()
|
||||
public override async ValueTask WritePreambleAsync()
|
||||
{
|
||||
// Root object (start)
|
||||
_writer.WriteStartObject();
|
||||
@@ -188,7 +188,7 @@ namespace DiscordChatExporter.Domain.Exporting.Writers
|
||||
await _writer.FlushAsync();
|
||||
}
|
||||
|
||||
public override async Task WriteMessageAsync(Message message)
|
||||
public override async ValueTask WriteMessageAsync(Message message)
|
||||
{
|
||||
_writer.WriteStartObject();
|
||||
|
||||
@@ -241,7 +241,7 @@ namespace DiscordChatExporter.Domain.Exporting.Writers
|
||||
_messageCount++;
|
||||
}
|
||||
|
||||
public override async Task WritePostambleAsync()
|
||||
public override async ValueTask WritePostambleAsync()
|
||||
{
|
||||
// Message array (end)
|
||||
_writer.WriteEndArray();
|
||||
|
||||
@@ -17,11 +17,11 @@ namespace DiscordChatExporter.Domain.Exporting.Writers
|
||||
Context = context;
|
||||
}
|
||||
|
||||
public virtual Task WritePreambleAsync() => Task.CompletedTask;
|
||||
public virtual ValueTask WritePreambleAsync() => default;
|
||||
|
||||
public abstract Task WriteMessageAsync(Message message);
|
||||
public abstract ValueTask WriteMessageAsync(Message message);
|
||||
|
||||
public virtual Task WritePostambleAsync() => Task.CompletedTask;
|
||||
public virtual ValueTask WritePostambleAsync() => default;
|
||||
|
||||
public virtual async ValueTask DisposeAsync() => await Stream.DisposeAsync();
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ namespace DiscordChatExporter.Domain.Exporting.Writers
|
||||
private string FormatMarkdown(string? markdown) =>
|
||||
PlainTextMarkdownVisitor.Format(Context, markdown ?? "");
|
||||
|
||||
private async Task WriteMessageHeaderAsync(Message message)
|
||||
private async ValueTask WriteMessageHeaderAsync(Message message)
|
||||
{
|
||||
// Timestamp & author
|
||||
await _writer.WriteAsync($"[{message.Timestamp.ToLocalString(Context.Request.DateFormat)}]");
|
||||
@@ -37,7 +37,7 @@ namespace DiscordChatExporter.Domain.Exporting.Writers
|
||||
await _writer.WriteLineAsync();
|
||||
}
|
||||
|
||||
private async Task WriteAttachmentsAsync(IReadOnlyList<Attachment> attachments)
|
||||
private async ValueTask WriteAttachmentsAsync(IReadOnlyList<Attachment> attachments)
|
||||
{
|
||||
if (!attachments.Any())
|
||||
return;
|
||||
@@ -50,7 +50,7 @@ namespace DiscordChatExporter.Domain.Exporting.Writers
|
||||
await _writer.WriteLineAsync();
|
||||
}
|
||||
|
||||
private async Task WriteEmbedsAsync(IReadOnlyList<Embed> embeds)
|
||||
private async ValueTask WriteEmbedsAsync(IReadOnlyList<Embed> embeds)
|
||||
{
|
||||
foreach (var embed in embeds)
|
||||
{
|
||||
@@ -90,7 +90,7 @@ namespace DiscordChatExporter.Domain.Exporting.Writers
|
||||
}
|
||||
}
|
||||
|
||||
private async Task WriteReactionsAsync(IReadOnlyList<Reaction> reactions)
|
||||
private async ValueTask WriteReactionsAsync(IReadOnlyList<Reaction> reactions)
|
||||
{
|
||||
if (!reactions.Any())
|
||||
return;
|
||||
@@ -110,7 +110,7 @@ namespace DiscordChatExporter.Domain.Exporting.Writers
|
||||
await _writer.WriteLineAsync();
|
||||
}
|
||||
|
||||
public override async Task WritePreambleAsync()
|
||||
public override async ValueTask WritePreambleAsync()
|
||||
{
|
||||
await _writer.WriteLineAsync('='.Repeat(62));
|
||||
await _writer.WriteLineAsync($"Guild: {Context.Request.Guild.Name}");
|
||||
@@ -129,7 +129,7 @@ namespace DiscordChatExporter.Domain.Exporting.Writers
|
||||
await _writer.WriteLineAsync();
|
||||
}
|
||||
|
||||
public override async Task WriteMessageAsync(Message message)
|
||||
public override async ValueTask WriteMessageAsync(Message message)
|
||||
{
|
||||
await WriteMessageHeaderAsync(message);
|
||||
|
||||
@@ -147,7 +147,7 @@ namespace DiscordChatExporter.Domain.Exporting.Writers
|
||||
_messageCount++;
|
||||
}
|
||||
|
||||
public override async Task WritePostambleAsync()
|
||||
public override async ValueTask WritePostambleAsync()
|
||||
{
|
||||
await _writer.WriteLineAsync('='.Repeat(62));
|
||||
await _writer.WriteLineAsync($"Exported {_messageCount:N0} message(s)");
|
||||
|
||||
Reference in New Issue
Block a user