mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2026-02-10 20:02:31 +00:00
Refactor
This commit is contained in:
@@ -1,8 +1,3 @@
|
||||
using System;
|
||||
|
||||
namespace DiscordChatExporter.Core.Exceptions;
|
||||
|
||||
// Thrown when there is circumstancially no message to export with given parameters,
|
||||
// though it should not be treated as a runtime error; simply warn instead
|
||||
public class ChannelEmptyException(string message)
|
||||
: DiscordChatExporterException(message, false, null) { }
|
||||
public class ChannelEmptyException(string message) : DiscordChatExporterException(message);
|
||||
|
||||
@@ -31,7 +31,8 @@ public class ChannelExporter(DiscordClient discord)
|
||||
var context = new ExportContext(discord, request);
|
||||
await context.PopulateChannelsAndRolesAsync(cancellationToken);
|
||||
|
||||
// Export messages
|
||||
// Initialize the exporter before further checks to ensure the file is created even if
|
||||
// an exception is thrown after this point.
|
||||
await using var messageExporter = new MessageExporter(context);
|
||||
|
||||
// Check if the channel is empty
|
||||
|
||||
@@ -13,24 +13,7 @@ internal partial class MessageExporter(ExportContext context) : IAsyncDisposable
|
||||
|
||||
public long MessagesExported { get; private set; }
|
||||
|
||||
private async ValueTask ResetWriterAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (_writer is not null)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _writer.WritePostambleAsync(cancellationToken);
|
||||
}
|
||||
// Writer must be disposed, even if it fails to write the postamble
|
||||
finally
|
||||
{
|
||||
await _writer.DisposeAsync();
|
||||
_writer = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async ValueTask<MessageWriter> GetWriterAsync(
|
||||
private async ValueTask<MessageWriter> InitializeWriterAsync(
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
@@ -43,7 +26,7 @@ internal partial class MessageExporter(ExportContext context) : IAsyncDisposable
|
||||
)
|
||||
)
|
||||
{
|
||||
await ResetWriterAsync(cancellationToken);
|
||||
await UninitializeWriterAsync(cancellationToken);
|
||||
_partitionIndex++;
|
||||
}
|
||||
|
||||
@@ -60,21 +43,40 @@ internal partial class MessageExporter(ExportContext context) : IAsyncDisposable
|
||||
return _writer = writer;
|
||||
}
|
||||
|
||||
private async ValueTask UninitializeWriterAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (_writer is not null)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _writer.WritePostambleAsync(cancellationToken);
|
||||
}
|
||||
// Writer must be disposed, even if it fails to write the postamble
|
||||
finally
|
||||
{
|
||||
await _writer.DisposeAsync();
|
||||
_writer = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask ExportMessageAsync(
|
||||
Message message,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
var writer = await GetWriterAsync(cancellationToken);
|
||||
var writer = await InitializeWriterAsync(cancellationToken);
|
||||
await writer.WriteMessageAsync(message, cancellationToken);
|
||||
MessagesExported++;
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
// causes the file to be created whether there were messages written or not
|
||||
await GetWriterAsync();
|
||||
await ResetWriterAsync();
|
||||
// If not messages were written, force the creation of an empty file
|
||||
if (MessagesExported <= 0)
|
||||
_ = await InitializeWriterAsync();
|
||||
|
||||
await UninitializeWriterAsync();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user