2013-04-10 17:46:05 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2015-03-15 20:38:55 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
2013-04-10 17:46:05 +00:00
|
|
|
|
namespace ChatSharp
|
|
|
|
|
{
|
2013-04-17 16:16:55 +00:00
|
|
|
|
public class RequestManager
|
2013-04-10 17:46:05 +00:00
|
|
|
|
{
|
2013-04-17 16:16:55 +00:00
|
|
|
|
public RequestManager()
|
2013-04-10 17:46:05 +00:00
|
|
|
|
{
|
|
|
|
|
PendingOperations = new Dictionary<string, RequestOperation>();
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-17 16:16:55 +00:00
|
|
|
|
private Dictionary<string, RequestOperation> PendingOperations { get; set; }
|
2013-04-10 17:46:05 +00:00
|
|
|
|
|
2013-04-17 16:16:55 +00:00
|
|
|
|
public void QueueOperation(string key, RequestOperation operation)
|
2013-04-10 17:46:05 +00:00
|
|
|
|
{
|
|
|
|
|
if (PendingOperations.ContainsKey(key))
|
|
|
|
|
throw new InvalidOperationException("Operation is already pending.");
|
|
|
|
|
PendingOperations.Add(key, operation);
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-17 16:16:55 +00:00
|
|
|
|
public RequestOperation PeekOperation(string key)
|
2013-04-10 17:46:05 +00:00
|
|
|
|
{
|
2015-03-15 20:45:41 +00:00
|
|
|
|
var realKey = PendingOperations.Keys.FirstOrDefault(k => string.Equals(k, key, StringComparison.OrdinalIgnoreCase));
|
2015-03-11 03:56:21 +00:00
|
|
|
|
return PendingOperations[realKey];
|
2013-04-10 17:46:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-04-17 16:16:55 +00:00
|
|
|
|
public RequestOperation DequeueOperation(string key)
|
2013-04-10 17:46:05 +00:00
|
|
|
|
{
|
|
|
|
|
var operation = PendingOperations[key];
|
|
|
|
|
PendingOperations.Remove(key);
|
|
|
|
|
return operation;
|
|
|
|
|
}
|
2013-04-17 16:16:55 +00:00
|
|
|
|
}
|
2013-04-10 17:46:05 +00:00
|
|
|
|
|
2013-04-17 16:16:55 +00:00
|
|
|
|
public class RequestOperation
|
|
|
|
|
{
|
2013-04-10 17:46:05 +00:00
|
|
|
|
public object State { get; set; }
|
|
|
|
|
public Action<RequestOperation> Callback { get; set; }
|
|
|
|
|
|
|
|
|
|
public RequestOperation(object state, Action<RequestOperation> callback)
|
|
|
|
|
{
|
|
|
|
|
State = state;
|
|
|
|
|
Callback = callback;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|