Skip to content

Commit 6a05a2d

Browse files
Document public API
1 parent 10ecde8 commit 6a05a2d

3 files changed

Lines changed: 89 additions & 5 deletions

File tree

src/WeakEvent/AsyncWeakEventSource.cs

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,84 @@
1+
using System;
12
using System.Threading.Tasks;
23
using static WeakEvent.WeakEventSourceHelper;
34

45
namespace WeakEvent
56
{
7+
/// <summary>
8+
/// Represents the method that will handle an event asynchronously when the event provides data.
9+
/// </summary>
10+
/// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
11+
/// <param name="sender">The source of the event.</param>
12+
/// <param name="e">An object that contains the event data.</param>
13+
/// <returns></returns>
614
public delegate Task AsyncEventHandler<TEventArgs>(object sender, TEventArgs e);
7-
15+
16+
/// <summary>
17+
/// An async event with weak subscription, i.e. it won't keep handlers from being garbage collected.
18+
/// </summary>
19+
/// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
820
public class AsyncWeakEventSource<TEventArgs>
921
{
1022
private DelegateCollection? _handlers;
1123

12-
public async Task RaiseAsync(object? sender, TEventArgs e)
24+
/// <summary>
25+
/// Raises the event by invoking each handler that hasn't been garbage collected.
26+
/// </summary>
27+
/// <param name="sender">The source of the event.</param>
28+
/// <param name="args">An object that contains the event data.</param>
29+
/// <remarks>The handlers are invoked one after the other, in the order they were subscribed in.
30+
/// Each handler is awaited before invoking the next one.</remarks>
31+
public async Task RaiseAsync(object? sender, TEventArgs args)
1332
{
1433
var validHandlers = GetValidHandlers(_handlers);
1534
foreach (var handler in validHandlers)
1635
{
17-
await handler.Invoke(sender, e);
36+
await handler.Invoke(sender, args);
1837
}
1938
}
2039

40+
/// <summary>
41+
/// Adds an event handler.
42+
/// </summary>
43+
/// <param name="handler">The handler to subscribe.</param>
44+
/// <remarks>Only a weak reference to the handler's <c>Target</c> is kept, so that it can be garbage collected.</remarks>
2145
public void Subscribe(AsyncEventHandler<TEventArgs> handler)
2246
{
2347
Subscribe(null, handler);
2448
}
2549

50+
/// <summary>
51+
/// Adds an event handler, specifying a lifetime object.
52+
/// </summary>
53+
/// <param name="lifetimeObject">An object that keeps the handler alive as long as it's alive.</param>
54+
/// <param name="handler">The handler to subscribe.</param>
55+
/// <remarks>Only a weak reference to the handler's <c>Target</c> is kept, so that it can be garbage collected.
56+
/// However, as long as the <c>lifetime</c> object is alive, the handler will be kept alive. This is useful for
57+
/// subscribing with anonymous methods (e.g. lambda expressions). Note that you must specify the same lifetime
58+
/// object to unsubscribe, otherwise the handler's lifetime will remain tied to the lifetime object.</remarks>
2659
public void Subscribe(object? lifetimeObject, AsyncEventHandler<TEventArgs> handler)
2760
{
2861
Subscribe<DelegateCollection, OpenEventHandler, StrongHandler>(lifetimeObject, ref _handlers, handler);
2962
}
3063

64+
/// <summary>
65+
/// Removes an event handler.
66+
/// </summary>
67+
/// <param name="handler">The handler to unsubscribe.</param>
68+
/// <remarks>The behavior is the same as that of <see cref="Delegate.Remove(Delegate, Delegate)"/>. Only the last instance
69+
/// of the handler's invocation list is removed. If the exact invocation list is not found, nothing is removed.</remarks>
3170
public void Unsubscribe(AsyncEventHandler<TEventArgs> handler)
3271
{
3372
Unsubscribe(null, handler);
3473
}
3574

75+
/// <summary>
76+
/// Removes an event handler that was subscribed with a lifetime object.
77+
/// </summary>
78+
/// <param name="lifetimeObject">The lifetime object that was associated with the handler.</param>
79+
/// <param name="handler">The handler to unsubscribe.</param>
80+
/// <remarks>The behavior is the same as that of <see cref="Delegate.Remove(Delegate, Delegate)"/>. Only the last instance
81+
/// of the handler's invocation list is removed. If the exact invocation list is not found, nothing is removed.</remarks>
3682
public void Unsubscribe(object? lifetimeObject, AsyncEventHandler<TEventArgs> handler)
3783
{
3884
Unsubscribe<OpenEventHandler, StrongHandler>(lifetimeObject, _handlers, handler);

src/WeakEvent/WeakEvent.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<AssemblyOriginatorKeyFile>WeakEvent.snk</AssemblyOriginatorKeyFile>
1111
<SignAssembly>true</SignAssembly>
1212
<MinVerVerbosity>quiet</MinVerVerbosity>
13+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1314
</PropertyGroup>
1415

1516
<PropertyGroup Label="Package properties">

src/WeakEvent/WeakEventSource.cs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,74 @@
33

44
namespace WeakEvent
55
{
6+
/// <summary>
7+
/// An event with weak subscription, i.e. it won't keep handlers from being garbage collected.
8+
/// </summary>
9+
/// <typeparam name="TEventArgs">The type of the event's arguments.</typeparam>
610
public class WeakEventSource<TEventArgs>
711
#if NET40
812
where TEventArgs : EventArgs
913
#endif
1014
{
1115
private DelegateCollection? _handlers;
1216

13-
public void Raise(object? sender, TEventArgs e)
17+
/// <summary>
18+
/// Raises the event by invoking each handler that hasn't been garbage collected.
19+
/// </summary>
20+
/// <param name="sender">The source of the event.</param>
21+
/// <param name="args">An object that contains the event data.</param>
22+
/// <remarks>The handlers are invoked one after the other, in the order they were subscribed in.</remarks>
23+
public void Raise(object? sender, TEventArgs args)
1424
{
1525
var validHandlers = GetValidHandlers(_handlers);
1626
foreach (var handler in validHandlers)
1727
{
18-
handler.Invoke(sender, e);
28+
handler.Invoke(sender, args);
1929
}
2030
}
2131

32+
/// <summary>
33+
/// Adds an event handler.
34+
/// </summary>
35+
/// <param name="handler">The handler to subscribe.</param>
36+
/// <remarks>Only a weak reference to the handler's <c>Target</c> is kept, so that it can be garbage collected.</remarks>
2237
public void Subscribe(EventHandler<TEventArgs> handler)
2338
{
2439
Subscribe(null, handler);
2540
}
2641

42+
/// <summary>
43+
/// Adds an event handler, specifying a lifetime object.
44+
/// </summary>
45+
/// <param name="lifetimeObject">An object that keeps the handler alive as long as it's alive.</param>
46+
/// <param name="handler">The handler to subscribe.</param>
47+
/// <remarks>Only a weak reference to the handler's <c>Target</c> is kept, so that it can be garbage collected.
48+
/// However, as long as the <c>lifetime</c> object is alive, the handler will be kept alive. This is useful for
49+
/// subscribing with anonymous methods (e.g. lambda expressions). Note that you must specify the same lifetime
50+
/// object to unsubscribe, otherwise the handler's lifetime will remain tied to the lifetime object.</remarks>
2751
public void Subscribe(object? lifetimeObject, EventHandler<TEventArgs> handler)
2852
{
2953
Subscribe<DelegateCollection, OpenEventHandler, StrongHandler>(lifetimeObject, ref _handlers, handler);
3054
}
3155

56+
/// <summary>
57+
/// Removes an event handler.
58+
/// </summary>
59+
/// <param name="handler">The handler to unsubscribe.</param>
60+
/// <remarks>The behavior is the same as that of <see cref="Delegate.Remove(Delegate, Delegate)"/>. Only the last instance
61+
/// of the handler's invocation list is removed. If the exact invocation list is not found, nothing is removed.</remarks>
3262
public void Unsubscribe(EventHandler<TEventArgs> handler)
3363
{
3464
Unsubscribe(null, handler);
3565
}
3666

67+
/// <summary>
68+
/// Removes an event handler that was subscribed with a lifetime object.
69+
/// </summary>
70+
/// <param name="lifetimeObject">The lifetime object that was associated with the handler.</param>
71+
/// <param name="handler">The handler to unsubscribe.</param>
72+
/// <remarks>The behavior is the same as that of <see cref="Delegate.Remove(Delegate, Delegate)"/>. Only the last instance
73+
/// of the handler's invocation list is removed. If the exact invocation list is not found, nothing is removed.</remarks>
3774
public void Unsubscribe(object? lifetimeObject, EventHandler<TEventArgs> handler)
3875
{
3976
Unsubscribe<OpenEventHandler, StrongHandler>(lifetimeObject, _handlers, handler);

0 commit comments

Comments
 (0)