-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathObservableConcurrentQueue.cs
More file actions
55 lines (51 loc) · 1.85 KB
/
Copy pathObservableConcurrentQueue.cs
File metadata and controls
55 lines (51 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StatTag.Models
{
/// <summary>
/// We will acknowledge that a similar class with the same name exists (https://github.com/cyounes/ObservableConcurrentQueue), but
/// this implementation is not directly based on and does not use any of that code.
/// </summary>
/// <typeparam name="T"></typeparam>
public class ObservableConcurrentQueue<T> : ConcurrentQueue<T>
{
public delegate void ItemAddedHandler(ConcurrentQueue<T> queue, T item);
public event ItemAddedHandler ItemAdded;
public new void Enqueue(T item)
{
base.Enqueue(item);
HandleItemAdded(item);
}
/// <summary>
/// Enqueue an item, ensuring that only one instance of the item is within the queue.
/// This ensures that a notification about an item added is sent, regardless if the Enqueue
/// method is actually called. This reduces multiple queue entries for the same option,
/// while allowing us to notify listeners that the process of adding an item was invoked.
/// </summary>
/// <param name="item"></param>
public void EnqueueDistinctWithNotification(T item)
{
if (!this.Contains(item))
{
Enqueue(item);
}
else
{
// If we haven't added the item (meaning, it was already in the list), our collection
// still provides a notification to any listeners.
HandleItemAdded(item);
}
}
private void HandleItemAdded(T item)
{
if (ItemAdded != null)
{
ItemAdded(this, item);
}
}
}
}