forked from Fydar/RPGCore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventDictionaryHandlerCollection.cs
155 lines (131 loc) · 3.56 KB
/
EventDictionaryHandlerCollection.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
154
155
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
namespace RPGCore.Events;
public sealed class EventDictionaryHandlerCollection<TKey, TValue> : IEnumerable<KeyValuePair<object, IEventDictionaryHandler<TKey, TValue>>>
{
public readonly ref struct ContextWrapped
{
private readonly IEventDictionary<TKey, TValue> collection;
private readonly object context;
public ContextWrapped(IEventDictionary<TKey, TValue> collection, object context)
{
this.collection = collection;
this.context = context;
}
public void Clear()
{
collection.Handlers.Clear(context);
}
public void Add(IEventDictionaryHandler<TKey, TValue> handler)
{
collection.Handlers.internalHandlers.Add(new KeyValuePair<object, IEventDictionaryHandler<TKey, TValue>>(context, handler));
}
public void AddAndInvoke(IEventDictionaryHandler<TKey, TValue> handler)
{
Add(handler);
foreach (var kvp in collection)
{
handler.OnAdd(kvp.Key, kvp.Value);
}
}
public void Remove(IEventDictionaryHandler<TKey, TValue> handler)
{
collection.Handlers.internalHandlers.Remove(new KeyValuePair<object, IEventDictionaryHandler<TKey, TValue>>(context, handler));
}
public void InvokeAndRemove(IEventDictionaryHandler<TKey, TValue> handler)
{
foreach (var kvp in collection)
{
handler.OnRemove(kvp.Key, kvp.Value);
}
Remove(handler);
}
public void InvokeAndRemove()
{
for (int i = collection.Handlers.internalHandlers.Count - 1; i >= 0; i--)
{
var handler = collection.Handlers.internalHandlers[i];
if (handler.Key == context)
{
foreach (var kvp in collection)
{
handler.Value.OnRemove(kvp.Key, kvp.Value);
}
collection.Handlers.internalHandlers.RemoveAt(i);
}
}
}
}
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly IEventDictionary<TKey, TValue> collection;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly List<KeyValuePair<object, IEventDictionaryHandler<TKey, TValue>>> internalHandlers;
public ContextWrapped this[object context] => new(collection, context);
public EventDictionaryHandlerCollection(IEventDictionary<TKey, TValue> collection)
{
this.collection = collection;
internalHandlers = new List<KeyValuePair<object, IEventDictionaryHandler<TKey, TValue>>>();
}
public void Clear(object context)
{
for (int i = internalHandlers.Count - 1; i >= 0; i--)
{
if (internalHandlers[i].Key == context)
{
internalHandlers.RemoveAt(i);
}
}
}
public void Clear()
{
internalHandlers.Clear();
}
public void InvokeAndClear()
{
InvokeRemoved();
Clear();
}
internal void InvokeAdd()
{
foreach (var kvp in collection)
{
for (int i = 0; i < internalHandlers.Count; i++)
{
internalHandlers[i].Value.OnAdd(kvp.Key, kvp.Value);
}
}
}
internal void InvokeRemoved()
{
foreach (var kvp in collection)
{
for (int i = 0; i < internalHandlers.Count; i++)
{
internalHandlers[i].Value.OnRemove(kvp.Key, kvp.Value);
}
}
}
internal void InvokeAdd(TKey key, TValue value)
{
for (int i = 0; i < internalHandlers.Count; i++)
{
internalHandlers[i].Value.OnAdd(key, value);
}
}
internal void InvokeRemoved(TKey key, TValue value)
{
for (int i = 0; i < internalHandlers.Count; i++)
{
internalHandlers[i].Value.OnRemove(key, value);
}
}
public IEnumerator<KeyValuePair<object, IEventDictionaryHandler<TKey, TValue>>> GetEnumerator()
{
return internalHandlers.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return internalHandlers.GetEnumerator();
}
}