Skip to content

Commit 20d199f

Browse files
Adopt the async-only message broker (#663) (#665)
Requires Radical 3.0.0-beta.2. Lifecycle and application notifications use a fire-and-forget BroadcastAsync, subscriptions and the auto-subscribed handlers return a Task, and ShutdownAsync awaits the ApplicationShutdownRequested subscribers before reading Cancel. Shutdown is obsolete with an error, to be removed in v5.0.0. (cherry picked from commit 5e16c22)
1 parent 1e1d016 commit 20d199f

12 files changed

Lines changed: 138 additions & 48 deletions

‎src/Radical.Windows.Tests/API/APIApprovals.Approve_API.approved.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,7 +1942,11 @@ namespace Radical.Windows
19421942
public sealed class RadicalApplication
19431943
{
19441944
public void Boot() { }
1945+
[System.Obsolete("Shutdown has been obsoleted and will be removed in v5.0.0. Use ShutdownAsync inst" +
1946+
"ead: the message broker is async-only, and shutting down awaits the ApplicationS" +
1947+
"hutdownRequested subscribers.", true)]
19451948
public void Shutdown() { }
1949+
public System.Threading.Tasks.Task ShutdownAsync() { }
19461950
}
19471951
public static class VisualTreeCrawler
19481952
{
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using Radical.ComponentModel.Messaging;
4+
using Radical.Windows.Messaging;
5+
using System.Threading.Tasks;
6+
using System.Windows;
7+
8+
namespace Radical.Windows.Tests.Boot
9+
{
10+
[TestClass]
11+
public class RadicalApplicationShutdownTests
12+
{
13+
/*
14+
* Only canceled shutdowns are tested: a completed shutdown
15+
* would shut down the application shared by all the tests.
16+
*/
17+
[SharedApplicationTestMethod]
18+
public async Task ShutdownAsync_should_be_canceled_by_an_async_ApplicationShutdownRequested_subscriber()
19+
{
20+
var shuttingDown = false;
21+
var subscriberCompleted = false;
22+
23+
var radicalApplication = Application.Current.AddRadicalApplication(configuration =>
24+
{
25+
configuration.DisableAutoBoot();
26+
configuration.OnBootCompleted(serviceProvider =>
27+
{
28+
var broker = serviceProvider.GetRequiredService<IMessageBroker>();
29+
broker.Subscribe<ApplicationShutdownRequested>(serviceProvider, async (s, m) =>
30+
{
31+
await Task.Yield();
32+
m.Cancel = true;
33+
subscriberCompleted = true;
34+
});
35+
});
36+
configuration.OnShuttingDown(_ => shuttingDown = true);
37+
});
38+
39+
radicalApplication.Boot();
40+
41+
await radicalApplication.ShutdownAsync();
42+
43+
Assert.IsTrue(subscriberCompleted);
44+
Assert.IsFalse(shuttingDown);
45+
}
46+
}
47+
}

‎src/Radical.Windows.Tests/Radical.Windows.Tests.csproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.0" />
2525
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.0" />
2626
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.135" />
27-
<PackageReference Include="Radical" Version="3.0.0-beta.1" />
27+
<PackageReference Include="Radical" Version="3.0.0-beta.2" />
2828
<PackageReference Include="Unity.Microsoft.DependencyInjection" Version="5.11.5" />
2929
</ItemGroup>
3030

‎src/Radical.Windows/ApplicationBootstrapper.cs‎

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ public ApplicationBootstrapper()
103103
if (!isShuttingDown)
104104
{
105105
var reason = isSessionEnding ? ApplicationShutdownReason.SessionEnding : ApplicationShutdownReason.ApplicationRequest;
106-
OnShutdownCore(reason);
106+
// Not a user request: completes synchronously.
107+
_ = OnShutdownCoreAsync(reason);
107108
}
108109
};
109110
}
@@ -237,7 +238,7 @@ void OnBoot()
237238
ExposeRegisteredAppResources();
238239

239240
var broker = serviceProvider.TryGetService<IMessageBroker>();
240-
broker?.Broadcast(this, new ApplicationBootCompleted());
241+
_ = broker?.BroadcastAsync(this, new ApplicationBootCompleted());
241242

242243
bootCompletedHandler?.Invoke(serviceProvider);
243244

