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.

48 lines
1.1KB

  1. using System;
  2. namespace Svelto.ECS
  3. {
  4. public class DispatchOnSet<T>
  5. {
  6. public DispatchOnSet(EGID senderID)
  7. {
  8. _senderID = senderID;
  9. }
  10. public T value
  11. {
  12. set
  13. {
  14. _value = value;
  15. if (_paused == false)
  16. _subscriber(_senderID, value);
  17. }
  18. }
  19. public void NotifyOnValueSet(Action<EGID, T> action)
  20. {
  21. #if DEBUG && !PROFILE_SVELTO
  22. DBC.ECS.Check.Require(_subscriber == null, $"{this.GetType().Name}: listener already registered");
  23. #endif
  24. _subscriber = action;
  25. _paused = false;
  26. }
  27. public void StopNotify()
  28. {
  29. _subscriber = null;
  30. _paused = true;
  31. }
  32. public void PauseNotify() { _paused = true; }
  33. public void ResumeNotify() { _paused = false; }
  34. protected T _value;
  35. readonly EGID _senderID;
  36. Action<EGID, T> _subscriber;
  37. bool _paused;
  38. }
  39. }