ChainEventSubscription.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Blue {
  4. public class ChainEventSubscription : IChainEventSubscription
  5. {
  6. private Dictionary<Type, int> actionHashPool;
  7. private Type mChainEventType;
  8. public ChainEventSubscription(Type chainEventType)
  9. {
  10. mChainEventType = chainEventType;
  11. }
  12. public int SubscribeCount => actionHashPool.Count;
  13. public IChainEventUnSubscribe Subscribe(Type eventType,int actionHashCode,Action onOnSubscribe)
  14. {
  15. actionHashPool.TryAdd(eventType, actionHashCode);
  16. return new DefaultChainEventUnSubcribe(mChainEventType, () => { onOnSubscribe?.Invoke(); UnSubscribe(eventType); });
  17. }
  18. public bool IsSubscribed(Type eventType)
  19. {
  20. return actionHashPool.ContainsKey(eventType);
  21. }
  22. public int GetActionHashCode(Type eventType)
  23. {
  24. return actionHashPool[eventType];
  25. }
  26. public List<Type> GetEventTypeList()
  27. {
  28. List<Type> valueList = new List<Type>(actionHashPool.Count);
  29. valueList.AddRange(actionHashPool.Keys);
  30. return valueList;
  31. }
  32. public bool UnSubscribe(Type eventType)
  33. {
  34. return actionHashPool.Remove(eventType);
  35. }
  36. }
  37. }