// Copyright (c) 2015 - 2023 Doozy Entertainment. All Rights Reserved. // This code can only be used under the standard Unity Asset Store End User License Agreement // A Copy of the EULA APPENDIX 1 is available at http://unity3d.com/company/legal/as_terms using System; using System.Collections.Generic; using Doozy.Runtime.Common.Attributes; namespace Doozy.Runtime.Signals { internal static class SignalPool { private static HashSet pool { get; set; } [ClearOnReload(resetValue: false)] private static bool initialized { get; set; } [ExecuteOnReload] private static void Initialize() { if (initialized) return; pool = new HashSet(); initialized = true; } /// Get a signal from the given signal type, either from the pool or a new one /// Signal Type public static T Get() where T : Signal, new() { Initialize(); pool.Remove(null); T signal = null; foreach (Signal pooledItem in pool) { if (!(pooledItem is T castedItem)) continue; signal = castedItem; pool.Remove(castedItem); break; } signal ??= Activator.CreateInstance(); return signal; } /// Return the given reaction to the pool public static void AddToPool(this T signal) where T : Signal { Initialize(); signal.Reset(); pool.Add(signal); } } }