~ruther/NosSmooth

ref: 994aad0e431e34fae3d1bc656a436ae07de98efd NosSmooth/Tests/NosSmooth.Core.Tests/Fakes/Packets/Events/PacketEvent.cs -rw-r--r-- 2.2 KiB
994aad0e — František Boháček feat(packets): make vnum int 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
//  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;

/// <summary>
/// A fake packet event.
/// </summary>
public class PacketEvent : IPreExecutionEvent, IPostExecutionEvent
{
    private readonly Func<INostaleClient, PacketSource, IPacket, string, Result> _preHandler;
    private readonly Func<INostaleClient, PacketSource, IPacket, string, IReadOnlyList<Result>, Result> _postHandler;

    /// <summary>
    /// Initializes a new instance of the <see cref="PacketEvent"/> class.
    /// </summary>
    /// <param name="preHandler">The pre handler.</param>
    /// <param name="postHandler">The post handler.</param>
    public PacketEvent
    (
        Func<INostaleClient, PacketSource, IPacket, string, Result> preHandler,
        Func<INostaleClient, PacketSource, IPacket, string, IReadOnlyList<Result>, Result> postHandler
    )
    {
        _preHandler = preHandler;
        _postHandler = postHandler;

    }

    /// <inheritdoc />
    public Task<Result> ExecuteBeforeExecutionAsync<TPacket>
        (INostaleClient client, PacketEventArgs<TPacket> packetArgs, CancellationToken ct = default)
        where TPacket : IPacket
        => Task.FromResult(_preHandler(client, packetArgs.Source, packetArgs.Packet, packetArgs.PacketString));

    /// <inheritdoc />
    public Task<Result> ExecuteAfterExecutionAsync<TPacket>
    (
        INostaleClient client,
        PacketEventArgs<TPacket> packetArgs,
        IReadOnlyList<Result> executionResults,
        CancellationToken ct = default
    )
        where TPacket : IPacket
        => Task.FromResult
        (
            _postHandler
            (
                client,
                packetArgs.Source,
                packetArgs.Packet,
                packetArgs.PacketString,
                executionResults
            )
        );
}
Do not follow this link