Skip to content

Commit

Permalink
✨ 支持主线程执行
Browse files Browse the repository at this point in the history
  • Loading branch information
xinansky committed Jun 25, 2024
1 parent 71ea2c1 commit 22caf7f
Show file tree
Hide file tree
Showing 35 changed files with 1,253 additions and 249 deletions.
24 changes: 12 additions & 12 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
"name": "package.openupm.com",
"url": "https://package.openupm.com",
"scopes": [
"com.aio.timer",
"com.aio.runner"
"com.aio.runner",
"com.aio.timer"
]
}
]
Expand Down Expand Up @@ -94,6 +94,7 @@ openupm add com.aio.timer
-**支持 自定义时间轮大小**
-**支持 时间轮动态扩容**
-**支持 同时添加1000000+ 定时任务**
-**支持 `int` `string` `enum` `Guid` 作为 循环任务KEY**

## 📚 使用

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

```csharp
TimerSystem.Push(1, () => { Debug.Log("1ms"); });
TimerSystem.Push(2, () => { Debug.Log("2ms"); });
TimerSystem.Push(1000, () => { Debug.Log("2s"); });
```

<h4>添加循环定时任务</h4>

```csharp
TimerSystem.PushLoop(tid, 3, () => { Debug.Log("3ms"); });
// 后台线程
TimerSystem.Push("KEY", 1, () => { Debug.Log("1ms"); }); // 自定义次数 默认为1
TimerSystem.PushOnce("KEY", 2, () => { Debug.Log("2ms"); }); // 一次
TimerSystem.PushLoop("KEY", 1000, () => { Debug.Log("2s"); }); // 循环
// 主线程
TimerSystem.PushMain("KEY", 1, () => { Debug.Log("1ms"); }, 1); // 自定义次数 默认为1
TimerSystem.PushOnceMain("KEY", 2, () => { Debug.Log("2ms"); }); // 一次
TimerSystem.PushLoopMain("KEY", 1000, () => { Debug.Log("2s"); }); // 循环
```

<h4>移除循环定时任务</h4>
Expand Down Expand Up @@ -166,4 +166,4 @@ TimerSystem.Pop(tid);

