Mirror of Svelto.ECS because we're a fan of it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.5KB

  1. using System;
  2. using System.Collections.Generic;
  3. using WeakReferenceI = Svelto.DataStructures.WeakReference<Svelto.Context.IWaitForFrameworkInitialization>;
  4. using WeakReferenceD = Svelto.DataStructures.WeakReference<Svelto.Context.IWaitForFrameworkDestruction>;
  5. namespace Svelto.Context
  6. {
  7. public class ContextNotifier : IContextNotifer
  8. {
  9. public ContextNotifier()
  10. {
  11. _toInitialize = new List<WeakReferenceI>();
  12. _toDeinitialize = new List<WeakReferenceD>();
  13. }
  14. public void AddFrameworkDestructionListener(IWaitForFrameworkDestruction obj)
  15. {
  16. if (_toDeinitialize != null)
  17. _toDeinitialize.Add(new WeakReferenceD(obj));
  18. else
  19. throw new Exception("An object is expected to be initialized after the framework has been deinitialized. Type: " + obj.GetType());
  20. }
  21. public void AddFrameworkInitializationListener(IWaitForFrameworkInitialization obj)
  22. {
  23. if (_toInitialize != null)
  24. _toInitialize.Add(new WeakReferenceI(obj));
  25. else
  26. throw new Exception("An object is expected to be initialized after the framework has been initialized. Type: " + obj.GetType());
  27. }
  28. /// <summary>
  29. /// A Context is meant to be deinitialized only once in its timelife
  30. /// </summary>
  31. public void NotifyFrameworkDeinitialized()
  32. {
  33. for (var i = _toDeinitialize.Count - 1; i >= 0; --i)
  34. try
  35. {
  36. var obj = _toDeinitialize[i];
  37. if (obj.IsAlive)
  38. obj.Target.OnFrameworkDestroyed();
  39. }
  40. catch (Exception e)
  41. {
  42. Utility.Console.LogException(e);
  43. }
  44. _toDeinitialize = null;
  45. }
  46. /// <summary>
  47. /// A Context is meant to be initialized only once in its timelife
  48. /// </summary>
  49. public void NotifyFrameworkInitialized()
  50. {
  51. for (var i = _toInitialize.Count - 1; i >= 0; --i)
  52. try
  53. {
  54. var obj = _toInitialize[i];
  55. if (obj.IsAlive)
  56. obj.Target.OnFrameworkInitialized();
  57. }
  58. catch (Exception e)
  59. {
  60. Utility.Console.LogException(e);
  61. }
  62. _toInitialize = null;
  63. }
  64. List<WeakReferenceD> _toDeinitialize;
  65. List<WeakReferenceI> _toInitialize;
  66. }
  67. }