~ruther/NosSmooth.Comms

ref: ecfc8cd7dc9143e9550044f3f5971bf8631f51b7 NosSmooth.Comms/src/Local/NosSmooth.Comms.Inject/MessageResponders/FocusResponder.cs -rw-r--r-- 2.0 KiB
ecfc8cd7 — Rutherther feat: add local injectable and injector to allow making connections to nostale processes 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
//
//  FocusResponder.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.LocalData;
using NosSmooth.LocalBinding;
using NosSmooth.LocalBinding.Hooks;
using NosSmooth.LocalBinding.Structs;
using Remora.Results;

namespace NosSmooth.Comms.Inject.MessageResponders;

/// <summary>
/// A resopnder to <see cref="FocusResponder"/>.
/// </summary>
public class FocusResponder : IMessageResponder<FocusMessage>
{
    private readonly NosBrowserManager _browserManager;
    private readonly NosThreadSynchronizer _synchronizer;
    private readonly IEntityFocusHook _focusHook;

    /// <summary>
    /// Initializes a new instance of the <see cref="FocusResponder"/> class.
    /// </summary>
    /// <param name="browserManager">The browser manager.</param>
    /// <param name="synchronizer">The synchronizer.</param>
    /// <param name="focusHook">The focus hook.</param>
    public FocusResponder
        (NosBrowserManager browserManager, NosThreadSynchronizer synchronizer, IEntityFocusHook focusHook)
    {
        _browserManager = browserManager;
        _synchronizer = synchronizer;
        _focusHook = focusHook;
    }

    /// <inheritdoc />
    public async Task<Result> Respond(FocusMessage message, CancellationToken ct = default)
    {
        MapBaseObj? entity = null;
        if (message.EntityId is not null)
        {
            var entityResult = _browserManager.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
        (
            () =>
            {
                _focusHook.WrapperFunction(entity);
                return Result.FromSuccess();
            },
            ct
        );
    }
}
Do not follow this link