2 using System.Collections.Generic;
6 internal sealed
class NamedEvent<T> : IDisposable
8 private readonly Dictionary<Identifier, Action<T>> events =
new Dictionary<Identifier, Action<T>>();
10 public void Register(Identifier identifier, Action<T> action)
12 if (HasEvent(identifier))
14 throw new ArgumentException($
"Event with the identifier \"{identifier}\" has already been registered.", nameof(identifier));
17 events.Add(identifier, action);
20 public void RegisterOverwriteExisting(Identifier identifier, Action<T> action)
22 if (HasEvent(identifier))
24 Deregister(identifier);
27 Register(identifier, action);
30 public void Deregister(Identifier identifier)
32 events.Remove(identifier);
35 public void TryDeregister(Identifier identifier)
37 if (!HasEvent(identifier)) {
return; }
38 Deregister(identifier);
41 public bool HasEvent(Identifier identifier) => events.ContainsKey(identifier);
43 public void Invoke(T data)
45 foreach (var (_, action) in events)