diff options
author | Brian Lagunas <brianlagunas@users.noreply.github.com> | 2015-09-09 21:23:37 +0300 |
---|---|---|
committer | Brian Lagunas <brianlagunas@users.noreply.github.com> | 2015-09-09 21:23:37 +0300 |
commit | 1cdfb35b4450695e2a5926d9eeb90ecf58f33c66 (patch) | |
tree | 076eba6a1f0eaab9372e45f31291828ac7c9abf1 | |
parent | 75ce6017abe5473afee147127e97a51caf5cc0c2 (diff) | |
parent | c1c527e85e1551162249810e6ca9e61ad523dc60 (diff) |
Merge pull request #123 from frogger3d/feature/async-interaction-request
Add an async version of InteractionRequest<T>.Raise
4 files changed, 54 insertions, 0 deletions
diff --git a/Source/Wpf/Prism.Wpf.Tests/Interactivity/InteractionRequestTriggerFixture.cs b/Source/Wpf/Prism.Wpf.Tests/Interactivity/InteractionRequestTriggerFixture.cs index c9fcb79..5043de5 100644 --- a/Source/Wpf/Prism.Wpf.Tests/Interactivity/InteractionRequestTriggerFixture.cs +++ b/Source/Wpf/Prism.Wpf.Tests/Interactivity/InteractionRequestTriggerFixture.cs @@ -5,6 +5,7 @@ using System.Windows; using Microsoft.VisualStudio.TestTools.UnitTesting; using Prism.Interactivity.InteractionRequest; using Prism.Wpf.Tests.Mocks; +using System.Threading.Tasks; namespace Prism.Wpf.Tests.Interactivity { @@ -46,6 +47,26 @@ namespace Prism.Wpf.Tests.Interactivity request.Raise(new Notification()); Assert.IsTrue(action.ExecutionCount == 1); } + + [TestMethod] + public async Task WhenEventIsRaisedAsync_NotificationIsPassedBackAsync() + { + var request = new InteractionRequest<INotification>(); + var trigger = new TestableInteractionRequestTrigger(); + var associatedObject = new DependencyObject(); + var action = new TestableTriggerAction(); + var notification = new Notification(); + + trigger.Actions.Add(action); + trigger.Attach(associatedObject); + trigger.SourceObject = request; + + Assert.IsTrue(action.ExecutionCount == 0); + + var result = await request.RaiseAsync(notification); + Assert.IsTrue(action.ExecutionCount == 1); + Assert.ReferenceEquals(notification, result); + } } public class TestableInteractionRequestTrigger : InteractionRequestTrigger @@ -66,6 +87,10 @@ namespace Prism.Wpf.Tests.Interactivity protected override void Invoke(object parameter) { this.ExecutionCount++; + if (parameter is InteractionRequestedEventArgs) + { + ((InteractionRequestedEventArgs)parameter).Callback(); + } } } } diff --git a/Source/Wpf/Prism.Wpf/Common/CallbackHelper.cs b/Source/Wpf/Prism.Wpf/Common/CallbackHelper.cs new file mode 100644 index 0000000..28cdaa0 --- /dev/null +++ b/Source/Wpf/Prism.Wpf/Common/CallbackHelper.cs @@ -0,0 +1,15 @@ +using System; +using System.Threading.Tasks; + +namespace Prism.Common +{ + internal static class CallbackHelper + { + public static async Task<TResult> AwaitCallbackResult<TResult>(Action<Action<TResult>> f) + { + var tcs = new TaskCompletionSource<TResult>(); + f(n => tcs.SetResult(n)); + return await tcs.Task; + } + } +}
\ No newline at end of file diff --git a/Source/Wpf/Prism.Wpf/Interactivity/InteractionRequest/InteractionRequest.cs b/Source/Wpf/Prism.Wpf/Interactivity/InteractionRequest/InteractionRequest.cs index ce42e0c..9138dd1 100644 --- a/Source/Wpf/Prism.Wpf/Interactivity/InteractionRequest/InteractionRequest.cs +++ b/Source/Wpf/Prism.Wpf/Interactivity/InteractionRequest/InteractionRequest.cs @@ -1,6 +1,8 @@ using System; +using System.Threading.Tasks; +using Prism.Common; namespace Prism.Interactivity.InteractionRequest { @@ -39,5 +41,16 @@ namespace Prism.Interactivity.InteractionRequest handler(this, new InteractionRequestedEventArgs(context, () => { if(callback != null) callback(context); } )); } } + + /// <summary> + /// Fires the Raised event asynchronously. Please note that this request may never return + /// if the InteractionRequest is unhandled. + /// </summary> + /// <param name="context">The context for the interaction request.</param> + /// <returns>The context after the request has been handled by the UI.</returns> + public async Task<T> RaiseAsync(T context) + { + return await CallbackHelper.AwaitCallbackResult<T>(callback => this.Raise(context, callback)); + } } } diff --git a/Source/Wpf/Prism.Wpf/Prism.Wpf.csproj b/Source/Wpf/Prism.Wpf/Prism.Wpf.csproj index a51ec4f..8157b11 100644 --- a/Source/Wpf/Prism.Wpf/Prism.Wpf.csproj +++ b/Source/Wpf/Prism.Wpf/Prism.Wpf.csproj @@ -54,6 +54,7 @@ </ItemGroup> <ItemGroup> <Compile Include="Bootstrapper.cs" /> + <Compile Include="Common\CallbackHelper.cs" /> <Compile Include="Common\MvvmHelpers.cs" /> <Compile Include="Extensions\CollectionExtensions.cs" /> <Compile Include="Events\WeakDelegatesManager.cs" /> |