blob: c51016adcf86a9722819472e2758825836c0e922 (
plain)
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
|
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
namespace StardewModdingAPI.Framework
{
/// <summary>A thread-safe command queue optimized for infrequent changes.</summary>
internal class CommandQueue
{
/********
** Fields
********/
/// <summary>The underlying list of queued commands to parse and execute.</summary>
private readonly List<string> RawCommandQueue = new();
/********
** Public methods
********/
/// <summary>Add a command to the queue.</summary>
/// <param name="command">The command to add.</param>
public void Add(string command)
{
lock (this.RawCommandQueue)
this.RawCommandQueue.Add(command);
}
/// <summary>Remove and return all queued commands, if any.</summary>
/// <param name="queued">The commands that were dequeued, in the order they were originally queued.</param>
/// <returns>Returns whether any values were dequeued.</returns>
[SuppressMessage("ReSharper", "InconsistentlySynchronizedField", Justification = "Deliberately check if it's empty before locking unnecessarily.")]
public bool TryDequeue([NotNullWhen(true)] out string[]? queued)
{
if (this.RawCommandQueue.Count is 0)
{
queued = null;
return false;
}
lock (this.RawCommandQueue)
{
queued = this.RawCommandQueue.ToArray();
this.RawCommandQueue.Clear();
return queued.Length > 0;
}
}
}
}
|