43 lines
1021 B
C#
43 lines
1021 B
C#
namespace HermesSocketLibrary.Requests.Callbacks
|
|
{
|
|
public class CallbackManager<A> : ICallbackManager<A> where A : class
|
|
{
|
|
private readonly IDictionary<string, A> _callbacks;
|
|
|
|
|
|
public CallbackManager()
|
|
{
|
|
_callbacks = new Dictionary<string, A>();
|
|
}
|
|
|
|
|
|
public string GenerateKeyForCallback(A callback)
|
|
{
|
|
string key = GenerateKey();
|
|
_callbacks.Add(key, callback);
|
|
return key;
|
|
}
|
|
|
|
public A? Get(string key)
|
|
{
|
|
if (_callbacks.TryGetValue(key, out var callback))
|
|
return callback;
|
|
return null;
|
|
}
|
|
|
|
public A? Take(string key)
|
|
{
|
|
if (_callbacks.TryGetValue(key, out var callback))
|
|
{
|
|
_callbacks.Remove(key);
|
|
return callback;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private string GenerateKey()
|
|
{
|
|
return Guid.NewGuid().ToString("D");
|
|
}
|
|
}
|
|
} |