- **谢谢您选择我们的扩展包。**
- **如果此软件包对您有所帮助。**
- **请考虑通过添加⭐来表示支持。**
- **请考虑通过添加⭐来表示支持。**
23 changes: 12 additions & 11 deletions .github/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
"name": "package.openupm.com",
"url": "https://package.openupm.com",
"scopes": [
"com.aio.timer",
"com.aio.runner"
"com.aio.runner",
"com.aio.timer"
]
}
]
Expand Down Expand Up @@ -94,6 +94,7 @@ openupm add com.aio.timer
- **Support custom time wheel size.**
- **Support time wheel dynamic expansion.**
- **Support adding 1000000+ timing tasks at the same time.**
- **Support int` `string` `enum` `Guid` as loop task KEY**

## 📚 Usage

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

```csharp
TimerSystem.Push(1, () => { Debug.Log("1s"); });
TimerSystem.Push(2, () => { Debug.Log("2s"); });
```

<h4>Remove timing task</h4>

```csharp
TimerSystem.PushLoop(tid, 3, () => { Debug.Log("3s"); });
// Sub-thread
TimerSystem.Push("KEY", 1, () => { Debug.Log("1ms"); }); // Custom times default is 1
TimerSystem.PushOnce("KEY", 2, () => { Debug.Log("2ms"); }); // Once
TimerSystem.PushLoop("KEY", 1000, () => { Debug.Log("2s"); }); // Loop
// Main-thread
TimerSystem.PushMain("KEY", 1, () => { Debug.Log("1ms"); }, 1); // Custom times default is 1
TimerSystem.PushOnceMain("KEY", 2, () => { Debug.Log("2ms"); }); // Once
TimerSystem.PushLoopMain("KEY", 1000, () => { Debug.Log("2s"); }); // Loop
```

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

- **Thanks for using this software.**
- **If this package is useful to you.**
- **Please ⭐ this repository to support the project.**
- **Please ⭐ this repository to support the project.**
6 changes: 0 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
!*.meta
*.log
*.DS_Store
*.sln
Expand All @@ -13,11 +12,6 @@
.vscode/
.consulo/

!*.dll.meta
!*.asset.meta
!*.asmdef.meta
!*.cs.meta

[Bb]uild/
[Bb]uilds/
[Tt]emp/
Expand Down
3 changes: 2 additions & 1 deletion Runtime/Container/ITimerContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ internal interface ITimerContainer : IDisposable
/// <summary>
/// 推送更新
/// </summary>
/// <param name="timer">执行器</param>
void PushUpdate(ITimerExecutor timer);

/// <summary>
/// 推送更新
/// </summary>
void PushUpdate(List<ITimerExecutor> timer);
}
}
}
23 changes: 7 additions & 16 deletions Runtime/Container/TimerContainer.Loop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,9 @@ public TimerContainerLoop(long unit)
{
Unit = unit;

for (byte i = 0; i < TimerSystem.TimingUnits.Count; i++)
List.Add(new TimerOperatorLoop(
i,
TimerSystem.TimingUnits[i].Item2,
TimerSystem.TimingUnits[i].Item3,
DoneCallBack,
PushUpdate,
EvolutionCallBack
));
var tuples = TimerSystem.TimingUnits;
for (byte i = 0; i < tuples.Count; i++)
List.Add(new TimerOperatorLoop(i, tuples[i].Item2, tuples[i].Item3, DoneCallBack, PushUpdate, EvolutionCallBack));
}

protected override void Update()
Expand Down Expand Up @@ -101,7 +95,7 @@ protected override void Update()

#if UNITY_EDITOR
Debug.Log(
$"[循环定时器:{ID}] [容器数量:{List.Count}] [状态:结束] 精度单位:{Unit} 当前时间:{Counter} 剩余任务数量:{RemainNum}");
$"[循环定时器:{ID}] [容器数量:{List.Count}] [状态:结束] 精度单位:{Unit} 当前时间:{Counter} 剩余任务数量:{RemainNum}");
#endif
}
#if UNITY_EDITOR
Expand All @@ -112,7 +106,7 @@ protected override void Update()
{
#if UNITY_EDITOR
Debug.LogErrorFormat(
$"[循环定时器:{ID}] [容器数量:{List.Count}] [状态:异常] 精度单位:{Unit} 当前时间:{Counter} 剩余任务数量:{RemainNum} 异常信息:{e}");
$"[循环定时器:{ID}] [容器数量:{List.Count}] [状态:异常] 精度单位:{Unit} 当前时间:{Counter} 剩余任务数量:{RemainNum} 异常信息:{e}");
#endif
}
finally
Expand All @@ -130,9 +124,6 @@ private void DoneCallBack(List<ITimerExecutor> list)
});
}

private void EvolutionCallBack(int Index, List<ITimerExecutor> list)
{
List[Index].AddTimerSource(list);
}
private void EvolutionCallBack(int Index, List<ITimerExecutor> list) { List[Index].AddTimerSource(list); }
}
}
}
45 changes: 33 additions & 12 deletions Runtime/Container/TimerContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,19 @@ namespace AIO
{
public abstract class TimerContainer : ITimerContainer
{
/// <summary>
/// 容器ID
/// </summary>
private static int NUM;

/// <summary>
/// 定时器任务
/// </summary>
private Task TaskHandle;

/// <summary>
/// 构造函数
/// </summary>
protected TimerContainer()
{
Watch = Stopwatch.StartNew();
Expand All @@ -33,22 +42,31 @@ protected TimerContainer()

#region ITimerContainer Members

/// <inheritdoc />
public ITimerOperator this[int index] => List[index];

/// <inheritdoc />
public Stopwatch Watch { get; }

/// <inheritdoc />
public List<ITimerOperator> List { get; }

/// <inheritdoc />
public long Unit { get; protected set; }

/// <inheritdoc />
public long Counter { get; protected set; }

/// <inheritdoc />
public int RemainNum { get; protected set; }

/// <inheritdoc />
public int ID { get; }

/// <inheritdoc />
public long UpdateCacheTime { get; protected set; }

/// <inheritdoc />
public void Start()
{
if (List.Count <= 0)
Expand All @@ -63,13 +81,15 @@ public void Start()
TaskHandle = Task.Factory.StartNew(Update, TaskHandleToken);
}

/// <inheritdoc />
public void Cancel()
{
if (TaskHandle is null) return;
if (!TaskHandle.IsCompleted) TaskHandleTokenSource?.Cancel(true);
else TaskHandle.Dispose();
}

/// <inheritdoc />
public virtual void Dispose()
{
if (TaskHandleTokenSource != null)
Expand All @@ -94,15 +114,7 @@ public virtual void Dispose()
}
}

public override string ToString()
{
var builder = new StringBuilder();
builder.AppendLine(
$"[{GetType().Name} ID:{ID}] [容器数量:{List.Count}] 精度单位:{Unit} 当前时间:{Counter} 剩余任务数量:{RemainNum}");
foreach (var item in List) builder.AppendLine(item.ToString()).AppendLine();
return builder.ToString();
}

/// <inheritdoc />
public void PushUpdate(ITimerExecutor timer)
{
RemainNum += 1;
Expand All @@ -120,6 +132,7 @@ public void PushUpdate(ITimerExecutor timer)
}
}

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

timer.RemoveAt(i);
}
}

timer.Free();
timer.Free();
}
}

#endregion

public override string ToString()
{
var builder = new StringBuilder();
builder.AppendLine($"[{GetType().Name} ID:{ID}] [容器数量:{List.Count}] 精度单位:{Unit} 当前时间:{Counter} 剩余任务数量:{RemainNum}");
foreach (var item in List) builder.AppendLine(item.ToString()).AppendLine();
return builder.ToString();
}

/// <summary>
/// 更新
/// </summary>
protected abstract void Update();
}
}
}
22 changes: 17 additions & 5 deletions Runtime/Executor/ITimerExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ public interface ITimerExecutor : IComparable<ITimerExecutor>, IDisposable
/// <summary>
/// 定时器索引
/// </summary>

long TID
{
get;
#if UNITY_2021_1_OR_NEWER
long TID { get; protected set; }
#else
long TID { get; set; }
protected
#endif
set;
}

/// <summary>
/// 创建时间 单位毫秒
/// </summary>
Expand Down Expand Up @@ -57,7 +62,14 @@ public interface ITimerExecutor : IComparable<ITimerExecutor>, IDisposable
/// <summary>
/// 操作索引
/// </summary>
byte OperatorIndex { get; set; }
byte OperatorIndex
{
get;
#if UNITY_2021_1_OR_NEWER
protected
#endif
set;
}

/// <summary>
/// 精度器 记录当前任务实际持续时间
Expand Down Expand Up @@ -94,4 +106,4 @@ internal interface ITimerExecutor<out T> : ITimerExecutor
/// </summary>
T Delegates { get; }
}
}
}
21 changes: 20 additions & 1 deletion Runtime/Executor/TimerExecutor.Enumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,25 @@ Func<IEnumerator> delegateValue
Delegates = delegateValue;
}

/// <summary>
/// 定时计算器
/// </summary>
/// <param name="tid">识别ID</param>
/// <param name="duration">定时长度 单位为毫秒</param>
/// <param name="loop">循环次数</param>
/// <param name="createTime">创建时间</param>
/// <param name="delegateValue">委托函数</param>
internal TimerExecutorEnumerator(
int tid,
long duration,
int loop,
long createTime,
Func<IEnumerator> delegateValue
) : base(duration, loop, createTime, tid)
{
Delegates = delegateValue;
}

protected override void xExecute()
{
try
Expand All @@ -61,4 +80,4 @@ protected override void xExecute()
}
}
}
}
}
Loading

0 comments on commit 22caf7f

Please sign in to comment.