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.

79 lines
2.7KB

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