Last active
August 29, 2015 14:02
-
-
Save trcio/c34c7d609911b366d81f to your computer and use it in GitHub Desktop.
Callbackr: Easy callback class, for all your callback needs!
This file contains hidden or 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 static class Callbackr | |
{ | |
private static Dictionary<object, Action> callbacks = new Dictionary<object, Action>(); | |
/// <summary> | |
/// Adds the specified callback with the specified identifier. | |
/// </summary> | |
/// <param name="token">The callbacks specified identifier.</param> | |
/// <param name="callback">The callback to be added.</param> | |
public static void Add(object token, Action callback) | |
{ | |
if (callbacks.ContainsKey(token)) | |
throw new InvalidOperationException("Token already exists."); | |
callbacks.Add(token, callback); | |
} | |
/// <summary> | |
/// Removes the specified callback. | |
/// </summary> | |
/// <param name="token">The callbacks specified identifier.</param> | |
public static void Remove(object token) | |
{ | |
if (!callbacks.ContainsKey(token)) | |
throw new InvalidOperationException("Token does not exist."); | |
callbacks.Remove(token); | |
} | |
/// <summary> | |
/// Executes the specified callback. | |
/// </summary> | |
/// <param name="token">The callbacks specified identifier.</param> | |
public static bool Execute(object token) | |
{ | |
if (callbacks.ContainsKey(token)) | |
{ | |
callbacks[token](); | |
return true; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment