forked from gitextensions/gitextensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLockableNotifier.cs
80 lines (69 loc) · 2.01 KB
/
LockableNotifier.cs
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using GitUIPluginInterfaces;
using System;
using System.Text;
namespace GitCommands
{
public abstract class LockableNotifier : ILockableNotifier
{
private int lockCount = 0;
private bool notifyRequested = false;
protected abstract void InternalNotify();
private void CheckNotify()
{
if (!IsLocked && notifyRequested)
{
notifyRequested = false;
InternalNotify();
}
}
/// <summary>
/// notifies if is unlocked
/// </summary>
public void Notify()
{
notifyRequested = true;
CheckNotify();
}
/// <summary>
/// locks raising notification
/// </summary>
public void Lock()
{
lockCount++;
}
/// <summary>
/// unlocks raising notification
/// to unlock raising notification, UnLock has to be called as many times as Lock was called
/// </summary>
/// <param name="requestNotify">true if Notify has to be called</param>
public void UnLock(bool requestNotify)
{
if (lockCount > 0)
lockCount--;
else
throw new InvalidOperationException("There was no counterpart call to Lock");
if (requestNotify)
Notify();
else
CheckNotify();
}
/// <summary>
/// true if raising notification is locked
/// </summary>
public bool IsLocked { get { return lockCount != 0; } }
}
public class ActionNotifier : LockableNotifier
{
private Action NotifyAction;
public ActionNotifier(Action aNotifyAction)
{
if (aNotifyAction == null)
throw new ArgumentNullException("aNotifyAction");
NotifyAction = aNotifyAction;
}
protected override void InternalNotify()
{
NotifyAction();
}
}
}