diff --git a/src/WeakEvent/WeakEvent.cs b/src/WeakEvent/WeakEvent.cs
index 5bad7f7..bb53fbc 100644
--- a/src/WeakEvent/WeakEvent.cs
+++ b/src/WeakEvent/WeakEvent.cs
@@ -9,8 +9,15 @@
/// For more information, visit the GitHub repository.
///
/// The type of the event argument.
-public class WeakEvent : WeakEventBase
+public class WeakEvent
{
+ private readonly WeakEventCore _core = new();
+
+ ///
+ /// Number of living subscribers currently registered to the event.
+ ///
+ public int SubscriberCount => _core.SubscriberCount;
+
///
/// Subscribes the specified handler to the event.
///
@@ -19,7 +26,7 @@ public class WeakEvent : WeakEventBase
/// provided that the target is still alive.
///
/// Thrown when is null.
- public void Subscribe(Action handler) => base.SubscribeCore(handler);
+ public void Subscribe(Action handler) => _core.Subscribe(handler, WeakEventHandler.Create);
///
/// Subscribes the specified async handler to the event.
@@ -29,7 +36,7 @@ public class WeakEvent : WeakEventBase
/// provided that the target is still alive.
///
/// Thrown when is null.
- public void Subscribe(Func handler) => base.SubscribeCore(handler);
+ public void Subscribe(Func handler) => _core.Subscribe(handler, WeakEventHandler.Create);
///
/// Subscribes the specified async handler with a cancellation token to the event.
@@ -39,28 +46,29 @@ public class WeakEvent : WeakEventBase
/// provided that the target is still alive.
///
/// Thrown when is null.
- public void Subscribe(Func handler) => base.SubscribeCore(handler);
+ public void Subscribe(Func handler)
+ => _core.Subscribe(handler, WeakEventHandler.Create);
///
/// Unsubscribes the specified handler from the event.
///
/// The handler to unsubscribe.
/// Thrown when is null.
- public bool Unsubscribe(Action handler) => base.Unsubscribe(handler);
+ public bool Unsubscribe(Action handler) => _core.Unsubscribe(handler);
///
/// Unsubscribes the specified async handler from the event.
///
/// The handler to unsubscribe.
/// Thrown when is null.
- public bool Unsubscribe(Func handler) => base.Unsubscribe(handler);
+ public bool Unsubscribe(Func handler) => _core.Unsubscribe(handler);
///
/// Unsubscribes the specified async handler from the event.
///
/// The handler to unsubscribe.
/// Thrown when is null.
- public bool Unsubscribe(Func handler) => base.Unsubscribe(handler);
+ public bool Unsubscribe(Func handler) => _core.Unsubscribe(handler);
///
/// Raises the event, invoking all live subscribers with the provided event data.
@@ -70,7 +78,7 @@ public class WeakEvent : WeakEventBase
/// The event data to publish to the subscribers.
/// The cancellation token to cancel the operation.
public Task PublishAsync(TEvent eventData, CancellationToken cancellationToken = default)
- => base.PublishAsync(eventData, cancellationToken);
+ => _core.PublishAsync(eventData, cancellationToken);
}
///
@@ -81,8 +89,15 @@ public Task PublishAsync(TEvent eventData, CancellationToken cancellationToken =
/// Weakly referenced event subscribers. Keep your .NET events lean and memory-safe.
/// For more information, visit the GitHub repository.
///
-public class WeakEvent : WeakEventBase
+public class WeakEvent
{
+ private readonly WeakEventCore _core = new();
+
+ ///
+ /// Number of living subscribers currently registered to the event.
+ ///
+ public int SubscriberCount => _core.SubscriberCount;
+
///
/// Subscribes the specified handler to the event.
///
@@ -91,7 +106,7 @@ public class WeakEvent : WeakEventBase
/// provided that the target is still alive.
///
/// Thrown when is null.
- public void Subscribe(Action handler) => base.SubscribeCore(handler);
+ public void Subscribe(Action handler) => _core.Subscribe(handler, WeakEventHandler.Create);
///
/// Subscribes the specified async handler to the event.
@@ -101,7 +116,7 @@ public class WeakEvent : WeakEventBase
/// provided that the target is still alive.
///
/// Thrown when is null.
- public void Subscribe(Func handler) => base.SubscribeCore(handler);
+ public void Subscribe(Func handler) => _core.Subscribe(handler, WeakEventHandler.Create);
///
/// Subscribes the specified async handler with a cancellation token to the event.
@@ -111,28 +126,29 @@ public class WeakEvent : WeakEventBase
/// provided that the target is still alive.
///
/// Thrown when is null.
- public void Subscribe(Func handler) => base.SubscribeCore(handler);
+ public void Subscribe(Func handler)
+ => _core.Subscribe(handler, WeakEventHandler.Create);
///
/// Unsubscribes the specified handler from the event.
///
/// The handler to unsubscribe.
/// Thrown when is null.
- public bool Unsubscribe(Action handler) => base.Unsubscribe(handler);
+ public bool Unsubscribe(Action handler) => _core.Unsubscribe(handler);
///
/// Unsubscribes the specified async handler from the event.
///
/// The handler to unsubscribe.
/// Thrown when is null.
- public bool Unsubscribe(Func handler) => base.Unsubscribe(handler);
+ public bool Unsubscribe(Func handler) => _core.Unsubscribe(handler);
///
/// Unsubscribes the specified async handler from the event.
///
/// The handler to unsubscribe.
/// Thrown when is null.
- public bool Unsubscribe(Func handler) => base.Unsubscribe(handler);
+ public bool Unsubscribe(Func handler) => _core.Unsubscribe(handler);
///
/// Raises the event, invoking all live subscribers with the provided event data.
@@ -141,177 +157,5 @@ public class WeakEvent : WeakEventBase
///
/// The cancellation token to cancel the operation.
public Task PublishAsync(CancellationToken cancellationToken = default)
- => base.PublishAsync(null, cancellationToken);
-}
-
-///
-/// Represents a weak event that stores its subscribers using weak references.
-/// Subscribers register a and when an event is raised, only those whose target is still alive will be invoked.
-///
-public abstract class WeakEventBase
-{
- private readonly List _handlers = [];
- private readonly ReaderWriterLockSlim _rwLock = new();
-
- ///
- /// Number of living subscribers currently registered to the event.
- ///
- public int SubscriberCount
- {
- get
- {
- _rwLock.EnterReadLock();
- try
- {
- return _handlers.Count(x => x.IsAlive);
- }
- finally
- {
- _rwLock.ExitReadLock();
- }
- }
- }
-
- ///
- /// Subscribes the specified delegate handler to the event.
- ///
- ///
- /// The handler to subscribe. It will be invoked when the event is raised,
- /// provided that the target is still alive.
- ///
- /// Thrown when is null.
- protected void SubscribeCore(Action handler)
- => AddHandlers(handler, WeakEventHandler.Create);
-
- ///
- /// Subscribes the specified delegate handler to the event.
- ///
- protected void SubscribeCore(Action handler)
- => AddHandlers(handler, WeakEventHandler.Create);
-
- ///
- /// Subscribes the specified delegate handler to the event.
- ///
- protected void SubscribeCore(Func handler)
- => AddHandlers(handler, WeakEventHandler.Create);
-
- ///
- /// Subscribes the specified delegate handler to the event.
- ///
- protected void SubscribeCore(Func handler)
- => AddHandlers(handler, WeakEventHandler.Create);
-
- ///
- /// Subscribes the specified delegate handler to the event.
- ///
- protected void SubscribeCore(Func handler)
- => AddHandlers(handler, WeakEventHandler.Create);
-
- ///
- /// Subscribes the specified delegate handler to the event.
- ///
- protected void SubscribeCore(Func handler)
- => AddHandlers(handler, WeakEventHandler.Create);
-
- private void AddHandlers(
- TDelegate handler,
- Func createHandler
- )
- where TDelegate : Delegate
- {
- if (handler is null)
- {
- throw new ArgumentNullException(nameof(handler));
- }
-
- var handlers = handler
- .GetInvocationList()
- .Select(invocation => createHandler((TDelegate)invocation))
- .ToArray();
-
- _rwLock.EnterWriteLock();
- try
- {
- _handlers.AddRange(handlers);
- }
- finally
- {
- _rwLock.ExitWriteLock();
- }
- }
-
- ///
- /// Unsubscribes the specified delegate handler from the event.
- ///
- /// The handler to unsubscribe.
- /// Thrown when is null.
- protected bool Unsubscribe(Delegate handler)
- {
- if (handler is null)
- {
- throw new ArgumentNullException(nameof(handler));
- }
-
- var handlers = handler.GetInvocationList();
-
- _rwLock.EnterWriteLock();
- try
- {
- return _handlers.RemoveAll(
- weakHandler => handlers.Any(weakHandler.Matches)
- ) > 0;
- }
- finally
- {
- _rwLock.ExitWriteLock();
- }
- }
-
- ///
- /// Raises the event, invoking all live subscribers with the provided event data.
- /// Dead subscribers (whose targets have been garbage-collected) are removed.
- /// Subscribers are invoked sequentially from a snapshot captured when publication starts.
- /// Cancellation is observed before each subscriber is invoked.
- ///
- /// Event data passed to each data-taking delegate.
- /// The cancellation token to cancel the operation.
- protected async Task PublishAsync(
- object? eventData,
- CancellationToken cancellationToken = default
- )
- {
- List handlers;
-
- _rwLock.EnterUpgradeableReadLock();
-
- try
- {
- cancellationToken.ThrowIfCancellationRequested();
-
- if (_handlers.Any(x => !x.IsAlive))
- {
- _rwLock.EnterWriteLock();
- try
- {
- _handlers.RemoveAll(x => !x.IsAlive);
- }
- finally
- {
- _rwLock.ExitWriteLock();
- }
- }
-
- handlers = [.. _handlers];
- }
- finally
- {
- _rwLock.ExitUpgradeableReadLock();
- }
-
- foreach (var handler in handlers)
- {
- cancellationToken.ThrowIfCancellationRequested();
- await handler.InvokeAsync(eventData, cancellationToken);
- }
- }
+ => _core.PublishAsync