using System;
using System.Collections.Generic;
namespace XGame.Framework.Time
{
///
/// 定时器仓储
///
public class TimerRepository : IClockTick, IDisposable
{
private readonly List _aliveTimers = new List();
internal ITimer AddDelayTimer(int delay, Action action)
{
var timer = new Timer(delay, delay, 1, action);
_aliveTimers.Add(timer);
return timer;
}
internal ITimer AddLooperTimer(int delay, int interval, Action action, int loopTime = -1)
{
var timer = new Timer(delay, interval, loopTime, action);
_aliveTimers.Add(timer);
return timer;
}
public void ClockUpdate(int millisecond)
{
// 客户端定时器数量较少不考虑时间轮优化
for (int i = _aliveTimers.Count - 1; i >= 0; --i)
{
if (_aliveTimers[i].Update(millisecond))
{
_aliveTimers.RemoveAt(i);
}
}
}
public void Dispose()
{
foreach (var item in _aliveTimers)
{
item.Dispose();
}
_aliveTimers.Clear();
}
}
}