using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.Profiling; namespace Coffee.UIParticleInternal { /// /// Base class for a fast action. /// internal class FastActionBase { private static readonly ObjectPool> s_NodePool = new ObjectPool>(() => new LinkedListNode(default), _ => true, x => x.Value = default); private readonly LinkedList _delegates = new LinkedList(); /// /// Adds a delegate to the action. /// public void Add(T rhs) { if (rhs == null) return; Profiler.BeginSample("(COF)[FastAction] Add Action"); var node = s_NodePool.Rent(); node.Value = rhs; _delegates.AddLast(node); Profiler.EndSample(); } /// /// Removes a delegate from the action. /// public void Remove(T rhs) { if (rhs == null) return; Profiler.BeginSample("(COF)[FastAction] Remove Action"); var node = _delegates.Find(rhs); if (node != null) { _delegates.Remove(node); s_NodePool.Return(ref node); } Profiler.EndSample(); } /// /// Invokes the action with a callback function. /// protected void Invoke(Action callback) { var node = _delegates.First; while (node != null) { try { callback(node.Value); } catch (Exception e) { Debug.LogException(e); } node = node.Next; } } public void Clear() { _delegates.Clear(); } } /// /// A fast action without parameters. /// internal class FastAction : FastActionBase { /// /// Invoke all the registered delegates. /// public void Invoke() { Invoke(action => action.Invoke()); } } }