Skip to content

Commit

Permalink
Merge pull request #7 from brminnick/Release-1.1.0
Browse files Browse the repository at this point in the history
Release 1.1.0
  • Loading branch information
brminnick authored Dec 16, 2018
2 parents 4550911 + 292df67 commit 67ceabd
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 19 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Extensions for `System.Threading.Tasks.Task`, inspired by [John Thiriet](https:/
- AsyncAwaitBestPractices
- An extension method to safely fire-and-forget a `Task`:
- `SafeFireAndForget`
- `WeakEventManager`
- [Usage instructions below](#asyncawaitbestpractices)
- AsyncAwaitBestPractices.MVVM
- Allows for `Task` to safely be used asynchronously with `ICommand`:
Expand Down Expand Up @@ -60,6 +61,21 @@ async Task ExampleAsyncMethod()
}
```

An event implementation that enables the [garbage collector to collect an object without needing to unsubscribe event handlers](http://paulstovell.com/blog/weakevents):
- `WeakEventManager`

```csharp
readonly WeakEventManager _weakEventManager = new WeakEventManager();

public event EventHandler CanExecuteChanged
{
add => _weakEventManager.AddEventHandler(value);
remove => _weakEventManager.RemoveEventHandler(value);
}

public void RaiseCanExecuteChanged() => _weakEventManager.HandleEvent(this, EventArgs.Empty, nameof(CanExecuteChanged));
```

### AsyncAwaitBestPractices.MVVM

Allows for `Task` to safely be used asynchronously with `ICommand`:
Expand Down
8 changes: 4 additions & 4 deletions Src/AsyncAwaitBestPractices.MVVM.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata minClientVersion="2.5">
<id>AsyncAwaitBestPractices.MVVM</id>
<version>1.0.1</version>
<version>1.1.0</version>
<title>Task Extensions for MVVM</title>
<authors>Brandon Minnick, John Thiriet</authors>
<owners>Brandon Minnick</owners>
Expand All @@ -14,12 +14,12 @@
<summary>Includes AsyncCommand and IAsyncCommand which allows ICommand to safely be used asynchronously with Task.</summary>
<tags>task,fire and forget, threading, extensions, system.threading.tasks,async,await</tags>
<dependencies>
<dependency id="AsyncAwaitBestPractices" version="1.0.1" />
<dependency id="AsyncAwaitBestPractices" version="1.1.0" />
</dependencies>
<releaseNotes>
New In This Release:
- AsyncAwaitBestPractices.MVVM.AsyncCommand
- AsyncAwaitBestPractices.MVVM.IAsyncCommand
- Improved memory management
- Implemented WeakEventManager for AsyncCommand.CanExecuteChanged
</releaseNotes>
<copyright>Copyright (c) 2018 Brandon Minnick</copyright>
</metadata>
Expand Down
8 changes: 4 additions & 4 deletions Src/AsyncAwaitBestPractices.MVVM/AsyncCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public AsyncCommand(Func<T, Task> execute,
/// </summary>
public event EventHandler CanExecuteChanged
{
add { _weakEventManager.AddEventHandler(nameof(CanExecuteChanged), value); }
remove { _weakEventManager.RemoveEventHandler(nameof(CanExecuteChanged), value); }
add => _weakEventManager.AddEventHandler(value);
remove => _weakEventManager.RemoveEventHandler(value);
}
#endregion

Expand Down Expand Up @@ -135,8 +135,8 @@ public AsyncCommand(Func<Task> execute,
/// </summary>
public event EventHandler CanExecuteChanged
{
add { _weakEventManager.AddEventHandler(nameof(CanExecuteChanged), value); }
remove { _weakEventManager.RemoveEventHandler(nameof(CanExecuteChanged), value); }
add => _weakEventManager.AddEventHandler(value);
remove => _weakEventManager.RemoveEventHandler(value);
}
#endregion

Expand Down
5 changes: 2 additions & 3 deletions Src/AsyncAwaitBestPractices.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata minClientVersion="2.5">
<id>AsyncAwaitBestPractices</id>
<version>1.0.1</version>
<version>1.1.0</version>
<title>Task Extensions for System.Threading.Tasks</title>
<authors>Brandon Minnick, John Thiriet</authors>
<owners>Brandon Minnick</owners>
Expand All @@ -19,8 +19,7 @@
<tags>task,fire and forget, threading, extensions, system.threading.tasks,async,await</tags>
<releaseNotes>
New In This Release:
- Improved memory management
- Implemented WeakEventManager
- Added WeakEventManager
</releaseNotes>
<copyright>Copyright (c) 2018 Brandon Minnick</copyright>
</metadata>
Expand Down
17 changes: 9 additions & 8 deletions Src/AsyncAwaitBestPractices/WeakEventManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Reflection;

using static System.String;
using System.Runtime.CompilerServices;

namespace AsyncAwaitBestPractices
{
Expand All @@ -17,10 +18,10 @@ public class WeakEventManager
/// <summary>
/// Adds the event handler
/// </summary>
/// <param name="eventName">Event name</param>
/// <param name="handler">Handler</param>
/// <param name="eventName">Event name</param>
/// <typeparam name="TEventArgs">EventHandler type</typeparam>
public void AddEventHandler<TEventArgs>(string eventName, EventHandler<TEventArgs> handler)
public void AddEventHandler<TEventArgs>(EventHandler<TEventArgs> handler, [CallerMemberName] string eventName = "")
where TEventArgs : EventArgs
{
if (IsNullOrWhiteSpace(eventName))
Expand All @@ -35,9 +36,9 @@ public void AddEventHandler<TEventArgs>(string eventName, EventHandler<TEventArg
/// <summary>
/// Adds the event handler
/// </summary>
/// <param name="eventName">Event name</param>
/// <param name="handler">Handler</param>
public void AddEventHandler(string eventName, EventHandler handler)
/// <param name="eventName">Event name</param>
public void AddEventHandler(EventHandler handler, [CallerMemberName] string eventName = "")
{
if (IsNullOrWhiteSpace(eventName))
throw new ArgumentNullException(nameof(eventName));
Expand Down Expand Up @@ -96,10 +97,10 @@ public void HandleEvent(object sender, object args, string eventName)
/// <summary>
/// Removes the event handler
/// </summary>
/// <param name="eventName">Event name</param>
/// <param name="handler">Handler</param>
/// <param name="eventName">Event name</param>
/// <typeparam name="TEventArgs">EventHandler type</typeparam>
public void RemoveEventHandler<TEventArgs>(string eventName, EventHandler<TEventArgs> handler)
public void RemoveEventHandler<TEventArgs>(EventHandler<TEventArgs> handler, [CallerMemberName] string eventName = "")
where TEventArgs : EventArgs
{
if (IsNullOrEmpty(eventName))
Expand All @@ -114,9 +115,9 @@ public void RemoveEventHandler<TEventArgs>(string eventName, EventHandler<TEvent
/// <summary>
/// Removes the event handler.
/// </summary>
/// <param name="eventName">Event name</param>
/// <param name="handler">Handler</param>
public void RemoveEventHandler(string eventName, EventHandler handler)
/// <param name="eventName">Event name</param>
public void RemoveEventHandler(EventHandler handler, [CallerMemberName] string eventName = "")
{
if (IsNullOrEmpty(eventName))
throw new ArgumentNullException(nameof(eventName));
Expand Down

0 comments on commit 67ceabd

Please sign in to comment.