CollectionChangedEventManager.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.Linq;
  5. using System.Runtime.CompilerServices;
  6. using Avalonia.Threading;
  7. using Avalonia.Utilities;
  8. namespace Avalonia.Controls.Utils
  9. {
  10. internal interface ICollectionChangedListener
  11. {
  12. void PreChanged(INotifyCollectionChanged sender, NotifyCollectionChangedEventArgs e);
  13. void Changed(INotifyCollectionChanged sender, NotifyCollectionChangedEventArgs e);
  14. void PostChanged(INotifyCollectionChanged sender, NotifyCollectionChangedEventArgs e);
  15. }
  16. internal class CollectionChangedEventManager
  17. {
  18. public static CollectionChangedEventManager Instance { get; } = new CollectionChangedEventManager();
  19. private ConditionalWeakTable<INotifyCollectionChanged, Entry> _entries =
  20. new ConditionalWeakTable<INotifyCollectionChanged, Entry>();
  21. private CollectionChangedEventManager()
  22. {
  23. }
  24. public void AddListener(INotifyCollectionChanged collection, ICollectionChangedListener listener)
  25. {
  26. collection = collection ?? throw new ArgumentNullException(nameof(collection));
  27. listener = listener ?? throw new ArgumentNullException(nameof(listener));
  28. Dispatcher.UIThread.VerifyAccess();
  29. if (!_entries.TryGetValue(collection, out var entry))
  30. {
  31. entry = new Entry(collection);
  32. _entries.Add(collection, entry);
  33. }
  34. foreach (var l in entry.Listeners)
  35. {
  36. if (l.TryGetTarget(out var target) && target == listener)
  37. {
  38. throw new InvalidOperationException(
  39. "Collection listener already added for this collection/listener combination.");
  40. }
  41. }
  42. entry.Listeners.Add(new WeakReference<ICollectionChangedListener>(listener));
  43. }
  44. public void RemoveListener(INotifyCollectionChanged collection, ICollectionChangedListener listener)
  45. {
  46. collection = collection ?? throw new ArgumentNullException(nameof(collection));
  47. listener = listener ?? throw new ArgumentNullException(nameof(listener));
  48. Dispatcher.UIThread.VerifyAccess();
  49. if (_entries.TryGetValue(collection, out var entry))
  50. {
  51. var listeners = entry.Listeners;
  52. for (var i = 0; i < listeners.Count; ++i)
  53. {
  54. if (listeners[i].TryGetTarget(out var target) && target == listener)
  55. {
  56. listeners.RemoveAt(i);
  57. if (listeners.Count == 0)
  58. {
  59. entry.Dispose();
  60. _entries.Remove(collection);
  61. }
  62. return;
  63. }
  64. }
  65. }
  66. throw new InvalidOperationException(
  67. "Collection listener not registered for this collection/listener combination.");
  68. }
  69. private class Entry : IWeakEventSubscriber<NotifyCollectionChangedEventArgs>, IDisposable
  70. {
  71. private INotifyCollectionChanged _collection;
  72. public Entry(INotifyCollectionChanged collection)
  73. {
  74. _collection = collection;
  75. Listeners = new List<WeakReference<ICollectionChangedListener>>();
  76. WeakEvents.CollectionChanged.Subscribe(_collection, this);
  77. }
  78. public List<WeakReference<ICollectionChangedListener>> Listeners { get; }
  79. public void Dispose()
  80. {
  81. WeakEvents.CollectionChanged.Unsubscribe(_collection, this);
  82. }
  83. void IWeakEventSubscriber<NotifyCollectionChangedEventArgs>.
  84. OnEvent(object? notifyCollectionChanged, WeakEvent ev, NotifyCollectionChangedEventArgs e)
  85. {
  86. static void Notify(
  87. INotifyCollectionChanged incc,
  88. NotifyCollectionChangedEventArgs args,
  89. WeakReference<ICollectionChangedListener>[] listeners)
  90. {
  91. foreach (var l in listeners)
  92. {
  93. if (l.TryGetTarget(out var target))
  94. {
  95. target.PreChanged(incc, args);
  96. }
  97. }
  98. foreach (var l in listeners)
  99. {
  100. if (l.TryGetTarget(out var target))
  101. {
  102. target.Changed(incc, args);
  103. }
  104. }
  105. foreach (var l in listeners)
  106. {
  107. if (l.TryGetTarget(out var target))
  108. {
  109. target.PostChanged(incc, args);
  110. }
  111. }
  112. }
  113. var l = Listeners.ToArray();
  114. if (Dispatcher.UIThread.CheckAccess())
  115. {
  116. Notify(_collection, e, l);
  117. }
  118. else
  119. {
  120. var eCapture = e;
  121. Dispatcher.UIThread.Post(() => Notify(_collection, eCapture, l), DispatcherPriority.Send);
  122. }
  123. }
  124. }
  125. }
  126. }