From de6dc39fe60222f6a9ec696a98ebb7cbe96d5c83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joonatan=20Uusv=C3=A4li?= Date: Sun, 22 Nov 2026 17:36:39 +0200 Subject: [PATCH] Document weak event publication behavior --- README.md | 17 +++++++++++++---- src/WeakEvent/PACKAGE.md | 17 +++++++++++++---- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 0b86692..af42e4b 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,15 @@ In the context of Blazor components, this functionality is particularly valuable - **Automatic Cleanup:** Dead subscribers (whose targets have been garbage-collected) are automatically removed when the event is raised. - **Simple API:** Intuitive methods for subscribing, unsubscribing, and publishing events. +### Publication behavior + +- Subscribers are invoked sequentially in subscription order. +- Publishing uses a subscriber snapshot taken when publication starts. Subscribing or unsubscribing during publication affects later publications, not the current one. +- Cancellation is observed before each subscriber is invoked. Cancellation-aware subscribers receive the token passed to `PublishAsync`. +- Static handlers remain alive for the lifetime of the event source. Instance handlers, including capturing lambdas, are held weakly and do not need explicit unsubscription to become collectible. +- Exceptions from synchronous handlers propagate as their original exception type. Exceptions from asynchronous handlers are propagated after the returned task is awaited. +- Multicast delegates subscribe each invocation independently and can be unsubscribed as a group. + ## Installation Install the latest stable package via NuGet: @@ -82,11 +91,11 @@ public record MyEventData(string Message); * `Subscribe(Action handler)`\ `Subscribe(Func handler)`\ `Subscribe(Func handler)`\ - Subscribes the specified handler to the event. The handler will be invoked when the event is raised, provided that its target is still alive. + Subscribes the specified handler to the event. A multicast delegate subscribes each invocation. The handlers will be invoked when the event is raised, provided that their targets are still alive. * `Unsubscribe(Action handler)`\ `Unsubscribe(Func handler)`\ `Unsubscribe(Func handler)`\ - Unsubscribes the specified handler from the event. + Unsubscribes the specified handler from the event. A multicast delegate unsubscribes all matching invocations. * `PublishAsync(CancellationToken cancellationToken = default)`\ Raises the event by invoking all live subscribers. Dead subscribers (whose targets have been garbage-collected) are removed. @@ -96,11 +105,11 @@ public record MyEventData(string Message); * `Subscribe(Action handler)`\ `Subscribe(Func handler)`\ `Subscribe(Func handler)`\ - Subscribes the specified handler to the event. The handler will be invoked when the event is raised, provided that its target is still alive. + Subscribes the specified handler to the event. A multicast delegate subscribes each invocation. The handlers will be invoked when the event is raised, provided that their targets are still alive. * `Unsubscribe(Action handler)`\ `Unsubscribe(Func handler)`\ `Unsubscribe(Func handler)`\ - Unsubscribes the specified handler from the event. + Unsubscribes the specified handler from the event. A multicast delegate unsubscribes all matching invocations. * `PublishAsync(TEvent eventData, CancellationToken cancellationToken = default)`\ Raises the event by invoking all live subscribers with the provided event data. Dead subscribers (whose targets have been garbage-collected) are removed. diff --git a/src/WeakEvent/PACKAGE.md b/src/WeakEvent/PACKAGE.md index 08433f2..aebef41 100644 --- a/src/WeakEvent/PACKAGE.md +++ b/src/WeakEvent/PACKAGE.md @@ -18,6 +18,15 @@ WeakEvent is a lightweight .NET library that implements the weak event pattern u - **Automatic Cleanup:** Dead subscribers are removed automatically during event publishing. - **Simple API:** Intuitive methods for subscribing, unsubscribing, and publishing events. +## Publication behavior + +- Subscribers are invoked sequentially in subscription order. +- Publishing uses a subscriber snapshot taken when publication starts. Subscribing or unsubscribing during publication affects later publications, not the current one. +- Cancellation is observed before each subscriber is invoked. Cancellation-aware subscribers receive the token passed to `PublishAsync`. +- Static handlers remain alive for the lifetime of the event source. Instance handlers, including capturing lambdas, are held weakly. +- Synchronous exceptions propagate as their original exception type, while asynchronous exceptions are propagated after awaiting the handler. +- Multicast delegates subscribe and unsubscribe their individual invocations. + ## Installation Install via NuGet: @@ -73,11 +82,11 @@ public record MyEventData(string Message); * `Subscribe(Action handler)`\ `Subscribe(Func handler)`\ `Subscribe(Func handler)`\ - Subscribes the specified handler to the event. The handler will be invoked when the event is raised, provided that its target is still alive. + Subscribes the specified handler to the event. A multicast delegate subscribes each invocation. The handlers will be invoked when the event is raised, provided that their targets are still alive. * `Unsubscribe(Action handler)`\ `Unsubscribe(Func handler)`\ `Unsubscribe(Func handler)`\ - Unsubscribes the specified handler from the event. + Unsubscribes the specified handler from the event. A multicast delegate unsubscribes all matching invocations. * `PublishAsync(CancellationToken cancellationToken = default)`\ Raises the event by invoking all live subscribers. Dead subscribers (whose targets have been garbage-collected) are removed. @@ -87,10 +96,10 @@ public record MyEventData(string Message); * `Subscribe(Action handler)`\ `Subscribe(Func handler)`\ `Subscribe(Func handler)`\ - Subscribes the specified handler to the event. The handler will be invoked when the event is raised, provided that its target is still alive. + Subscribes the specified handler to the event. A multicast delegate subscribes each invocation. The handlers will be invoked when the event is raised, provided that their targets are still alive. * `Unsubscribe(Action handler)`\ `Unsubscribe(Func handler)`\ `Unsubscribe(Func handler)`\ - Unsubscribes the specified handler from the event. + Unsubscribes the specified handler from the event. A multicast delegate unsubscribes all matching invocations. * `PublishAsync(TEvent eventData, CancellationToken cancellationToken = default)`\ Raises the event by invoking all live subscribers with the provided event data. Dead subscribers (whose targets have been garbage-collected) are removed. \ No newline at end of file