Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/GameUtils/Types/Collections/SynchronizedCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public abstract class SynchronizedCollection<T> : IEnumerable<T> where T : notnu
{
private readonly ConcurrentQueue<Operation<T>> _pending = new();
private readonly SemaphoreSlim _integrating = new(1, 1);
private IReadOnlyList<T>? _cachedSnapshot;
private bool _dirty = true;

/// <summary>
/// Schedules an entity to be added to the collection.
Expand All @@ -57,9 +59,16 @@ public void Integrate()
_integrating.Wait();
try
{
bool modified = false;
while (_pending.TryDequeue(out var operation))
{
HandleOperation(operation);
modified = true;
}

if (modified)
{
_dirty = true;
}
}
finally
Expand Down Expand Up @@ -101,7 +110,13 @@ public IEnumerable<T> Get()
_integrating.Wait();
try
{
return GetInternal().ToList();
if (_dirty || _cachedSnapshot is null)
{
_cachedSnapshot = GetInternal().ToList();
_dirty = false;
}

return _cachedSnapshot;
}
finally
{
Expand Down
Loading