CoreObservableCollection.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. // using System;
  2. // using System.Collections;
  3. // using System.Collections.ObjectModel;
  4. //
  5. // namespace InABox.Avalonia
  6. // {
  7. // // Licensed to the .NET Foundation under one or more agreements.
  8. // // The .NET Foundation licenses this file to you under the MIT license.
  9. // // See the LICENSE file in the project root for more information.
  10. //
  11. // using System.Collections.Generic;
  12. // using System.Collections.Specialized;
  13. // using System.ComponentModel;
  14. // using System.Diagnostics;
  15. // using System.Linq;
  16. //
  17. // /// <summary>
  18. // /// Implementation of a dynamic data collection based on generic Collection&lt;T&gt;,
  19. // /// implementing INotifyCollectionChanged to notify listeners
  20. // /// when items get added, removed or the whole list is refreshed.
  21. // ///
  22. // /// Modified to Implement Cross-thread Synchronization of events
  23. // /// Will trigger event callbacks on creating, rather than current thread.
  24. // /// Collections should be created on Msin UI thread the to prevent exceptions
  25. // /// </summary>
  26. // public class CoreObservableCollection<T> : ObservableCollection<T>
  27. // {
  28. // //------------------------------------------------------
  29. // //
  30. // // Private Fields
  31. // //
  32. // //------------------------------------------------------
  33. //
  34. // #region Private Fields
  35. //
  36. // [NonSerialized] private DeferredEventsCollection? _deferredEvents;
  37. //
  38. // [NonSerialized] private SynchronizationContext? _synchronizationContext = SynchronizationContext.Current;
  39. //
  40. // #endregion Private Fields
  41. //
  42. //
  43. // //------------------------------------------------------
  44. // //
  45. // // Constructors
  46. // //
  47. // //------------------------------------------------------
  48. //
  49. // #region Constructors
  50. //
  51. // /// <summary>
  52. // /// Initializes a new instance of ObservableCollection that is empty and has default initial capacity.
  53. // /// </summary>
  54. // public CoreObservableCollection()
  55. // {
  56. // }
  57. //
  58. // /// <summary>
  59. // /// Initializes a new instance of the ObservableCollection class that contains
  60. // /// elements copied from the specified collection and has sufficient capacity
  61. // /// to accommodate the number of elements copied.
  62. // /// </summary>
  63. // /// <param name="collection">The collection whose elements are copied to the new list.</param>
  64. // /// <remarks>
  65. // /// The elements are copied onto the ObservableCollection in the
  66. // /// same order they are read by the enumerator of the collection.
  67. // /// </remarks>
  68. // /// <exception cref="ArgumentNullException"> collection is a null reference </exception>
  69. // public CoreObservableCollection(IEnumerable<T> collection) : base(collection)
  70. // {
  71. // }
  72. //
  73. // /// <summary>
  74. // /// Initializes a new instance of the ObservableCollection class
  75. // /// that contains elements copied from the specified list
  76. // /// </summary>
  77. // /// <param name="list">The list whose elements are copied to the new list.</param>
  78. // /// <remarks>
  79. // /// The elements are copied onto the ObservableCollection in the
  80. // /// same order they are read by the enumerator of the list.
  81. // /// </remarks>
  82. // /// <exception cref="ArgumentNullException"> list is a null reference </exception>
  83. // public CoreObservableCollection(List<T> list) : base(list)
  84. // {
  85. // }
  86. //
  87. // #endregion Constructors
  88. //
  89. // //------------------------------------------------------
  90. // //
  91. // // Public Properties
  92. // //
  93. // //------------------------------------------------------
  94. //
  95. // #region Public Properties
  96. //
  97. // EqualityComparer<T>? _Comparer;
  98. //
  99. // public EqualityComparer<T> Comparer
  100. // {
  101. // get => _Comparer ??= EqualityComparer<T>.Default;
  102. // private set => _Comparer = value;
  103. // }
  104. //
  105. // /// <summary>
  106. // /// Gets or sets a value indicating whether this collection acts as a <see cref="HashSet{T}"/>,
  107. // /// disallowing duplicate items, based on <see cref="Comparer"/>.
  108. // /// This might indeed consume background performance, but in the other hand,
  109. // /// it will pay off in UI performance as less required UI updates are required.
  110. // /// </summary>
  111. // public bool AllowDuplicates { get; set; } = true;
  112. //
  113. // // If the collection is created on a non-UI thread, but needs to update the UI,
  114. // // we should be able to set the UI context here to prevent exceptions
  115. // public SynchronizationContext? SynchronizationContext
  116. // {
  117. // get => _synchronizationContext;
  118. // set => _synchronizationContext = value;
  119. // }
  120. //
  121. // #endregion Public Properties
  122. //
  123. // //------------------------------------------------------
  124. // //
  125. // // Public Methods
  126. // //
  127. // //------------------------------------------------------
  128. //
  129. // #region Public Methods
  130. //
  131. // /// <summary>
  132. // /// Adds the elements of the specified collection to the end of the <see cref="ObservableCollection{T}"/>.
  133. // /// </summary>
  134. // /// <param name="collection">
  135. // /// The collection whose elements should be added to the end of the <see cref="ObservableCollection{T}"/>.
  136. // /// The collection itself cannot be null, but it can contain elements that are null, if type T is a reference type.
  137. // /// </param>
  138. // /// <exception cref="ArgumentNullException"><paramref name="collection"/> is null.</exception>
  139. // public void AddRange(IEnumerable<T> collection)
  140. // {
  141. // InsertRange(Count, collection);
  142. // }
  143. //
  144. // /// <summary>
  145. // /// Inserts the elements of a collection into the <see cref="ObservableCollection{T}"/> at the specified index.
  146. // /// </summary>
  147. // /// <param name="index">The zero-based index at which the new elements should be inserted.</param>
  148. // /// <param name="collection">The collection whose elements should be inserted into the List<T>.
  149. // /// The collection itself cannot be null, but it can contain elements that are null, if type T is a reference type.</param>
  150. // /// <exception cref="ArgumentNullException"><paramref name="collection"/> is null.</exception>
  151. // /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is not in the collection range.</exception>
  152. // public void InsertRange(int index, IEnumerable<T> collection)
  153. // {
  154. // if (collection == null)
  155. // throw new ArgumentNullException(nameof(collection));
  156. // if (index < 0)
  157. // throw new ArgumentOutOfRangeException(nameof(index));
  158. // if (index > Count)
  159. // throw new ArgumentOutOfRangeException(nameof(index));
  160. //
  161. // if (!AllowDuplicates)
  162. // collection =
  163. // collection
  164. // .Distinct(Comparer)
  165. // .Where(item => !Items.Contains(item, Comparer))
  166. // .ToList();
  167. //
  168. // if (collection is ICollection<T> countable)
  169. // {
  170. // if (countable.Count == 0)
  171. // return;
  172. // }
  173. // else if (!collection.Any())
  174. // return;
  175. //
  176. // CheckReentrancy();
  177. //
  178. // //expand the following couple of lines when adding more constructors.
  179. // var target = (List<T>)Items;
  180. // target.InsertRange(index, collection);
  181. //
  182. // OnEssentialPropertiesChanged();
  183. //
  184. // if (!(collection is IList list))
  185. // list = new List<T>(collection);
  186. //
  187. // OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, list, index));
  188. // }
  189. //
  190. //
  191. // /// <summary>
  192. // /// Removes the first occurence of each item in the specified collection from the <see cref="ObservableCollection{T}"/>.
  193. // /// </summary>
  194. // /// <param name="collection">The items to remove.</param>
  195. // /// <exception cref="ArgumentNullException"><paramref name="collection"/> is null.</exception>
  196. // public void RemoveRange(IEnumerable<T> collection)
  197. // {
  198. // if (collection == null)
  199. // throw new ArgumentNullException(nameof(collection));
  200. //
  201. // if (Count == 0)
  202. // return;
  203. // else if (collection is ICollection<T> countable)
  204. // {
  205. // if (countable.Count == 0)
  206. // return;
  207. // else if (countable.Count == 1)
  208. // using (IEnumerator<T> enumerator = countable.GetEnumerator())
  209. // {
  210. // enumerator.MoveNext();
  211. // Remove(enumerator.Current);
  212. // return;
  213. // }
  214. // }
  215. // else if (!collection.Any())
  216. // return;
  217. //
  218. // CheckReentrancy();
  219. //
  220. // var clusters = new Dictionary<int, List<T>>();
  221. // var lastIndex = -1;
  222. // List<T>? lastCluster = null;
  223. // foreach (T item in collection)
  224. // {
  225. // var index = IndexOf(item);
  226. // if (index < 0)
  227. // continue;
  228. //
  229. // Items.RemoveAt(index);
  230. //
  231. // if (lastIndex == index && lastCluster != null)
  232. // lastCluster.Add(item);
  233. // else
  234. // clusters[lastIndex = index] = lastCluster = new List<T> { item };
  235. // }
  236. //
  237. // OnEssentialPropertiesChanged();
  238. //
  239. // if (Count == 0)
  240. // OnCollectionReset();
  241. // else
  242. // foreach (KeyValuePair<int, List<T>> cluster in clusters)
  243. // OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove,
  244. // cluster.Value, cluster.Key));
  245. // }
  246. //
  247. // /// <summary>
  248. // /// Iterates over the collection and removes all items that satisfy the specified match.
  249. // /// </summary>
  250. // /// <remarks>The complexity is O(n).</remarks>
  251. // /// <param name="match"></param>
  252. // /// <returns>Returns the number of elements that where </returns>
  253. // /// <exception cref="ArgumentNullException"><paramref name="match"/> is null.</exception>
  254. // public int RemoveAll(Predicate<T> match)
  255. // {
  256. // return RemoveAll(0, Count, match);
  257. // }
  258. //
  259. // /// <summary>
  260. // /// Iterates over the specified range within the collection and removes all items that satisfy the specified match.
  261. // /// </summary>
  262. // /// <remarks>The complexity is O(n).</remarks>
  263. // /// <param name="index">The index of where to start performing the search.</param>
  264. // /// <param name="count">The number of items to iterate on.</param>
  265. // /// <param name="match"></param>
  266. // /// <returns>Returns the number of elements that where </returns>
  267. // /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is out of range.</exception>
  268. // /// <exception cref="ArgumentOutOfRangeException"><paramref name="count"/> is out of range.</exception>
  269. // /// <exception cref="ArgumentNullException"><paramref name="match"/> is null.</exception>
  270. // public int RemoveAll(int index, int count, Predicate<T> match)
  271. // {
  272. // if (index < 0)
  273. // throw new ArgumentOutOfRangeException(nameof(index));
  274. // if (count < 0)
  275. // throw new ArgumentOutOfRangeException(nameof(count));
  276. // if (index + count > Count)
  277. // throw new ArgumentOutOfRangeException(nameof(index));
  278. // if (match == null)
  279. // throw new ArgumentNullException(nameof(match));
  280. //
  281. // if (Count == 0)
  282. // return 0;
  283. //
  284. // List<T>? cluster = null;
  285. // var clusterIndex = -1;
  286. // var removedCount = 0;
  287. //
  288. // using (BlockReentrancy())
  289. // using (DeferEvents())
  290. // {
  291. // for (var i = 0; i < count; i++, index++)
  292. // {
  293. // T item = Items[index];
  294. // if (match(item))
  295. // {
  296. // Items.RemoveAt(index);
  297. // removedCount++;
  298. //
  299. // if (clusterIndex == index)
  300. // {
  301. // Debug.Assert(cluster != null);
  302. // cluster!.Add(item);
  303. // }
  304. // else
  305. // {
  306. // cluster = new List<T> { item };
  307. // clusterIndex = index;
  308. // }
  309. //
  310. // index--;
  311. // }
  312. // else if (clusterIndex > -1)
  313. // {
  314. // OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove,
  315. // cluster, clusterIndex));
  316. // clusterIndex = -1;
  317. // cluster = null;
  318. // }
  319. // }
  320. //
  321. // if (clusterIndex > -1)
  322. // OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove,
  323. // cluster, clusterIndex));
  324. // }
  325. //
  326. // if (removedCount > 0)
  327. // OnEssentialPropertiesChanged();
  328. //
  329. // return removedCount;
  330. // }
  331. //
  332. // /// <summary>
  333. // /// Removes a range of elements from the <see cref="ObservableCollection{T}"/>>.
  334. // /// </summary>
  335. // /// <param name="index">The zero-based starting index of the range of elements to remove.</param>
  336. // /// <param name="count">The number of elements to remove.</param>
  337. // /// <exception cref="ArgumentOutOfRangeException">The specified range is exceeding the collection.</exception>
  338. // public void RemoveRange(int index, int count)
  339. // {
  340. // if (index < 0)
  341. // throw new ArgumentOutOfRangeException(nameof(index));
  342. // if (count < 0)
  343. // throw new ArgumentOutOfRangeException(nameof(count));
  344. // if (index + count > Count)
  345. // throw new ArgumentOutOfRangeException(nameof(index));
  346. //
  347. // if (count == 0)
  348. // return;
  349. //
  350. // if (count == 1)
  351. // {
  352. // RemoveItem(index);
  353. // return;
  354. // }
  355. //
  356. // //Items will always be List<T>, see constructors
  357. // var items = (List<T>)Items;
  358. // List<T> removedItems = items.GetRange(index, count);
  359. //
  360. // CheckReentrancy();
  361. //
  362. // items.RemoveRange(index, count);
  363. //
  364. // OnEssentialPropertiesChanged();
  365. //
  366. // if (Count == 0)
  367. // OnCollectionReset();
  368. // else
  369. // OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove,
  370. // removedItems, index));
  371. // }
  372. //
  373. // /// <summary>
  374. // /// Clears the current collection and replaces it with the specified collection,
  375. // /// using <see cref="Comparer"/>.
  376. // /// </summary>
  377. // /// <param name="collection">The items to fill the collection with, after clearing it.</param>
  378. // /// <exception cref="ArgumentNullException"><paramref name="collection"/> is null.</exception>
  379. // public void ReplaceRange(IEnumerable<T> collection)
  380. // {
  381. // ReplaceRange(0, Count, collection);
  382. // }
  383. //
  384. // /// <summary>
  385. // /// Removes the specified range and inserts the specified collection in its position, leaving equal items in equal positions intact.
  386. // /// </summary>
  387. // /// <param name="index">The index of where to start the replacement.</param>
  388. // /// <param name="count">The number of items to be replaced.</param>
  389. // /// <param name="collection">The collection to insert in that location.</param>
  390. // /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is out of range.</exception>
  391. // /// <exception cref="ArgumentOutOfRangeException"><paramref name="count"/> is out of range.</exception>
  392. // /// <exception cref="ArgumentNullException"><paramref name="collection"/> is null.</exception>
  393. // /// <exception cref="ArgumentNullException"><paramref name="comparer"/> is null.</exception>
  394. // public void ReplaceRange(int index, int count, IEnumerable<T> collection)
  395. // {
  396. // if (index < 0)
  397. // throw new ArgumentOutOfRangeException(nameof(index));
  398. // if (count < 0)
  399. // throw new ArgumentOutOfRangeException(nameof(count));
  400. // if (index + count > Count)
  401. // throw new ArgumentOutOfRangeException(nameof(index));
  402. //
  403. // if (collection == null)
  404. // throw new ArgumentNullException(nameof(collection));
  405. //
  406. // if (!AllowDuplicates)
  407. // collection =
  408. // collection
  409. // .Distinct(Comparer)
  410. // .ToList();
  411. //
  412. // if (collection is ICollection<T> countable)
  413. // {
  414. // if (countable.Count == 0)
  415. // {
  416. // RemoveRange(index, count);
  417. // return;
  418. // }
  419. // }
  420. // else if (!collection.Any())
  421. // {
  422. // RemoveRange(index, count);
  423. // return;
  424. // }
  425. //
  426. // if (index + count == 0)
  427. // {
  428. // InsertRange(0, collection);
  429. // return;
  430. // }
  431. //
  432. // if (!(collection is IList<T> list))
  433. // list = new List<T>(collection);
  434. //
  435. // using (BlockReentrancy())
  436. // using (DeferEvents())
  437. // {
  438. // var rangeCount = index + count;
  439. // var addedCount = list.Count;
  440. //
  441. // var changesMade = false;
  442. // List<T>?
  443. // newCluster = null,
  444. // oldCluster = null;
  445. //
  446. //
  447. // int i = index;
  448. // for (; i < rangeCount && i - index < addedCount; i++)
  449. // {
  450. // //parallel position
  451. // T old = this[i], @new = list[i - index];
  452. // if (Comparer.Equals(old, @new))
  453. // {
  454. // OnRangeReplaced(i, newCluster!, oldCluster!);
  455. // continue;
  456. // }
  457. // else
  458. // {
  459. // Items[i] = @new;
  460. //
  461. // if (newCluster == null)
  462. // {
  463. // Debug.Assert(oldCluster == null);
  464. // newCluster = new List<T> { @new };
  465. // oldCluster = new List<T> { old };
  466. // }
  467. // else
  468. // {
  469. // newCluster.Add(@new);
  470. // oldCluster!.Add(old);
  471. // }
  472. //
  473. // changesMade = true;
  474. // }
  475. // }
  476. //
  477. // OnRangeReplaced(i, newCluster!, oldCluster!);
  478. //
  479. // //exceeding position
  480. // if (count != addedCount)
  481. // {
  482. // var items = (List<T>)Items;
  483. // if (count > addedCount)
  484. // {
  485. // var removedCount = rangeCount - addedCount;
  486. // T[] removed = new T[removedCount];
  487. // items.CopyTo(i, removed, 0, removed.Length);
  488. // items.RemoveRange(i, removedCount);
  489. // OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove,
  490. // removed, i));
  491. // }
  492. // else
  493. // {
  494. // var k = i - index;
  495. // T[] added = new T[addedCount - k];
  496. // for (int j = k; j < addedCount; j++)
  497. // {
  498. // T @new = list[j];
  499. // added[j - k] = @new;
  500. // }
  501. //
  502. // items.InsertRange(i, added);
  503. // OnCollectionChanged(
  504. // new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, added, i));
  505. // }
  506. //
  507. // OnEssentialPropertiesChanged();
  508. // }
  509. // else if (changesMade)
  510. // {
  511. // OnIndexerPropertyChanged();
  512. // }
  513. // }
  514. // }
  515. //
  516. // #endregion Public Methods
  517. //
  518. //
  519. // //------------------------------------------------------
  520. // //
  521. // // Protected Methods
  522. // //
  523. // //------------------------------------------------------
  524. //
  525. // #region Protected Methods
  526. //
  527. // /// <summary>
  528. // /// Called by base class Collection&lt;T&gt; when the list is being cleared;
  529. // /// raises a CollectionChanged event to any listeners.
  530. // /// </summary>
  531. // protected override void ClearItems()
  532. // {
  533. // if (Count == 0)
  534. // return;
  535. //
  536. // CheckReentrancy();
  537. // base.ClearItems();
  538. // OnEssentialPropertiesChanged();
  539. // OnCollectionReset();
  540. // }
  541. //
  542. // /// <inheritdoc/>
  543. // protected override void InsertItem(int index, T item)
  544. // {
  545. // if (!AllowDuplicates && Items.Contains(item))
  546. // return;
  547. //
  548. // base.InsertItem(index, item);
  549. // }
  550. //
  551. // /// <inheritdoc/>
  552. // protected override void SetItem(int index, T item)
  553. // {
  554. // if (AllowDuplicates)
  555. // {
  556. // if (Comparer.Equals(this[index], item))
  557. // return;
  558. // }
  559. // else if (Items.Contains(item, Comparer))
  560. // return;
  561. //
  562. // CheckReentrancy();
  563. // T oldItem = this[index];
  564. // base.SetItem(index, item);
  565. //
  566. // OnIndexerPropertyChanged();
  567. // OnCollectionChanged(NotifyCollectionChangedAction.Replace, oldItem!, item!, index);
  568. // }
  569. //
  570. // /// <summary>
  571. // /// Raise CollectionChanged event to any listeners.
  572. // /// Properties/methods modifying this ObservableCollection will raise
  573. // /// a collection changed event through this virtual method.
  574. // /// </summary>
  575. // /// <remarks>
  576. // /// When overriding this method, either call its base implementation
  577. // /// or call <see cref="BlockReentrancy"/> to guard against reentrant collection changes.
  578. // /// </remarks>
  579. // protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
  580. // {
  581. // if (_synchronizationContext == null || SynchronizationContext.Current == _synchronizationContext)
  582. // {
  583. // // Execute the CollectionChanged event on the current thread
  584. // RaiseCollectionChanged(e);
  585. // }
  586. // else
  587. // {
  588. // // Raises the CollectionChanged event on the creator thread
  589. // _synchronizationContext.Send(RaiseCollectionChanged, e);
  590. // }
  591. // }
  592. //
  593. // private void RaiseCollectionChanged(object? param)
  594. // {
  595. // // We are in the creator thread, call the base implementation directly
  596. // if (param is NotifyCollectionChangedEventArgs args)
  597. // {
  598. // if (_deferredEvents != null)
  599. // {
  600. // _deferredEvents.Add(args);
  601. // return;
  602. // }
  603. //
  604. // base.OnCollectionChanged(args);
  605. // }
  606. // }
  607. //
  608. // protected virtual IDisposable DeferEvents() => new DeferredEventsCollection(this);
  609. //
  610. // #endregion Protected Methods
  611. //
  612. //
  613. // //------------------------------------------------------
  614. // //
  615. // // Private Methods
  616. // //
  617. // //------------------------------------------------------
  618. //
  619. // #region Private Methods
  620. //
  621. // /// <summary>
  622. // /// Helper to raise Count property and the Indexer property.
  623. // /// </summary>
  624. // void OnEssentialPropertiesChanged()
  625. // {
  626. // OnPropertyChanged(EventArgsCache.CountPropertyChanged);
  627. // OnIndexerPropertyChanged();
  628. // }
  629. //
  630. // /// <summary>
  631. // /// /// Helper to raise a PropertyChanged event for the Indexer property
  632. // /// /// </summary>
  633. // void OnIndexerPropertyChanged() =>
  634. // OnPropertyChanged(EventArgsCache.IndexerPropertyChanged);
  635. //
  636. // protected override void OnPropertyChanged(PropertyChangedEventArgs e)
  637. // {
  638. // if (_synchronizationContext == null || SynchronizationContext.Current == _synchronizationContext)
  639. // {
  640. // // Execute the PropertyChanged event on the current thread
  641. // RaisePropertyChanged(e);
  642. // }
  643. // else
  644. // {
  645. // // Raises the PropertyChanged event on the creator thread
  646. // _synchronizationContext.Send(RaisePropertyChanged, e);
  647. // }
  648. // }
  649. //
  650. // private void RaisePropertyChanged(object? param)
  651. // {
  652. // // We are in the creator thread, call the base implementation directly
  653. // if (param is PropertyChangedEventArgs args)
  654. // base.OnPropertyChanged(args);
  655. // }
  656. //
  657. // /// <summary>
  658. // /// Helper to raise CollectionChanged event to any listeners
  659. // /// </summary>
  660. // void OnCollectionChanged(NotifyCollectionChangedAction action, object oldItem, object newItem, int index) =>
  661. // OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, newItem, oldItem, index));
  662. //
  663. // /// <summary>
  664. // /// Helper to raise CollectionChanged event with action == Reset to any listeners
  665. // /// </summary>
  666. // void OnCollectionReset() =>
  667. // OnCollectionChanged(EventArgsCache.ResetCollectionChanged);
  668. //
  669. // /// <summary>
  670. // /// Helper to raise event for clustered action and clear cluster.
  671. // /// </summary>
  672. // /// <param name="followingItemIndex">The index of the item following the replacement block.</param>
  673. // /// <param name="newCluster"></param>
  674. // /// <param name="oldCluster"></param>
  675. // //TODO should have really been a local method inside ReplaceRange(int index, int count, IEnumerable<T> collection, IEqualityComparer<T> comparer),
  676. // //move when supported language version updated.
  677. // void OnRangeReplaced(int followingItemIndex, ICollection<T> newCluster, ICollection<T> oldCluster)
  678. // {
  679. // if (oldCluster == null || oldCluster.Count == 0)
  680. // {
  681. // Debug.Assert(newCluster == null || newCluster.Count == 0);
  682. // return;
  683. // }
  684. //
  685. // OnCollectionChanged(
  686. // new NotifyCollectionChangedEventArgs(
  687. // NotifyCollectionChangedAction.Replace,
  688. // new List<T>(newCluster),
  689. // new List<T>(oldCluster),
  690. // followingItemIndex - oldCluster.Count));
  691. //
  692. // oldCluster.Clear();
  693. // newCluster.Clear();
  694. // }
  695. //
  696. // #endregion Private Methods
  697. //
  698. // //------------------------------------------------------
  699. // //
  700. // // Private Types
  701. // //
  702. // //------------------------------------------------------
  703. //
  704. // #region Private Types
  705. //
  706. // sealed class DeferredEventsCollection : List<NotifyCollectionChangedEventArgs>, IDisposable
  707. // {
  708. // readonly CoreObservableCollection<T> _collection;
  709. //
  710. // public DeferredEventsCollection(CoreObservableCollection<T> collection)
  711. // {
  712. // Debug.Assert(collection != null);
  713. // Debug.Assert(collection._deferredEvents == null);
  714. // _collection = collection;
  715. // _collection._deferredEvents = this;
  716. // }
  717. //
  718. // public void Dispose()
  719. // {
  720. // _collection._deferredEvents = null;
  721. // foreach (var args in this)
  722. // _collection.OnCollectionChanged(args);
  723. // }
  724. // }
  725. //
  726. // #endregion Private Types
  727. // }
  728. //
  729. // /// <remarks>
  730. // /// To be kept outside <see cref="ObservableCollection{T}"/>, since otherwise, a new instance will be created for each generic type used.
  731. // /// </remarks>
  732. // internal static class EventArgsCache
  733. // {
  734. // internal static readonly PropertyChangedEventArgs CountPropertyChanged = new PropertyChangedEventArgs("Count");
  735. //
  736. // internal static readonly PropertyChangedEventArgs IndexerPropertyChanged =
  737. // new PropertyChangedEventArgs("Item[]");
  738. //
  739. // internal static readonly NotifyCollectionChangedEventArgs ResetCollectionChanged =
  740. // new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
  741. // }
  742. // }