forked from microsoft/PowerToys
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I think this pilfers the
InvokeAsync
as mentioned in #333
- Loading branch information
1 parent
ce40df9
commit 6eab9df
Showing
2 changed files
with
75 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/modules/cmdpal/Microsoft.CmdPal.UI/Helpers/TypedEventHandlerExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright (c) Microsoft Corporation | ||
// The Microsoft Corporation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using CommunityToolkit.Common.Deferred; | ||
using Windows.Foundation; | ||
|
||
// Pilfered from CommunityToolkit.WinUI.Deferred | ||
namespace Microsoft.CmdPal.UI.Deferred; | ||
|
||
/// <summary> | ||
/// Extensions to <see cref="TypedEventHandler{TSender, TResult}"/> for Deferred Events. | ||
/// </summary> | ||
public static class TypedEventHandlerExtensions | ||
{ | ||
/// <summary> | ||
/// Use to invoke an async <see cref="TypedEventHandler{TSender, TResult}"/> using <see cref="DeferredEventArgs"/>. | ||
/// </summary> | ||
/// <typeparam name="S">Type of sender.</typeparam> | ||
/// <typeparam name="R"><see cref="EventArgs"/> type.</typeparam> | ||
/// <param name="eventHandler"><see cref="TypedEventHandler{TSender, TResult}"/> to be invoked.</param> | ||
/// <param name="sender">Sender of the event.</param> | ||
/// <param name="eventArgs"><see cref="EventArgs"/> instance.</param> | ||
/// <returns><see cref="Task"/> to wait on deferred event handler.</returns> | ||
#pragma warning disable CA1715 // Identifiers should have correct prefix | ||
#pragma warning disable SA1314 // Type parameter names should begin with T | ||
public static Task InvokeAsync<S, R>(this TypedEventHandler<S, R> eventHandler, S sender, R eventArgs) | ||
#pragma warning restore SA1314 // Type parameter names should begin with T | ||
#pragma warning restore CA1715 // Identifiers should have correct prefix | ||
where R : DeferredEventArgs => InvokeAsync(eventHandler, sender, eventArgs, CancellationToken.None); | ||
|
||
/// <summary> | ||
/// Use to invoke an async <see cref="TypedEventHandler{TSender, TResult}"/> using <see cref="DeferredEventArgs"/> with a <see cref="CancellationToken"/>. | ||
/// </summary> | ||
/// <typeparam name="S">Type of sender.</typeparam> | ||
/// <typeparam name="R"><see cref="EventArgs"/> type.</typeparam> | ||
/// <param name="eventHandler"><see cref="TypedEventHandler{TSender, TResult}"/> to be invoked.</param> | ||
/// <param name="sender">Sender of the event.</param> | ||
/// <param name="eventArgs"><see cref="EventArgs"/> instance.</param> | ||
/// <param name="cancellationToken"><see cref="CancellationToken"/> option.</param> | ||
/// <returns><see cref="Task"/> to wait on deferred event handler.</returns> | ||
#pragma warning disable CA1715 // Identifiers should have correct prefix | ||
#pragma warning disable SA1314 // Type parameter names should begin with T | ||
public static Task InvokeAsync<S, R>(this TypedEventHandler<S, R> eventHandler, S sender, R eventArgs, CancellationToken cancellationToken) | ||
#pragma warning restore SA1314 // Type parameter names should begin with T | ||
#pragma warning restore CA1715 // Identifiers should have correct prefix | ||
where R : DeferredEventArgs | ||
{ | ||
if (eventHandler == null) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
var tasks = eventHandler.GetInvocationList() | ||
.OfType<TypedEventHandler<S, R>>() | ||
.Select(invocationDelegate => | ||
{ | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
|
||
invocationDelegate(sender, eventArgs); | ||
|
||
#pragma warning disable CS0618 // Type or member is obsolete | ||
var deferral = eventArgs.GetCurrentDeferralAndReset(); | ||
|
||
return deferral?.WaitForCompletion(cancellationToken) ?? Task.CompletedTask; | ||
#pragma warning restore CS0618 // Type or member is obsolete | ||
}) | ||
.ToArray(); | ||
|
||
return Task.WhenAll(tasks); | ||
} | ||
} |
6eab9df
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@check-spelling-bot Report
🔴 Please review
See the 📜action log or 📝 job summary for details.
Unrecognized words (2)
TResult
TSender
These words are not needed and should be removed
AVery mtu rsdkTo accept these unrecognized words as correct and remove the previously acknowledged and now absent words, you could run the following commands
... in a clone of the [email protected]:zadjii-msft/PowerToys.git repository
on the
dev/migrie/f/pilfer-async-stream-icons
branch (ℹ️ how do I use this?):Errors (1)
See the 📜action log or 📝 job summary for details.
See ❌ Event descriptions for more information.
If the flagged items are 🤯 false positives
If items relate to a ...
binary file (or some other file you wouldn't want to check at all).
Please add a file path to the
excludes.txt
file matching the containing file.File paths are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your files.
^
refers to the file's path from the root of the repository, so^README\.md$
would exclude README.md (on whichever branch you're using).well-formed pattern.
If you can write a pattern that would match it,
try adding it to the
patterns.txt
file.Patterns are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your lines.
Note that patterns can't match multiline strings.