using System; using System.Collections.Generic; using Svelto.DataStructures; namespace Svelto.Context { class ContextNotifier : IContextNotifer { public ContextNotifier() { _toInitialize = new List>(); _toDeinitialize = new List>(); } public void AddFrameworkDestructionListener(IWaitForFrameworkDestruction obj) { if (_toDeinitialize != null) _toDeinitialize.Add(new WeakReference(obj)); else throw new Exception("An object is expected to be initialized after the framework has been deinitialized. Type: " + obj.GetType()); } public void AddFrameworkInitializationListener(IWaitForFrameworkInitialization obj) { if (_toInitialize != null) _toInitialize.Add(new WeakReference(obj)); else throw new Exception("An object is expected to be initialized after the framework has been initialized. Type: " + obj.GetType()); } /// /// A Context is meant to be deinitialized only once in its timelife /// public void NotifyFrameworkDeinitialized() { for (var i = _toDeinitialize.Count - 1; i >= 0; --i) try { var obj = _toDeinitialize[i]; if (obj.IsAlive) obj.Target.OnFrameworkDestroyed(); } catch (Exception e) { Utility.Console.LogException(e); } _toDeinitialize = null; } /// /// A Context is meant to be initialized only once in its timelife /// public void NotifyFrameworkInitialized() { for (var i = _toInitialize.Count - 1; i >= 0; --i) try { var obj = _toInitialize[i]; if (obj.IsAlive) obj.Target.OnFrameworkInitialized(); } catch (Exception e) { Utility.Console.LogException(e); } _toInitialize = null; } List> _toDeinitialize; List> _toInitialize; } }