~ruther/NosSmooth.Comms

ref: 2d0c91b61599398f037d97a8d6288736e01082d9 NosSmooth.Comms/src/Local/NosSmooth.Comms.Inject/MessageResponders/FollowResponder.cs -rw-r--r-- 3.0 KiB
2d0c91b6 — František Boháček chore: bump versions 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//
//  FollowResponder.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 NosSmooth.Comms.Data.Responders;
using NosSmooth.Comms.Inject.Messages;
using NosSmooth.LocalBinding;
using NosSmooth.LocalBinding.Errors;
using NosSmooth.LocalBinding.Hooks;
using NosSmooth.LocalBinding.Structs;
using Remora.Results;

namespace NosSmooth.Comms.Inject.MessageResponders;

/// <summary>
/// A responder to <see cref="FollowMessage"/>.
/// </summary>
public class FollowResponder : IMessageResponder<FollowMessage>
{
    private readonly NosBrowserManager _browserManager;
    private readonly NosThreadSynchronizer _synchronizer;
    private readonly IHookManager _hookManager;

    /// <summary>
    /// Initializes a new instance of the <see cref="FollowResponder"/> class.
    /// </summary>
    /// <param name="browserManager">The browser manager.</param>
    /// <param name="synchronizer">The synchronizer.</param>
    /// <param name="hookManager">The hook manager.</param>
    public FollowResponder
        (NosBrowserManager browserManager, NosThreadSynchronizer synchronizer, IHookManager hookManager)
    {
        _browserManager = browserManager;
        _synchronizer = synchronizer;
        _hookManager = hookManager;
    }

    /// <inheritdoc />
    public async Task<Result> Respond(FollowMessage message, CancellationToken ct = default)
    {
        MapBaseObj? entity = null;
        if (!_browserManager.SceneManager.TryGet(out var sceneManager))
        {
            return new OptionalNotPresentError(nameof(SceneManager));
        }

        if (message.EntityId is not null)
        {
            var entityResult = sceneManager.FindEntity(message.EntityId.Value);

            if (!entityResult.IsDefined(out entity))
            {
                return Result.FromError(new NotFoundError($"Entity with id {message.EntityId} not found."));
            }
        }

        return await _synchronizer.SynchronizeAsync
        (
            () =>
            {
                if (entity is null)
                {
                    return _hookManager.EntityUnfollow.MapResult
                    (
                        unfollow => unfollow.WrapperFunction.MapResult
                        (
                            wrapper =>
                            {
                                wrapper();
                                return Result.FromSuccess();
                            }
                        )
                    );
                }

                return _hookManager.EntityFollow.MapResult
                (
                    unfollow => unfollow.WrapperFunction.MapResult
                    (
                        wrapper =>
                        {
                            wrapper(entity);
                            return Result.FromSuccess();
                        }
                    )
                );
            },
            ct
        );
    }
}
Do not follow this link