Skip to content

Instantly share code, notes, and snippets.

@hackathi
Last active August 29, 2015 14:21
Show Gist options
  • Select an option

  • Save hackathi/a771f009a6b8b62b94eb to your computer and use it in GitHub Desktop.

Select an option

Save hackathi/a771f009a6b8b62b94eb to your computer and use it in GitHub Desktop.
private readonly ConcurrentBag<Tuple<Func<IUser, object>, Func<IUser, Task>>> GlobalUserListeners = new ConcurrentBag<Tuple<Func<IUser, object>, Func<IUser, Task>>>();
private readonly ConcurrentDictionary<int, IList<Tuple<Func<IUser, object>, Func<IUser, Task>, object>>> UserListeners = new ConcurrentDictionary<int, IList<Tuple<Func<IUser, object>, Func<IUser, Task>, object>>>();
private readonly ConcurrentBag<Tuple<Func<IChannel, object>, Func<IChannel, Task>>> GlobalChannelListeners = new ConcurrentBag<Tuple<Func<IChannel, object>, Func<IChannel, Task>>>();
private readonly ConcurrentDictionary<int, IList<Tuple<Func<IChannel, object>, Func<IChannel, Task>, object>>> ChannelListeners = new ConcurrentDictionary<int, IList<Tuple<Func<IChannel, object>, Func<IChannel, Task>, object>>>();
public void AddListener<T>(Func<T, object> mapper, Func<T, Task> listener) {
ConcurrentBag<Tuple<Func<T, object>, Func<T, Task>>> globalListeners;
ConcurrentDictionary<int, IList<Tuple<Func<T, object>, Func<T, Task>, object>>> listeners;
Type t = typeof(T);
if (t == typeof(IUser)) {
object o = GlobalUserListeners;
globalListeners = (ConcurrentBag<Tuple<Func<T, object>, Func<T, Task>>>)o;
listeners = UserListeners;
} else if (t == typeof(IChannel)) {
globalListeners = GlobalChannelListeners;
listeners = ChannelListeners;
} else {
throw new TypeAccessException();
}
}
private void convertBag<T1, T2>(ConcurrentBag<Tuple<Func<T1, object>, Func<T1, Task>>> bag)
{
var ret = new ConcurrentBag<Tuple<Func<T2, object>, Func<T2, Task>>>();
foreach (var t in bag) {
Func<T2, object> f1 = (Func<T2, object>)t.Item1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment