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.

ContextNotifier.cs 2.5KB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.Collections.Generic;
  3. using Svelto.DataStructures;
  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 (var i = _toDeinitialize.Count - 1; i >= 0; --i)
  33. try
  34. {
  35. var obj = _toDeinitialize[i];
  36. if (obj.IsAlive)
  37. obj.Target.OnFrameworkDestroyed();
  38. }
  39. catch (Exception e)
  40. {
  41. Utility.Console.LogException(e);
  42. }
  43. _toDeinitialize = null;
  44. }
  45. /// <summary>
  46. /// A Context is meant to be initialized only once in its timelife
  47. /// </summary>
  48. public void NotifyFrameworkInitialized()
  49. {
  50. for (var i = _toInitialize.Count - 1; i >= 0; --i)
  51. try
  52. {
  53. var obj = _toInitialize[i];
  54. if (obj.IsAlive)
  55. obj.Target.OnFrameworkInitialized();
  56. }
  57. catch (Exception e)
  58. {
  59. Utility.Console.LogException(e);
  60. }
  61. _toInitialize = null;
  62. }
  63. List<WeakReference<IWaitForFrameworkDestruction>> _toDeinitialize;
  64. List<WeakReference<IWaitForFrameworkInitialization>> _toInitialize;
  65. }
  66. }