Files
MinFt/Client/Assets/Scripts/TimeManager/TimeTickService.cs
2026-04-27 12:07:32 +08:00

51 lines
1.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using UniRx;
namespace TimeManager
{
/// <summary>
/// 统一 Tick 服务实现 - 管理秒级和分钟级的定时器。
/// 构造时即启动 Interval须在业务启动链中显式 <c>Resolve&lt;TimeRefreshManager&gt;()</c>(如 ActivityManager.Init 开头),以挂上跨天/跨小时逻辑。
/// Stop 后需再调 Start() 恢复。
/// </summary>
public class TimeTickService : ITimeTickService, IDisposable
{
private IObservable<long> _secondTick;
private IObservable<long> _minuteTick;
private bool _isStarted;
public TimeTickService()
{
EnsureStarted();
}
public IObservable<long> SecondTick => _secondTick;
public IObservable<long> MinuteTick => _minuteTick;
public void Start() => EnsureStarted();
private void EnsureStarted()
{
if (_isStarted) return;
_secondTick = Observable.Interval(TimeSpan.FromSeconds(1f));
_minuteTick = Observable.Interval(TimeSpan.FromMinutes(1f));
_isStarted = true;
}
public void Stop()
{
_secondTick = null;
_minuteTick = null;
_isStarted = false;
}
public void Dispose()
{
Stop();
}
}
}