Last active
August 29, 2015 14:10
-
-
Save smoothdeveloper/690a1a47170e9c4faf20 to your computer and use it in GitHub Desktop.
LatchedActionsQueue.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LatchedActionsQueue { | |
private List<KeyValuePair<object, Action>> actionsQueue = new List<KeyValuePair<object, Action>>(); | |
public void Enlist(Action action) { | |
enlist(action, action); | |
} | |
public void Enlist<T1>(Action<T1> action, T1 p1) { | |
enlist(action, () => action(p1)); | |
} | |
public void Enlist<T1, T2>(Action<T1, T2> action, T1 p1, T2 p2) { | |
enlist(action, () => action(p1, p2)); | |
} | |
public void Enlist(object key, Action action) { | |
enlist(key, action); | |
} | |
public void Enlist<T1>(object key, Action<T1> action, T1 p1) { | |
enlist(key, () => action(p1)); | |
} | |
public void Enlist<T1, T2>(object key, Action<T1, T2> action, T1 p1, T2 p2) { | |
enlist(key, () => action(p1, p2)); | |
} | |
void enlist(object key, Action action) { | |
actionsQueue.Add(new KeyValuePair<object, Action>(key, action)); | |
} | |
public void DequeueActions() { | |
var alreadyPerformedActions = new HashSet<object>(); | |
foreach (var entry in actionsQueue.AsEnumerable().Reverse()) { | |
if (alreadyPerformedActions.Contains(entry.Key)) { | |
continue; | |
} | |
alreadyPerformedActions.Add(entry.Key); | |
entry.Value(); | |
} | |
actionsQueue.Clear(); | |
} | |
} | |
[TestFixture] | |
public class LatchedActionsQueueTestFixture { | |
[Test] | |
public void issuing_several_calls_only_executes_last_key_based_on_action() { | |
var queue = new LatchedActionsQueue(); | |
bool doneTodo1 = false; | |
bool doneTodo2 = false; | |
int lastTodo1Result = default(int); | |
int lastTodo2Result = default(int); | |
Action<int> toDo1 = (value) => | |
{ | |
Assert.IsFalse(doneTodo1); | |
lastTodo1Result = value; | |
doneTodo1 = true; | |
}; | |
Action<int, int> toDo2 = (value1, value2) => | |
{ | |
Assert.IsFalse(doneTodo2); | |
lastTodo2Result = value1 + value2; | |
doneTodo2 = true; | |
}; | |
queue.Enlist(toDo1, 1); | |
queue.Enlist(toDo1, 42); | |
queue.Enlist(toDo2, 42, 0); | |
queue.Enlist(toDo2, 42, 42); | |
queue.DequeueActions(); | |
Assert.AreEqual(42, lastTodo1Result); | |
Assert.AreEqual(84, lastTodo2Result); | |
} | |
[Test] | |
public void issuing_several_calls_only_executes_last_arbitrary_key() { | |
var queue = new LatchedActionsQueue(); | |
bool doneTodo2 = false; | |
int lastTodo2Result = default(int); | |
Action<int> toDo1 = (value) => | |
{ | |
Assert.Fail(); | |
}; | |
Action<int, int> toDo2 = (value1, value2) => | |
{ | |
Assert.IsFalse(doneTodo2); | |
lastTodo2Result = value1 + value2; | |
doneTodo2 = true; | |
}; | |
queue.Enlist(1, toDo1, 1); | |
queue.Enlist(1, toDo1, 42); | |
queue.Enlist(1, toDo2, 42, 0); | |
queue.Enlist(1, toDo2, 42, 42); | |
queue.DequeueActions(); | |
Assert.AreEqual(84, lastTodo2Result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment