Skip to content

Commit ddeb5e8

Browse files
committed
✨ 优化API 添加移除等基础函数
1 parent 65a9532 commit ddeb5e8

File tree

4 files changed

+25
-28
lines changed

4 files changed

+25
-28
lines changed

Runtime/Executor/TimerExecutor.cs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -118,18 +118,6 @@ public virtual bool UpdateLoop()
118118
return true;
119119
}
120120

121-
if (Loop == 1 || Loop == 0)
122-
{
123-
Number = Number + 1; //次数增加
124-
CurrentTime = Watch.ElapsedMilliseconds;
125-
Interval = CurrentTime - Number * Duration;
126-
Watch.Stop();
127-
Watch = null;
128-
if (TID != 0 && TimerSystem.TimerExecutors.ContainsKey(TID))
129-
TimerSystem.TimerExecutors.Remove(TID);
130-
return false; //达到次数
131-
}
132-
133121
if (Loop == -1) //无限循环
134122
{
135123
Number = Number + 1; //次数增加
@@ -140,13 +128,16 @@ public virtual bool UpdateLoop()
140128
return true;
141129
}
142130

143-
if (Loop == -2) //不执行回调结束
131+
if (Loop == 1 || Loop == 0)
144132
{
145-
Watch.Stop();
146-
Watch = null;
147-
return false;
133+
Number = Number + 1; //次数增加
134+
CurrentTime = Watch.ElapsedMilliseconds;
135+
Interval = CurrentTime - Number * Duration;
148136
}
149137

138+
Watch.Stop();
139+
Watch = null;
140+
if (TID != 0 && TimerSystem.Exist(TID)) TimerSystem.Pop(TID);
150141
return false;
151142
}
152143

@@ -157,10 +148,7 @@ public int CompareTo(ITimerExecutor other)
157148
return 0;
158149
}
159150

160-
public void Dispose()
161-
{
162-
Delegates = null;
163-
}
151+
public void Dispose() { Delegates = null; }
164152

165153
public sealed override string ToString()
166154
{
@@ -192,4 +180,4 @@ public void Execute()
192180

193181
protected abstract void xExecute();
194182
}
195-
}
183+
}

Runtime/Operator/TimerOperator.Loop.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,4 @@ public override void OtherUpdate(long nowTime)
103103
}
104104
}
105105
}
106-
}
106+
}

Runtime/System/TimerSystem.Exist.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#region
22

33
using System;
4+
using System.Collections.Generic;
45

56
#endregion
67

@@ -21,6 +22,14 @@ partial class TimerSystem
2122
/// <summary>
2223
/// 判断任务是否存在
2324
/// </summary>
24-
public static bool Exist<E>(E key) where E : Enum { return TimerExecutors.ContainsKey(key.GetHashCode()); }
25+
public static bool Exist<E>(E key)
26+
where E : Enum => TimerExecutors.ContainsKey(key.GetHashCode());
27+
28+
public static ITimerExecutor Get(long key) { return TimerExecutors.GetValueOrDefault(key, null); }
29+
30+
public static ITimerExecutor Get(string key) { return TimerExecutors.GetValueOrDefault(key.GetHashCode(), null); }
31+
32+
public static ITimerExecutor Get<E>(E key)
33+
where E : Enum => TimerExecutors.GetValueOrDefault(key.GetHashCode(), null);
2534
}
26-
}
35+
}

Runtime/System/TimerSystem.Pop.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ partial class TimerSystem
1515
/// <param name="key"> 任务ID </param>
1616
public static void Pop(long key)
1717
{
18-
if (!TimerExecutors.TryGetValue(key, out var executor)) return;
18+
if (!TimerExecutors.Remove(key, out var executor)) return;
1919
executor.Loop = -2;
20-
TimerExecutors.Remove(key);
2120
}
2221

2322
/// <summary>
@@ -34,6 +33,7 @@ public static void Pop(string key)
3433
/// 取出循环任务
3534
/// </summary>
3635
/// <param name="key"> 任务ID </param>
37-
public static void Pop<E>(E key) where E : Enum { Pop(key.GetHashCode()); }
36+
public static void Pop<E>(E key)
37+
where E : Enum => Pop(key.GetHashCode());
3838
}
39-
}
39+
}

0 commit comments

Comments
 (0)