~ruther/NosTale-PacketLogger

ref: c61f45970d133d6973bfa51d03c9ff4ff5b4e19b NosTale-PacketLogger/src/PacketLogger/Models/Titles/NumberedTitleGenerator.cs -rw-r--r-- 5.2 KiB
c61f4597 — Rutherther fix: make sender title change correctly 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//
//  NumberedTitleGenerator.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.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.JavaScript;
using System.Threading;

namespace PacketLogger.Models.Titles;

/// <summary>
/// A generator and manager of document titles.
/// </summary>
public class NumberedTitleGenerator
{
    private readonly object _lock = new object();
    private readonly ConcurrentDictionary<string, List<Title>> _titles;

    /// <summary>
    /// Initializes a new instance of the <see cref="NumberedTitleGenerator"/> class.
    /// </summary>
    public NumberedTitleGenerator()
    {
        _titles = new ConcurrentDictionary<string, List<Title>>();
    }

    /// <summary>
    /// Add title with given information.
    /// </summary>
    /// <param name="setDocumentTitle">The function used for setting new document title.</param>
    /// <param name="titleChanged">The observable observing changes to the default, unnumbered title.</param>
    /// <param name="initialTitle">The current initial title.</param>
    /// <returns>The handle, title will be removed upon disposal.</returns>
    public TitleHandle AddTitle
    (
        Action<string> setDocumentTitle,
        IObservable<string> titleChanged,
        string initialTitle
    )
    {
        var title = new Title
        (
            setDocumentTitle,
            initialTitle
        );

        title.TitleChanged = titleChanged.Subscribe
        (
            newTitle => HandleTitleChange(title, newTitle)
        );
        HandleTitleChange(title, title.CurrentTitle);

        return new TitleHandle(this, title);
    }

    private void RemoveTitle(Title title)
    {
        _titles.AddOrUpdate
        (
            title.CurrentTitle,
            _ => new List<Title>(),
            (_, u) =>
            {
                u.Remove(title);
                return u;
            }
        );
        UpdateNumbers(title.CurrentTitle);
    }

    private void HandleTitleChange(Title title, string newTitle)
    {
        lock (_lock)
        {
            _titles.AddOrUpdate
            (
                title.CurrentTitle,
                _ => new List<Title>(),
                (_, u) =>
                {
                    u.Remove(title);
                    return u;
                }
            );
            UpdateNumbers(title.CurrentTitle);

            title.CurrentTitle = newTitle;
            _titles.TryAdd(newTitle, new List<Title>());
            _titles.AddOrUpdate
            (
                newTitle,
                _ => new List<Title>(),
                (_, u) =>
                {
                    u.Add(title);
                    return u;
                }
            );
            UpdateNumbers(title.CurrentTitle);
        }
    }

    private void UpdateNumbers(string title)
    {
        if (_titles.TryGetValue(title, out var titles))
        {
            if (titles.Count == 1)
            {
                titles[0].CurrentNumber = 0;
                titles[0].SetDocumentTitle(titles[0].CurrentTitle);
            }
            else if (titles.Count > 1)
            {
                titles[0].CurrentNumber = null;
                titles[0].SetDocumentTitle(titles[0].CurrentTitle);

                for (int i = 1; i < titles.Count; i++)
                {
                    titles[i].CurrentNumber = i;
                    titles[i].SetDocumentTitle($"{titles[i].CurrentTitle} ({i})");
                }
            }
        }
    }

    /// <summary>
    /// A store of a title.
    /// </summary>
    public class TitleHandle : IDisposable
    {
        private readonly NumberedTitleGenerator _titleGenerator;
        private readonly Title _title;

        /// <summary>
        /// Initializes a new instance of the <see cref="TitleHandle"/> class.
        /// </summary>
        /// <param name="titleGenerator">The title generator.</param>
        /// <param name="title">The title.</param>
        public TitleHandle(NumberedTitleGenerator titleGenerator, Title title)
        {
            _titleGenerator = titleGenerator;
            _title = title;
        }

        /// <inheritdoc />
        public void Dispose()
        {
            _title.TitleChanged.Dispose();
            _titleGenerator.RemoveTitle(_title);
        }
    }

    /// <summary>
    /// A title.
    /// </summary>
    /// <param name="SetDocumentTitle">The function used for setting the title of document.</param>
    /// <param name="CurrentTitle">The current title.</param>
    public record Title(Action<string> SetDocumentTitle, string CurrentTitle)
    {
        /// <summary>
        /// Gets or sets the current suffix.
        /// </summary>
        public int? CurrentNumber { get; set; }

        /// <summary>
        /// Gets or sets the disposable title changed observer.
        /// </summary>
        public IDisposable TitleChanged { get; set; } = null!;

        /// <summary>
        /// Gets or sets the current title.
        /// </summary>
        public string CurrentTitle { get; set; } = CurrentTitle;
    }
}
Do not follow this link