Add PedroLamas/DeferredEvents Library

Plus some documentation headers and extra helpers for UWP specific types (thus in Microsoft.Toolkit.Uwp vs. Microsoft.Toolkit)
Add Fix for https://github.com/PedroLamas/DeferredEvents/issues/1
Add Cancel based EventArgs too.
This commit is contained in:
michael-hawker 2019-10-31 08:52:29 -07:00
Родитель c2949114aa
Коммит 13a9d7d1af
6 изменённых файлов: 278 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,17 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace Microsoft.Toolkit.Uwp.Deferred
{
/// <summary>
/// <see cref="DeferredEventArgs"/> which can also be Cancelled.
/// </summary>
public class DeferredCancelEventArgs : DeferredEventArgs
{
/// <summary>
/// Gets or sets a value indicating whether the event should be cancelled.
/// </summary>
public bool Cancel { get; set; }
}
}

Просмотреть файл

@ -0,0 +1,47 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
namespace Microsoft.Toolkit.Uwp.Deferred
{
/// <summary>
/// <see cref="EventArgs"/> which can retrieve a <see cref="EventDeferral"/> in order to process data asynchronously before an <see cref="EventHandler"/> completes and returns to the calling control.
/// </summary>
public class DeferredEventArgs : EventArgs
{
/// <summary>
/// Gets a new <see cref="DeferredEventArgs"/> to use in cases where no <see cref="EventArgs"/> wish to be provided.
/// </summary>
public static new DeferredEventArgs Empty => new DeferredEventArgs();
private readonly object _eventDeferralLock = new object();
private EventDeferral _eventDeferral;
/// <summary>
/// Returns an <see cref="EventDeferral"/> which can be completed when deferred event is ready to continue.
/// </summary>
/// <returns><see cref="EventDeferral"/> instance.</returns>
public EventDeferral GetDeferral()
{
lock (_eventDeferralLock)
{
return _eventDeferral ?? (_eventDeferral = new EventDeferral());
}
}
internal EventDeferral GetCurrentDeferralAndReset()
{
lock (_eventDeferralLock)
{
var eventDeferral = _eventDeferral;
_eventDeferral = null;
return eventDeferral;
}
}
}
}

Просмотреть файл

@ -0,0 +1,43 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.Toolkit.Uwp.Deferred
{
/// <summary>
/// Deferral handle provided by a <see cref="DeferredEventArgs"/>.
/// </summary>
public class EventDeferral : IDisposable
{
private readonly TaskCompletionSource<object> _taskCompletionSource = new TaskCompletionSource<object>();
internal EventDeferral()
{
}
/// <summary>
/// Call when finished with the Deferral.
/// </summary>
public void Complete() => _taskCompletionSource.TrySetResult(null);
internal async Task WaitForCompletion(CancellationToken cancellationToken)
{
using (cancellationToken.Register(() => _taskCompletionSource.TrySetCanceled()))
{
await _taskCompletionSource.Task;
}
}
/// <inheritdoc/>
public void Dispose()
{
Complete();
}
}
}

Просмотреть файл

@ -0,0 +1,69 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.Toolkit.Uwp.Deferred
{
/// <summary>
/// Extensions to <see cref="EventHandler{TEventArgs}"/> for Deferred Events.
/// </summary>
public static class EventHandlerExtensions
{
private static readonly Task CompletedTask = Task.FromResult(0);
/// <summary>
/// Use to invoke an async <see cref="EventHandler{TEventArgs}"/> using <see cref="DeferredEventArgs"/>.
/// </summary>
/// <typeparam name="T"><see cref="EventArgs"/> type.</typeparam>
/// <param name="eventHandler"><see cref="EventHandler{TEventArgs}"/> 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>
public static Task InvokeAsync<T>(this EventHandler<T> eventHandler, object sender, T eventArgs)
where T : DeferredEventArgs
{
return InvokeAsync(eventHandler, sender, eventArgs, CancellationToken.None);
}
/// <summary>
/// Use to invoke an async <see cref="EventHandler{TEventArgs}"/> using <see cref="DeferredEventArgs"/> with a <see cref="CancellationToken"/>.
/// </summary>
/// <typeparam name="T"><see cref="EventArgs"/> type.</typeparam>
/// <param name="eventHandler"><see cref="EventHandler{TEventArgs}"/> 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>
public static Task InvokeAsync<T>(this EventHandler<T> eventHandler, object sender, T eventArgs, CancellationToken cancellationToken)
where T : DeferredEventArgs
{
if (eventHandler == null)
{
return CompletedTask;
}
var tasks = eventHandler.GetInvocationList()
.OfType<EventHandler<T>>()
.Select(invocationDelegate =>
{
cancellationToken.ThrowIfCancellationRequested();
invocationDelegate(sender, eventArgs);
var deferral = eventArgs.GetCurrentDeferralAndReset();
return deferral?.WaitForCompletion(cancellationToken) ?? CompletedTask;
})
.ToArray();
return Task.WhenAll(tasks);
}
}
}

Просмотреть файл

@ -0,0 +1,70 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Windows.Foundation;
namespace Microsoft.Toolkit.Uwp.Deferred
{
/// <summary>
/// Extensions to <see cref="TypedEventHandler{TSender, TResult}"/> for Deferred Events.
/// </summary>
public static class TypedEventHandlerExtensions
{
private static readonly Task CompletedTask = Task.FromResult(0);
/// <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>
public static Task InvokeAsync<S, R>(this TypedEventHandler<S, R> eventHandler, S sender, R eventArgs)
where R : DeferredEventArgs
{
return 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>
public static Task InvokeAsync<S, R>(this TypedEventHandler<S, R> eventHandler, S sender, R eventArgs, CancellationToken cancellationToken)
where R : DeferredEventArgs
{
if (eventHandler == null)
{
return CompletedTask;
}
var tasks = eventHandler.GetInvocationList()
.OfType<TypedEventHandler<S, R>>()
.Select(invocationDelegate =>
{
cancellationToken.ThrowIfCancellationRequested();
invocationDelegate(sender, eventArgs);
var deferral = eventArgs.GetCurrentDeferralAndReset();
return deferral?.WaitForCompletion(cancellationToken) ?? CompletedTask;
})
.ToArray();
return Task.WhenAll(tasks);
}
}
}

32
ThirdPartyNotices.txt Normal file
Просмотреть файл

@ -0,0 +1,32 @@
Windows-Community-Toolkit
THIRD-PARTY SOFTWARE NOTICES AND INFORMATION
Do Not Translate or Localize
This project incorporates components from the projects listed below. The original copyright notices and the licenses under which the .NET Foundation received such components are set forth below. The .NET Foundation reserves all rights not expressly granted herein, whether by implication, estoppel or otherwise.
1. PedroLamas/DeferredEvents version 1.0.4 (https://github.com/PedroLamas/DeferredEvents)
%% PedroLamas/DeferredEvents NOTICES AND INFORMATION BEGIN HERE
=========================================
Copyright (C) 2017 Pedro Lamas, http://www.pedrolamas.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
=========================================
END OF PedroLamas/DeferredEvents NOTICES AND INFORMATION