@@ -378,10 +379,7 @@ public ApplicationBootstrapper OnSingletonApplicationStartup(Action<SingletonApp
378379
void OnBoot(IServiceProvider serviceProvider)
379380
{
380381
var broker = serviceProvider.GetService<IMessageBroker>();
381-
broker.Subscribe<ApplicationShutdownRequest>(this, InvocationModel.Safe, (s, m) =>
382-
{
383-
OnShutdownCore(ApplicationShutdownReason.UserRequest);
384-
});
382+
broker.Subscribe<ApplicationShutdownRequest>(this, InvocationModel.Safe, (s, m) => OnShutdownCoreAsync(ApplicationShutdownReason.UserRequest));
385383

386384
var args = new SingletonApplicationStartupArgs(singleton, key);
387385
HandleSingletonApplicationStartup(args);
@@ -392,7 +390,8 @@ void OnBoot(IServiceProvider serviceProvider)
392390
}
393391
else
394392
{
395-
OnShutdownCore(ApplicationShutdownReason.MultipleInstanceNotAllowed);
393+
// Not a user request: completes synchronously.
394+
_ = OnShutdownCoreAsync(ApplicationShutdownReason.MultipleInstanceNotAllowed);
396395
}
397396
}
398397

@@ -412,7 +411,7 @@ public void Boot()
412411
/// </summary>
413412
public void Shutdown()
414413
{
415-
OnShutdownCore(ApplicationShutdownReason.UserRequest);
414+
_ = OnShutdownCoreAsync(ApplicationShutdownReason.UserRequest);
416415
}
417416

418417
void OnBootCompleted(IServiceProvider serviceProvider)
@@ -510,9 +509,12 @@ await Task.Delay(remaining)
510509
}
511510
}
512511

513-
void OnShutdownCore(ApplicationShutdownReason reason)
512+
// Only a user request awaits, letting ApplicationShutdownRequested subscribers
513+
// complete asynchronously; any other reason completes synchronously, as
514+
// required when shutting down from the platform's synchronous events.
515+
async Task OnShutdownCoreAsync(ApplicationShutdownReason reason)
514516
{
515-
var canceled = false;
517+
var skipShutdown = false;
516518

517519
try
518520
{
@@ -521,13 +523,20 @@ void OnShutdownCore(ApplicationShutdownReason reason)
521523
var msg = new ApplicationShutdownRequested(reason);
522524

523525
var broker = serviceProvider.GetService<IMessageBroker>();
524-
broker.Dispatch(this, msg);
526+
await broker.DispatchAsync(this, msg);
527+
528+
if (isShuttingDown)
529+
{
530+
// the application shut down, e.g. Exit was raised, while awaiting the subscribers
531+
skipShutdown = true;
532+
return;
533+
}
525534

526-
canceled = msg.Cancel;
535+
skipShutdown = msg.Cancel;
527536

528-
if (canceled)
537+
if (skipShutdown)
529538
{
530-
broker.Broadcast(this, new ApplicationShutdownCanceled(reason));
539+
_ = broker.BroadcastAsync(this, new ApplicationShutdownCanceled(reason));
531540
return;
532541
}
533542
}
@@ -536,9 +545,9 @@ void OnShutdownCore(ApplicationShutdownReason reason)
536545

537546
if (isBootCompleted)
538547
{
539-
serviceProvider
548+
_ = serviceProvider
540549
.GetService<IMessageBroker>()
541-
.Broadcast(this, new ApplicationShutdown(reason));
550+
.BroadcastAsync(this, new ApplicationShutdown(reason));
542551

543552
var callbacks = serviceProvider.GetServices<IExpectShutdownCallback>();
544553
if (callbacks != null && callbacks.Any())
@@ -569,12 +578,12 @@ void OnShutdownCore(ApplicationShutdownReason reason)
569578
}
570579
finally
571580
{
572-
if (!canceled && reason != ApplicationShutdownReason.ApplicationRequest)
581+
if (!skipShutdown && reason != ApplicationShutdownReason.ApplicationRequest)
573582
{
574583
Application.Current.Shutdown();
575584
}
576585

577-
if (!canceled)
586+
if (!skipShutdown)
578587
{
579588
serviceProvider = null;
580589

‎src/Radical.Windows/Behaviors/DependencyObjectCloseHandlerBehavior.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Radical.Windows.ComponentModel;
44
using Radical.Windows.Messaging;
55
using System.Windows;
6+
using System.Threading.Tasks;
67

78
namespace Radical.Windows.Behaviors
89
{
@@ -48,6 +49,8 @@ protected override void OnAttached()
4849
w.Close();
4950
}
5051
}
52+
53+
return Task.CompletedTask;
5154
} );
5255
}
5356

