5.2 KiB
5.2 KiB
source_files, generated_at, model, schema_version, sha256
| source_files | generated_at | model | schema_version | sha256 | |
|---|---|---|---|---|---|
|
2026-04-16T11:42:05.172295+00:00 | zai-org/GLM-5-FP8 | 1 | 59cbfaadaa1e572f |
Documentation: DTS.Common.Core.EventManager
1. Purpose
The EventManager class provides a static, global implementation of the Publish/Subscribe (Pub/Sub) pattern. It enables decoupled communication between system components, allowing publishers to emit events without knowledge of subscribers, and subscribers to react to events without direct coupling to publishers. Additionally, it provides a diagnostic infrastructure to monitor the event system's activity, such as tracking subscription counts and event publication flow.
2. Public Interface
Delegates
SubscriberCallbackDelegate<in T>(T item)- The signature required for methods subscribing to events.
- Constraint:
Tmust be a reference type (class).
DiagnosticCallbackDelegate(EventDiagnosticType eventType, Type t, object eventData, string listener)- The signature required for methods subscribing to diagnostic events regarding the EventManager's internal operations.
Static Class: EventManager
Methods
void Publish(T eventData) where T : class- Publishes an event of type
Tto all registered subscribers. - Iterates through the subscriber list for type
T. If a subscriber has anEventFilter, the filter is evaluated; the callback is invoked only if the filter returnstrue. - If no subscribers exist for type
T, the method returns immediately without throwing an exception.
- Publishes an event of type
void Subscribe(SubscriberCallbackDelegate listener) where T : class- Registers a listener for event type
Twithout a filter. - Overload:
void Subscribe(SubscriberCallbackDelegate listener, Predicate eventFilter) where T : class - Registers a listener with a predicate filter. The callback will only be invoked if
eventFilter(eventData)returnstrue.
- Registers a listener for event type
void UnSubscribe(SubscriberCallbackDelegate listener) where T : class- Removes a specific listener from the subscriber list for type
T. - Uses the
Callbackdelegate equality to find and remove the matchingEventMetaData.
- Removes a specific listener from the subscriber list for type
void Clear()- Removes all listeners for all event types from the
SubscriberList.
- Removes all listeners for all event types from the
void SubscribeToDiagnosticEvents(DiagnosticCallbackDelegate listener)- Registers a listener to receive diagnostic events (e.g., when items are added or removed).
void UnSubscribeToDiagnosticEvents(DiagnosticCallbackDelegate listener)- Removes a specific listener from the diagnostic events list.
void ClearDiagnosticEvents()- Removes all listeners from the diagnostic events list.
Enum: EventDiagnosticType
Defines the types of diagnostic events generated by the system.
AddListener(0)AddListenerDiagnostic(1)PublishEvent(2)RemoveListenerDiagnostic(3)RemoveListener(4)
3. Invariants
- Type Constraint: All event types
Tused inPublish,Subscribe, andUnSubscribemust be reference types (class). - Thread Safety: The source code does not contain any locking mechanisms (e.g.,
lock,Monitor) around the staticDictionaryorListcollections. The module assumes a single-threaded execution context or requires external synchronization by the caller. - Reference Equality: Unsubscription relies on delegate reference equality. A subscriber must pass the exact same delegate instance used during
Subscribeto successfullyUnSubscribe. - Internal Storage: Listeners are stored internally as
EventMetaDataobjects within aList<object>, despite the dictionary value type beingList<object>.
4. Dependencies
- Internal Dependencies:
System.Collections.Generic(forDictionary,List,Predicate)System.Reflection(forMemberInfo,Typeused in diagnostics)
- External Dependencies:
- None identified from the source file alone. As a core utility, it is likely widely depended upon by higher-level application modules.
5. Gotchas
- Thread Safety Risk: The
SubscriberListandDiagnosticListare static and mutable. Concurrent calls toSubscribe/Publishfrom different threads will cause race conditions and likely throw exceptions (e.g.,InvalidOperationExceptionon the list enumerator duringPublish). - Diagnostic "Clear" Behavior: The
ClearDiagnosticEvents()method clears theDiagnosticListbefore sending the finalRemoveListenerDiagnosticevent. Consequently, the final diagnostic event is sent to an empty list and will effectively be lost/ignored. - Diagnostic Noise during Publish: The
Publishmethod sends aPublishEventdiagnostic message inside the loop for every single subscriber. If one event has 10 subscribers, 10 diagnostic events are fired for a singlePublishcall. - Memory Leaks: Because
SubscriberListis static and holds references to delegate targets, failing to callUnSubscribewill prevent the subscriber objects from being garbage collected. - Silent Failures:
Publishsilently ignores the event if no subscribers are registered.UnSubscribesilently does nothing if the listener is not found.