mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-09-16 15:22:27 +02:00
44 lines
1.3 KiB
C#
44 lines
1.3 KiB
C#
using Robust.Shared.Interfaces.Timers;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using Robust.Shared.Exceptions;
|
|
using Robust.Shared.IoC;
|
|
using Robust.Shared.Timing;
|
|
|
|
namespace Robust.Shared.Timers
|
|
{
|
|
internal sealed class TimerManager : ITimerManager
|
|
{
|
|
#pragma warning disable 649
|
|
[Dependency] private readonly IRuntimeLog _runtimeLog;
|
|
#pragma warning restore 649
|
|
|
|
private readonly List<(Timer, CancellationToken)> _timers
|
|
= new List<(Timer, CancellationToken)>();
|
|
|
|
public void AddTimer(Timer timer, CancellationToken cancellationToken = default)
|
|
{
|
|
_timers.Add((timer, cancellationToken));
|
|
}
|
|
|
|
public void UpdateTimers(FrameEventArgs frameEventArgs)
|
|
{
|
|
// Manual for loop so we can modify the list while enumerating.
|
|
// ReSharper disable once ForCanBeConvertedToForeach
|
|
for (var i = 0; i < _timers.Count; i++)
|
|
{
|
|
var (timer, cancellationToken) = _timers[i];
|
|
|
|
if (cancellationToken.IsCancellationRequested)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
timer.Update(frameEventArgs.DeltaSeconds, _runtimeLog);
|
|
}
|
|
|
|
_timers.RemoveAll(timer => !timer.Item1.IsActive || timer.Item2.IsCancellationRequested);
|
|
}
|
|
}
|
|
}
|