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.

49 lines
1.2KB

  1. namespace Svelto.ECS
  2. {
  3. public class DispatcherOnSet<T>
  4. {
  5. public DispatcherOnSet(int senderID)
  6. {
  7. _senderID = senderID;
  8. _subscribers = new System.Collections.Generic.HashSet<WeakAction<int, T>>();
  9. }
  10. public T value
  11. {
  12. set
  13. {
  14. _value = value;
  15. if (_subscribers != null)
  16. {
  17. using (var enumerator = _subscribers.GetEnumerator())
  18. {
  19. while (enumerator.MoveNext() == true)
  20. enumerator.Current.Invoke(_senderID, _value);
  21. }
  22. }
  23. }
  24. get
  25. {
  26. return _value;
  27. }
  28. }
  29. public void NotifyOnDataChange(System.Action<int, T> action)
  30. {
  31. _subscribers.Add(new WeakAction<int, T>(action));
  32. }
  33. public void StopNotifyOnDataChange(System.Action<int, T> action)
  34. {
  35. _subscribers.Remove(new WeakAction<int, T>(action));
  36. }
  37. protected T _value;
  38. protected int _senderID;
  39. protected System.Collections.Generic.HashSet<WeakAction<int, T>> _subscribers;
  40. }
  41. }