Skip to content

Commit a4928e6

Browse files
committed
Refactor the CommandBase and ComCommandBase to be more smarter about executing the conditions on the OnExecute path by having a separate collection rather than re-using the whole collection of functions for CanExecute.
1 parent e867996 commit a4928e6

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

Rubberduck.Core/UI/Command/ComCommands/ComCommandBase.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@ public abstract class ComCommandBase : CommandBase
99
protected ComCommandBase(IVbeEvents vbeEvents)
1010
{
1111
_vbeEvents = vbeEvents;
12-
AddToCanExecuteEvaluation(SpecialEvaluateCanExecute);
12+
AddToCanExecuteEvaluation(SpecialEvaluateCanExecute, true);
1313
}
1414

1515
private bool SpecialEvaluateCanExecute(object parameter)
1616
{
1717
return !_vbeEvents.Terminated;
1818
}
19-
20-
protected sealed override bool RequireReEvaluationOnExecute => true;
2119
}
2220
}

Rubberduck.Core/UI/Command/CommandBase.cs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,42 @@ protected CommandBase(ILogger logger = null)
2222
protected abstract void OnExecute(object parameter);
2323

2424
protected Func<object, bool> CanExecuteCondition { get; private set; }
25+
protected Func<object, bool> OnExecuteCondition { get; private set; }
26+
private bool RequireReEvaluationOnExecute => OnExecuteCondition != null;
2527

26-
protected void AddToCanExecuteEvaluation(Func<object, bool> furtherCanExecuteEvaluation)
28+
protected void AddToCanExecuteEvaluation(Func<object, bool> furtherCanExecuteEvaluation, bool requireReevaluation = false)
2729
{
2830
if (furtherCanExecuteEvaluation == null)
2931
{
3032
return;
3133
}
3234

33-
var currentCanExecute = CanExecuteCondition;
34-
CanExecuteCondition = (parameter) => currentCanExecute(parameter) && furtherCanExecuteEvaluation(parameter);
35+
AddToCanExecuteEvaluation(furtherCanExecuteEvaluation);
36+
37+
if (requireReevaluation)
38+
{
39+
AddToOnExecuteEvaluation(furtherCanExecuteEvaluation);
40+
}
41+
}
42+
43+
private void AddToCanExecuteEvaluation(Func<object, bool> furtherCanExecuteEvaluation)
44+
{
45+
var currentCanExecuteCondition = CanExecuteCondition;
46+
CanExecuteCondition = (parameter) =>
47+
currentCanExecuteCondition(parameter) && furtherCanExecuteEvaluation(parameter);
48+
}
49+
50+
private void AddToOnExecuteEvaluation(Func<object, bool> furtherCanExecuteEvaluation)
51+
{
52+
if (OnExecuteCondition == null)
53+
{
54+
OnExecuteCondition = furtherCanExecuteEvaluation;
55+
}
56+
else
57+
{
58+
var currentOnExecute = OnExecuteCondition;
59+
OnExecuteCondition = (parameter) => currentOnExecute(parameter) && furtherCanExecuteEvaluation(parameter);
60+
}
3561
}
3662

3763
public bool CanExecute(object parameter)
@@ -53,15 +79,13 @@ public bool CanExecute(object parameter)
5379
}
5480
}
5581

56-
protected virtual bool RequireReEvaluationOnExecute => false;
57-
5882
public void Execute(object parameter)
5983
{
6084
try
6185
{
6286
if (RequireReEvaluationOnExecute)
6387
{
64-
if (!CanExecute(parameter))
88+
if (!OnExecuteCondition(parameter))
6589
{
6690
return;
6791
}

0 commit comments

Comments
 (0)