//
// PacketEvent.cs
//
// Copyright (c) František Boháček. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using NosSmooth.Core.Client;
using NosSmooth.Core.Packets;
using NosSmooth.Packets;
using NosSmooth.PacketSerializer.Abstractions.Attributes;
using Remora.Results;
namespace NosSmooth.Core.Tests.Fakes.Packets.Events;
///
/// A fake packet event.
///
public class PacketEvent : IPreExecutionEvent, IPostExecutionEvent
{
private readonly Func _preHandler;
private readonly Func, Result> _postHandler;
///
/// Initializes a new instance of the class.
///
/// The pre handler.
/// The post handler.
public PacketEvent
(
Func preHandler,
Func, Result> postHandler
)
{
_preHandler = preHandler;
_postHandler = postHandler;
}
///
public Task ExecuteBeforeExecutionAsync
(INostaleClient client, PacketEventArgs packetArgs, CancellationToken ct = default)
where TPacket : IPacket
=> Task.FromResult(_preHandler(client, packetArgs.Source, packetArgs.Packet, packetArgs.PacketString));
///
public Task ExecuteAfterExecutionAsync
(
INostaleClient client,
PacketEventArgs packetArgs,
IReadOnlyList executionResults,
CancellationToken ct = default
)
where TPacket : IPacket
=> Task.FromResult
(
_postHandler
(
client,
packetArgs.Source,
packetArgs.Packet,
packetArgs.PacketString,
executionResults
)
);
}