This commit is contained in:
cneicy committed 2026-03-08 00:40:13 +08:00
1 parent 3dfbccc9d6
commit 8549ba5c80
38 files changed
+2187

No files matched your search

+41
View File
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
namespace ShrinkEventBus
{
public static class EventPool<T> where T : EventBase, new()
{
private static readonly Stack<T> Pool = new(32);
private static readonly object Lock = new();
public static T Get()
{
lock (Lock)
{
if (Pool.Count > 0)
{
var evt = Pool.Pop();
evt.IsInPool = false;
return evt;
}
}
var newEvt = new T();
newEvt.ReleaseAction = e => Release((T)e);
return newEvt;
}
public static void Release(T evt)
{
if (evt == null || evt.IsInPool) return;
evt.ResetInternal();
evt.IsInPool = true;
lock (Lock)
{
Pool.Push(evt);
}
}
}
}