forked from brian91292/ChatCore
-
Notifications
You must be signed in to change notification settings - Fork 7
/
DictionaryUtils.cs
153 lines (133 loc) · 3.98 KB
/
DictionaryUtils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
using System.Collections.Concurrent;
using System.Reflection;
using Microsoft.Extensions.Logging;
namespace ChatCore.Utilities
{
public static class DictionaryUtils
{
public static void AddAction(this ConcurrentDictionary<Assembly, Action> dict, Assembly assembly, Action value)
{
dict.AddOrUpdate(assembly, value, (_, existingActions) => existingActions + value);
}
public static void AddAction<TA>(this ConcurrentDictionary<Assembly, Action<TA>> dict, Assembly assembly, Action<TA> value)
{
dict.AddOrUpdate(assembly, value, (_, existingActions) => existingActions + value);
}
public static void AddAction<TA, TB>(this ConcurrentDictionary<Assembly, Action<TA, TB>> dict, Assembly assembly, Action<TA, TB> value)
{
dict.AddOrUpdate(assembly, value, (_, existingActions) => existingActions + value);
}
public static void AddAction<TA, TB, TC>(this ConcurrentDictionary<Assembly, Action<TA, TB, TC>> dict, Assembly assembly, Action<TA, TB, TC> value)
{
dict.AddOrUpdate(assembly, value, (_, existingActions) => existingActions + value);
}
public static void RemoveAction(this ConcurrentDictionary<Assembly, Action> dict, Assembly assembly, Action? value)
{
if (!dict.TryGetValue(assembly, out var compoundAction))
{
return;
}
compoundAction -= value;
dict[assembly] = compoundAction!;
}
public static void RemoveAction<TA>(this ConcurrentDictionary<Assembly, Action<TA>> dict, Assembly assembly, Action<TA> value)
{
if (!dict.TryGetValue(assembly, out var compoundAction))
{
return;
}
compoundAction -= value;
dict[assembly] = compoundAction!;
}
public static void RemoveAction<TA, TB>(this ConcurrentDictionary<Assembly, Action<TA, TB>> dict, Assembly assembly, Action<TA, TB> value)
{
if (!dict.TryGetValue(assembly, out var compoundAction))
{
return;
}
compoundAction -= value;
dict[assembly] = compoundAction!;
}
public static void RemoveAction<TA, TB, TC>(this ConcurrentDictionary<Assembly, Action<TA, TB, TC>> dict, Assembly assembly, Action<TA, TB, TC> value)
{
if (dict.TryGetValue(assembly, out var compoundAction))
{
compoundAction -= value;
dict[assembly] = compoundAction!;
}
}
public static void InvokeAll(this ConcurrentDictionary<Assembly, Action> dict, Assembly assembly, ILogger? logger = null)
{
foreach (var kvp in dict)
{
if (kvp.Key == assembly)
{
continue;
}
try
{
kvp.Value?.Invoke();
}
catch (Exception ex)
{
logger?.LogError(ex, $"An exception occurred while invoking action no params.");
}
}
}
public static void InvokeAll<TA>(this ConcurrentDictionary<Assembly, Action<TA>> dict, Assembly assembly, TA a, ILogger? logger = null)
{
foreach (var kvp in dict)
{
if (kvp.Key == assembly)
{
continue;
}
try
{
kvp.Value?.Invoke(a);
}
catch (Exception ex)
{
logger?.LogError(ex, $"An exception occurred while invoking action with param type {typeof(TA).Name}");
}
}
}
public static void InvokeAll<TA, TB>(this ConcurrentDictionary<Assembly, Action<TA, TB>> dict, Assembly assembly, TA a, TB b, ILogger? logger = null)
{
foreach (var kvp in dict)
{
if (kvp.Key == assembly)
{
continue;
}
try
{
kvp.Value?.Invoke(a, b);
}
catch (Exception ex)
{
logger?.LogError(ex, $"An exception occurred while invoking action with param types {typeof(TA).Name}, {typeof(TB).Name}");
}
}
}
public static void InvokeAll<TA, TB, TC>(this ConcurrentDictionary<Assembly, Action<TA, TB, TC>> dict, Assembly assembly, TA a, TB b, TC c, ILogger? logger = null)
{
foreach (var kvp in dict)
{
if (kvp.Key == assembly)
{
continue;
}
try
{
kvp.Value?.Invoke(a, b, c);
}
catch (Exception ex)
{
logger?.LogError(ex, $"An exception occurred while invoking action with param types {typeof(TA).Name}, {typeof(TB).Name}, {typeof(TC).Name}");
}
}
}
}
}