Skip to content

Commit 07ce3dd

Browse files
authored
Merge pull request #9 from AIO-GAME/1.x
1.x
2 parents 6ded919 + 56eaa24 commit 07ce3dd

37 files changed

+1255
-254
lines changed

.github/README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
"name": "package.openupm.com",
3838
"url": "https://package.openupm.com",
3939
"scopes": [
40-
"com.aio.timer",
41-
"com.aio.runner"
40+
"com.aio.runner",
41+
"com.aio.timer"
4242
]
4343
}
4444
]
@@ -94,6 +94,7 @@ openupm add com.aio.timer
9494
-**支持 自定义时间轮大小**
9595
-**支持 时间轮动态扩容**
9696
-**支持 同时添加1000000+ 定时任务**
97+
-**支持 `int` `string` `enum` `Guid` 作为 循环任务KEY**
9798

9899
## 📚 使用
99100

@@ -127,15 +128,14 @@ public static void Week(ICollection<(long, long, long)> units)
127128
<h4>添加定时任务</h4>
128129

129130
```csharp
130-
TimerSystem.Push(1, () => { Debug.Log("1ms"); });
131-
TimerSystem.Push(2, () => { Debug.Log("2ms"); });
132-
TimerSystem.Push(1000, () => { Debug.Log("2s"); });
133-
```
134-
135-
<h4>添加循环定时任务</h4>
136-
137-
```csharp
138-
TimerSystem.PushLoop(tid, 3, () => { Debug.Log("3ms"); });
131+
// 后台线程
132+
TimerSystem.Push("KEY", 1, () => { Debug.Log("1ms"); }); // 自定义次数 默认为1
133+
TimerSystem.PushOnce("KEY", 2, () => { Debug.Log("2ms"); }); // 一次
134+
TimerSystem.PushLoop("KEY", 1000, () => { Debug.Log("2s"); }); // 循环
135+
// 主线程
136+
TimerSystem.PushMain("KEY", 1, () => { Debug.Log("1ms"); }, 1); // 自定义次数 默认为1
137+
TimerSystem.PushOnceMain("KEY", 2, () => { Debug.Log("2ms"); }); // 一次
138+
TimerSystem.PushLoopMain("KEY", 1000, () => { Debug.Log("2s"); }); // 循环
139139
```
140140

141141
<h4>移除循环定时任务</h4>
@@ -173,4 +173,4 @@ TimerSystem.Pop(tid);
173173

174174
- **谢谢您选择我们的扩展包。**
175175
- **如果此软件包对您有所帮助。**
176-
- **请考虑通过添加⭐来表示支持。**
176+
- **请考虑通过添加⭐来表示支持。**

.github/README_EN.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
"name": "package.openupm.com",
3838
"url": "https://package.openupm.com",
3939
"scopes": [
40-
"com.aio.timer",
41-
"com.aio.runner"
40+
"com.aio.runner",
41+
"com.aio.timer"
4242
]
4343
}
4444
]
@@ -94,6 +94,7 @@ openupm add com.aio.timer
9494
- **Support custom time wheel size.**
9595
- **Support time wheel dynamic expansion.**
9696
- **Support adding 1000000+ timing tasks at the same time.**
97+
- **Support int` `string` `enum` `Guid` as loop task KEY**
9798

9899
## 📚 Usage
99100

@@ -127,14 +128,14 @@ public static void Week(ICollection<(long, long, long)> units)
127128
<h4>Add timing task</h4>
128129

129130
```csharp
130-
TimerSystem.Push(1, () => { Debug.Log("1s"); });
131-
TimerSystem.Push(2, () => { Debug.Log("2s"); });
132-
```
133-
134-
<h4>Remove timing task</h4>
135-
136-
```csharp
137-
TimerSystem.PushLoop(tid, 3, () => { Debug.Log("3s"); });
131+
// Sub-thread
132+
TimerSystem.Push("KEY", 1, () => { Debug.Log("1ms"); }); // Custom times default is 1
133+
TimerSystem.PushOnce("KEY", 2, () => { Debug.Log("2ms"); }); // Once
134+
TimerSystem.PushLoop("KEY", 1000, () => { Debug.Log("2s"); }); // Loop
135+
// Main-thread
136+
TimerSystem.PushMain("KEY", 1, () => { Debug.Log("1ms"); }, 1); // Custom times default is 1
137+
TimerSystem.PushOnceMain("KEY", 2, () => { Debug.Log("2ms"); }); // Once
138+
TimerSystem.PushLoopMain("KEY", 1000, () => { Debug.Log("2s"); }); // Loop
138139
```
139140

140141
<h4>Remove timing task</h4>
@@ -165,4 +166,4 @@ TimerSystem.Pop(tid);
165166

166167
- **Thanks for using this software.**
167168
- **If this package is useful to you.**
168-
- **Please ⭐ this repository to support the project.**
169+
- **Please ⭐ this repository to support the project.**

.gitignore

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
!*.meta
21
*.log
32
*.DS_Store
43
*.sln
@@ -13,11 +12,6 @@
1312
.vscode/
1413
.consulo/
1514

16-
!*.dll.meta
17-
!*.asset.meta
18-
!*.asmdef.meta
19-
!*.cs.meta
20-
2115
[Bb]uild/
2216
[Bb]uilds/
2317
[Tt]emp/

Runtime/Container/ITimerContainer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,12 @@ internal interface ITimerContainer : IDisposable
7171
/// <summary>
7272
/// 推送更新
7373
/// </summary>
74+
/// <param name="timer">执行器</param>
7475
void PushUpdate(ITimerExecutor timer);
7576

7677
/// <summary>
7778
/// 推送更新
7879
/// </summary>
7980
void PushUpdate(List<ITimerExecutor> timer);
8081
}
81-
}
82+
}

Runtime/Container/TimerContainer.Loop.cs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,9 @@ public TimerContainerLoop(long unit)
1717
{
1818
Unit = unit;
1919

20-
for (byte i = 0; i < TimerSystem.TimingUnits.Count; i++)
21-
List.Add(new TimerOperatorLoop(
22-
i,
23-
TimerSystem.TimingUnits[i].Item2,
24-
TimerSystem.TimingUnits[i].Item3,
25-
DoneCallBack,
26-
PushUpdate,
27-
EvolutionCallBack
28-
));
20+
var tuples = TimerSystem.TimingUnits;
21+
for (byte i = 0; i < tuples.Count; i++)
22+
List.Add(new TimerOperatorLoop(i, tuples[i].Item2, tuples[i].Item3, DoneCallBack, PushUpdate, EvolutionCallBack));
2923
}
3024

3125
protected override void Update()
@@ -101,7 +95,7 @@ protected override void Update()
10195

10296
#if UNITY_EDITOR
10397
Debug.Log(
104-
$"[循环定时器:{ID}] [容器数量:{List.Count}] [状态:结束] 精度单位:{Unit} 当前时间:{Counter} 剩余任务数量:{RemainNum}");
98+
$"[循环定时器:{ID}] [容器数量:{List.Count}] [状态:结束] 精度单位:{Unit} 当前时间:{Counter} 剩余任务数量:{RemainNum}");
10599
#endif
106100
}
107101
#if UNITY_EDITOR
@@ -112,7 +106,7 @@ protected override void Update()
112106
{
113107
#if UNITY_EDITOR
114108
Debug.LogErrorFormat(
115-
$"[循环定时器:{ID}] [容器数量:{List.Count}] [状态:异常] 精度单位:{Unit} 当前时间:{Counter} 剩余任务数量:{RemainNum} 异常信息:{e}");
109+
$"[循环定时器:{ID}] [容器数量:{List.Count}] [状态:异常] 精度单位:{Unit} 当前时间:{Counter} 剩余任务数量:{RemainNum} 异常信息:{e}");
116110
#endif
117111
}
118112
finally
@@ -130,9 +124,6 @@ private void DoneCallBack(List<ITimerExecutor> list)
130124
});
131125
}
132126

133-
private void EvolutionCallBack(int Index, List<ITimerExecutor> list)
134-
{
135-
List[Index].AddTimerSource(list);
136-
}
127+
private void EvolutionCallBack(int Index, List<ITimerExecutor> list) { List[Index].AddTimerSource(list); }
137128
}
138-
}
129+
}

Runtime/Container/TimerContainer.cs

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,19 @@ namespace AIO
1313
{
1414
public abstract class TimerContainer : ITimerContainer
1515
{
16+
/// <summary>
17+
/// 容器ID
18+
/// </summary>
1619
private static int NUM;
1720

21+
/// <summary>
22+
/// 定时器任务
23+
/// </summary>
1824
private Task TaskHandle;
1925

26+
/// <summary>
27+
/// 构造函数
28+
/// </summary>
2029
protected TimerContainer()
2130
{
2231
Watch = Stopwatch.StartNew();
@@ -33,22 +42,31 @@ protected TimerContainer()
3342

3443
#region ITimerContainer Members
3544

45+
/// <inheritdoc />
3646
public ITimerOperator this[int index] => List[index];
3747

48+
/// <inheritdoc />
3849
public Stopwatch Watch { get; }
3950

51+
/// <inheritdoc />
4052
public List<ITimerOperator> List { get; }
4153

54+
/// <inheritdoc />
4255
public long Unit { get; protected set; }
4356

57+
/// <inheritdoc />
4458
public long Counter { get; protected set; }
4559

60+
/// <inheritdoc />
4661
public int RemainNum { get; protected set; }
4762

63+
/// <inheritdoc />
4864
public int ID { get; }
4965

66+
/// <inheritdoc />
5067
public long UpdateCacheTime { get; protected set; }
5168

69+
/// <inheritdoc />
5270
public void Start()
5371
{
5472
if (List.Count <= 0)
@@ -63,13 +81,15 @@ public void Start()
6381
TaskHandle = Task.Factory.StartNew(Update, TaskHandleToken);
6482
}
6583

84+
/// <inheritdoc />
6685
public void Cancel()
6786
{
6887
if (TaskHandle is null) return;
6988
if (!TaskHandle.IsCompleted) TaskHandleTokenSource?.Cancel(true);
7089
else TaskHandle.Dispose();
7190
}
7291

92+
/// <inheritdoc />
7393
public virtual void Dispose()
7494
{
7595
if (TaskHandleTokenSource != null)
@@ -94,15 +114,7 @@ public virtual void Dispose()
94114
}
95115
}
96116

97-
public override string ToString()
98-
{
99-
var builder = new StringBuilder();
100-
builder.AppendLine(
101-
$"[{GetType().Name} ID:{ID}] [容器数量:{List.Count}] 精度单位:{Unit} 当前时间:{Counter} 剩余任务数量:{RemainNum}");
102-
foreach (var item in List) builder.AppendLine(item.ToString()).AppendLine();
103-
return builder.ToString();
104-
}
105-
117+
/// <inheritdoc />
106118
public void PushUpdate(ITimerExecutor timer)
107119
{
108120
RemainNum += 1;
@@ -120,6 +132,7 @@ public void PushUpdate(ITimerExecutor timer)
120132
}
121133
}
122134

135+
/// <inheritdoc />
123136
public void PushUpdate(List<ITimerExecutor> timer)
124137
{
125138
if (timer.Count == 0) return;
@@ -140,16 +153,24 @@ public void PushUpdate(List<ITimerExecutor> timer)
140153

141154
timer.RemoveAt(i);
142155
}
143-
}
144156

145-
timer.Free();
157+
timer.Free();
158+
}
146159
}
147160

148161
#endregion
149162

163+
public override string ToString()
164+
{
165+
var builder = new StringBuilder();
166+
builder.AppendLine($"[{GetType().Name} ID:{ID}] [容器数量:{List.Count}] 精度单位:{Unit} 当前时间:{Counter} 剩余任务数量:{RemainNum}");
167+
foreach (var item in List) builder.AppendLine(item.ToString()).AppendLine();
168+
return builder.ToString();
169+
}
170+
150171
/// <summary>
151172
/// 更新
152173
/// </summary>
153174
protected abstract void Update();
154175
}
155-
}
176+
}

Runtime/Executor/ITimerExecutor.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@ public interface ITimerExecutor : IComparable<ITimerExecutor>, IDisposable
1515
/// <summary>
1616
/// 定时器索引
1717
/// </summary>
18+
19+
long TID
20+
{
21+
get;
1822
#if UNITY_2021_1_OR_NEWER
19-
long TID { get; protected set; }
20-
#else
21-
long TID { get; set; }
23+
protected
2224
#endif
25+
set;
26+
}
27+
2328
/// <summary>
2429
/// 创建时间 单位毫秒
2530
/// </summary>
@@ -57,7 +62,14 @@ public interface ITimerExecutor : IComparable<ITimerExecutor>, IDisposable
5762
/// <summary>
5863
/// 操作索引
5964
/// </summary>
60-
byte OperatorIndex { get; set; }
65+
byte OperatorIndex
66+
{
67+
get;
68+
#if UNITY_2021_1_OR_NEWER
69+
protected
70+
#endif
71+
set;
72+
}
6173

6274
/// <summary>
6375
/// 精度器 记录当前任务实际持续时间
@@ -94,4 +106,4 @@ internal interface ITimerExecutor<out T> : ITimerExecutor
94106
/// </summary>
95107
T Delegates { get; }
96108
}
97-
}
109+
}

Runtime/Executor/TimerExecutor.Enumerator.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,25 @@ Func<IEnumerator> delegateValue
4949
Delegates = delegateValue;
5050
}
5151

52+
/// <summary>
53+
/// 定时计算器
54+
/// </summary>
55+
/// <param name="tid">识别ID</param>
56+
/// <param name="duration">定时长度 单位为毫秒</param>
57+
/// <param name="loop">循环次数</param>
58+
/// <param name="createTime">创建时间</param>
59+
/// <param name="delegateValue">委托函数</param>
60+
internal TimerExecutorEnumerator(
61+
int tid,
62+
long duration,
63+
int loop,
64+
long createTime,
65+
Func<IEnumerator> delegateValue
66+
) : base(duration, loop, createTime, tid)
67+
{
68+
Delegates = delegateValue;
69+
}
70+
5271
protected override void xExecute()
5372
{
5473
try
@@ -61,4 +80,4 @@ protected override void xExecute()
6180
}
6281
}
6382
}
64-
}
83+
}

0 commit comments

Comments
 (0)