‎src/Radical.Windows/Behaviors/FrameworkElementLifecycleNotificationsBehavior.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ public FrameworkElementLifecycleNotificationsBehavior( IMessageBroker broker, IC
3333

3434
if ( this.conventions.ShouldNotifyViewModelLoaded( view, dc ) )
3535
{
36-
this.broker.Broadcast( this, new ViewModelLoaded( dc ) );
36+
_ = this.broker.BroadcastAsync( this, new ViewModelLoaded( dc ) );
3737
}
3838

3939
if ( this.conventions.ShouldNotifyViewLoaded( view ) )
4040
{
41-
this.broker.Broadcast( this, new ViewLoaded( view ) );
41+
_ = this.broker.BroadcastAsync( this, new ViewLoaded( view ) );
4242
}
4343

4444
if (dc is IExpectViewLoadedCallback temp)

‎src/Radical.Windows/Behaviors/WindowLifecycleNotificationsBehavior.cs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public WindowLifecycleNotificationsBehavior( IMessageBroker broker, IConventions
5656
{
5757
logger.Debug( "ShouldNotifyViewModelLoaded -> true." );
5858

59-
this.broker.Broadcast( this, new ViewModelLoaded( dc ) );
59+
_ = this.broker.BroadcastAsync( this, new ViewModelLoaded( dc ) );
6060

6161
logger.Debug( "Message broadcasted." );
6262
}
@@ -65,7 +65,7 @@ public WindowLifecycleNotificationsBehavior( IMessageBroker broker, IConventions
6565
{
6666
logger.Debug( "ShouldNotifyViewLoaded -> true." );
6767

68-
this.broker.Broadcast( this, new ViewLoaded( view ));
68+
_ = this.broker.BroadcastAsync( this, new ViewLoaded( view ));
6969

7070
logger.Debug( "Message broadcasted." );
7171
}
@@ -89,7 +89,7 @@ public WindowLifecycleNotificationsBehavior( IMessageBroker broker, IConventions
8989

9090
if ( dc != null && dc.GetType().IsAttributeDefined<NotifyActivatedAttribute>() )
9191
{
92-
this.broker.Broadcast( this, new ViewModelActivated( dc ) );
92+
_ = this.broker.BroadcastAsync( this, new ViewModelActivated( dc ) );
9393
}
9494

9595
if (dc is IExpectViewActivatedCallback temp)
@@ -113,7 +113,7 @@ public WindowLifecycleNotificationsBehavior( IMessageBroker broker, IConventions
113113

114114
if( dc != null && dc.GetType().IsAttributeDefined<NotifyShownAttribute>() )
115115
{
116-
this.broker.Broadcast( this, new ViewModelShown( dc ) );
116+
_ = this.broker.BroadcastAsync( this, new ViewModelShown( dc ) );
117117
}
118118

119119
if (dc is IExpectViewShownCallback temp)
@@ -138,7 +138,7 @@ public WindowLifecycleNotificationsBehavior( IMessageBroker broker, IConventions
138138

139139
if ( dc != null && dc.GetType().IsAttributeDefined<NotifyClosedAttribute>() )
140140
{
141-
this.broker.Broadcast( this, new ViewModelClosed( dc ) );
141+
_ = this.broker.BroadcastAsync( this, new ViewModelClosed( dc ) );
142142
}
143143

144144
if (dc is IExpectViewClosedCallback temp)

‎src/Radical.Windows/Bootstrap/Features/AutoSubscribe.cs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Linq;
8+
using System.Threading.Tasks;
89

910
namespace Radical.Windows.Bootstrap.Features
1011
{
@@ -48,8 +49,10 @@ public void Subscribe(IMessageBroker broker, IServiceProvider serviceProvider)
4849

4950
if (handler.ShouldHandle(s, msg))
5051
{
51-
handler.Handle(s, msg);
52+
return handler.HandleAsync(s, msg);
5253
}
54+
55+
return Task.CompletedTask;
5356
});
5457
});
5558
}

‎src/Radical.Windows/Hosting/RadicalApplicationService.cs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ public Task StartAsync(CancellationToken cancellationToken)
3131

3232
public Task StopAsync(CancellationToken cancellationToken)
3333
{
34-
radicalApplication.Shutdown();
35-
return Task.CompletedTask;
34+
return radicalApplication.ShutdownAsync();
3635
}
3736
}
3837
}

‎src/Radical.Windows/Messaging/Handlers/ViewLoadedHandler.cs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Radical.ComponentModel.Messaging;
22
using Radical.Messaging;
33
using Radical.Windows.ComponentModel;
4+
using System.Threading.Tasks;
45

56
namespace Radical.Windows.Messaging.Handlers
67
{
@@ -17,7 +18,7 @@ public ViewLoadedHandler(IViewResolver viewProvider, IRegionService regionServic
1718
this.autoMappingHandler = autoMappingHandler;
1819
}
1920

20-
public override void Handle(object sender, ViewLoaded message )
21+
public override Task HandleAsync(object sender, ViewLoaded message )
2122
{
2223
var view = message.View;
2324
if ( regionService.HoldsRegionManager( view ) )
@@ -37,6 +38,8 @@ public override void Handle(object sender, ViewLoaded message )
3738
}
3839
}
3940
}
41+
42+
return Task.CompletedTask;
4043
}
4144
}
4245
}

0 commit comments

Comments
 (0)