Skip to content

Commit b3b4e18

Browse files
committed
Limit the added APIs by removing event cancellation.
1 parent a5ca308 commit b3b4e18

File tree

5 files changed

+13
-233
lines changed

5 files changed

+13
-233
lines changed

src/Components/Components/src/NavigationManager.cs

Lines changed: 0 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ public event EventHandler<EventArgs> NotFoundEvent
5454

5555
private EventHandler<EventArgs>? _notFound;
5656

57-
private readonly List<Func<NotFoundContext, ValueTask>> _notFoundHandlers = new();
58-
59-
private CancellationTokenSource? _notFoundCts;
60-
6157
// For the baseUri it's worth storing as a System.Uri so we can do operations
6258
// on that type. System.Uri gives us access to the original string anyway.
6359
private Uri? _baseUri;
@@ -487,126 +483,6 @@ protected async ValueTask<bool> NotifyLocationChangingAsync(string uri, string?
487483
}
488484
}
489485

490-
/// <summary>
491-
/// Notifies the registered handlers of the current ot found event.
492-
/// </summary>
493-
/// <returns>A <see cref="ValueTask{TResult}"/> representing the completion of the operation. If the result is <see langword="true"/>, the navigation should continue.</returns>
494-
protected async ValueTask<bool> NotifyNotFoundAsync()
495-
{
496-
_notFoundCts?.Cancel();
497-
_notFoundCts = null;
498-
499-
var handlerCount = _notFoundHandlers.Count;
500-
501-
if (handlerCount == 0)
502-
{
503-
return true;
504-
}
505-
506-
var cts = new CancellationTokenSource();
507-
508-
_notFoundCts = cts;
509-
510-
var cancellationToken = cts.Token;
511-
var context = new NotFoundContext
512-
{
513-
CancellationToken = cancellationToken,
514-
};
515-
516-
try
517-
{
518-
if (handlerCount == 1)
519-
{
520-
var handlerTask = InvokeNotFoundHandlerAsync(_notFoundHandlers[0], context);
521-
522-
if (handlerTask.IsFaulted)
523-
{
524-
await handlerTask;
525-
return false; // Unreachable because the previous line will throw.
526-
}
527-
528-
if (context.DidPreventRendering)
529-
{
530-
return false;
531-
}
532-
533-
if (!handlerTask.IsCompletedSuccessfully)
534-
{
535-
await handlerTask.AsTask().WaitAsync(cancellationToken);
536-
}
537-
}
538-
else
539-
{
540-
var notFoundHandlersCopy = ArrayPool<Func<NotFoundContext, ValueTask>>.Shared.Rent(handlerCount);
541-
542-
try
543-
{
544-
_notFoundHandlers.CopyTo(notFoundHandlersCopy);
545-
546-
var notFoundTasks = new HashSet<Task>();
547-
548-
for (var i = 0; i < handlerCount; i++)
549-
{
550-
var handlerTask = InvokeNotFoundHandlerAsync(notFoundHandlersCopy[i], context);
551-
552-
if (handlerTask.IsFaulted)
553-
{
554-
await handlerTask;
555-
return false; // Unreachable because the previous line will throw.
556-
}
557-
558-
if (context.DidPreventRendering)
559-
{
560-
return false;
561-
}
562-
563-
notFoundTasks.Add(handlerTask.AsTask());
564-
}
565-
566-
while (notFoundTasks.Count != 0)
567-
{
568-
var completedHandlerTask = await Task.WhenAny(notFoundTasks).WaitAsync(cancellationToken);
569-
570-
if (completedHandlerTask.IsFaulted)
571-
{
572-
await completedHandlerTask;
573-
return false; // Unreachable because the previous line will throw.
574-
}
575-
576-
notFoundTasks.Remove(completedHandlerTask);
577-
}
578-
}
579-
finally
580-
{
581-
ArrayPool<Func<NotFoundContext, ValueTask>>.Shared.Return(notFoundHandlersCopy);
582-
}
583-
}
584-
585-
return !context.DidPreventRendering;
586-
}
587-
catch (TaskCanceledException ex)
588-
{
589-
if (ex.CancellationToken == cancellationToken)
590-
{
591-
// This navigation was in progress when a successive navigation occurred.
592-
// We treat this as a canceled navigation.
593-
return false;
594-
}
595-
596-
throw;
597-
}
598-
finally
599-
{
600-
cts.Cancel();
601-
cts.Dispose();
602-
603-
if (_notFoundCts == cts)
604-
{
605-
_notFoundCts = null;
606-
}
607-
}
608-
}
609-
610486
private async ValueTask InvokeLocationChangingHandlerAsync(Func<LocationChangingContext, ValueTask> handler, LocationChangingContext context)
611487
{
612488
try
@@ -623,22 +499,6 @@ private async ValueTask InvokeLocationChangingHandlerAsync(Func<LocationChanging
623499
}
624500
}
625501

626-
private async ValueTask InvokeNotFoundHandlerAsync(Func<NotFoundContext, ValueTask> handler, NotFoundContext context)
627-
{
628-
try
629-
{
630-
await handler(context);
631-
}
632-
catch (OperationCanceledException)
633-
{
634-
// Ignore exceptions caused by cancellations.
635-
}
636-
catch (Exception ex)
637-
{
638-
HandleNotFoundHandlerException(ex, context);
639-
}
640-
}
641-
642502
/// <summary>
643503
/// Handles exceptions thrown in location changing handlers.
644504
/// </summary>
@@ -647,14 +507,6 @@ private async ValueTask InvokeNotFoundHandlerAsync(Func<NotFoundContext, ValueTa
647507
protected virtual void HandleLocationChangingHandlerException(Exception ex, LocationChangingContext context)
648508
=> throw new InvalidOperationException($"To support navigation locks, {GetType().Name} must override {nameof(HandleLocationChangingHandlerException)}");
649509

650-
/// <summary>
651-
/// Handles exceptions thrown in NotFound rendering handlers.
652-
/// </summary>
653-
/// <param name="ex">The exception to handle.</param>
654-
/// <param name="context">The context passed to the handler.</param>
655-
protected virtual void HandleNotFoundHandlerException(Exception ex, NotFoundContext context)
656-
=> throw new InvalidOperationException($"To support not found rendering locks, {GetType().Name} must override {nameof(HandleNotFoundHandlerException)}");
657-
658510
/// <summary>
659511
/// Sets whether navigation is currently locked. If it is, then implementations should not update <see cref="Uri"/> and call
660512
/// <see cref="NotifyLocationChanged(bool)"/> until they have first confirmed the navigation by calling

src/Components/Components/src/PublicAPI.Unshipped.txt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,8 @@ Microsoft.AspNetCore.Components.IEndpointHtmlRenderer
33
Microsoft.AspNetCore.Components.IEndpointHtmlRenderer.SetNotFoundResponse() -> void
44
Microsoft.AspNetCore.Components.NavigationManager.NotFoundEvent -> System.EventHandler<System.EventArgs!>!
55
Microsoft.AspNetCore.Components.NavigationManager.NotifyNotFound() -> void
6-
Microsoft.AspNetCore.Components.NavigationManager.NotifyNotFoundAsync() -> System.Threading.Tasks.ValueTask<bool>
76
Microsoft.AspNetCore.Components.NotFoundRenderingException
87
Microsoft.AspNetCore.Components.NotFoundRenderingException.NotFoundRenderingException(string! message, System.Exception! innerException) -> void
98
Microsoft.AspNetCore.Components.Routing.IHostEnvironmentNavigationManager.Initialize(string! baseUri, string! uri, Microsoft.AspNetCore.Components.IEndpointHtmlRenderer! renderer) -> void
10-
Microsoft.AspNetCore.Components.Routing.NotFoundContext
11-
Microsoft.AspNetCore.Components.Routing.NotFoundContext.CancellationToken.get -> System.Threading.CancellationToken
12-
Microsoft.AspNetCore.Components.Routing.NotFoundContext.CancellationToken.init -> void
13-
Microsoft.AspNetCore.Components.Routing.NotFoundContext.NotFoundContext() -> void
14-
Microsoft.AspNetCore.Components.Routing.NotFoundContext.PreventRendering() -> void
15-
virtual Microsoft.AspNetCore.Components.NavigationManager.HandleNotFoundHandlerException(System.Exception! ex, Microsoft.AspNetCore.Components.Routing.NotFoundContext! context) -> void
169
virtual Microsoft.AspNetCore.Components.NavigationManager.NotFound() -> void
1710
virtual Microsoft.AspNetCore.Components.NavigationManager.NotFoundCore() -> void

src/Components/Components/src/Routing/NotFoundContext.cs

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/Components/Server/src/Circuits/RemoteNavigationManager.cs

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -165,26 +165,14 @@ protected override void NotFoundCore()
165165
{
166166
Log.RequestingNotFound(_logger);
167167

168-
_ = NotFoundAsync();
169-
170-
async Task NotFoundAsync()
168+
try
171169
{
172-
try
173-
{
174-
bool shouldContinueRendering = await NotifyNotFoundAsync();
175-
if (!shouldContinueRendering)
176-
{
177-
Log.NotFoundRenderCanceled(_logger);
178-
return;
179-
}
180-
181-
NotifyNotFound();
182-
}
183-
catch (Exception ex)
184-
{
185-
Log.NotFoundRenderFailed(_logger, ex);
186-
UnhandledException?.Invoke(this, ex);
187-
}
170+
NotifyNotFound();
171+
}
172+
catch (Exception ex)
173+
{
174+
Log.NotFoundRenderFailed(_logger, ex);
175+
UnhandledException?.Invoke(this, ex);
188176
}
189177
}
190178

@@ -248,8 +236,5 @@ public static void RequestingNavigation(ILogger logger, string uri, NavigationOp
248236

249237
[LoggerMessage(7, LogLevel.Debug, "Navigation stopped because the session ended when navigating to {Uri}", EventName = "NavigationStoppedSessionEnded")]
250238
public static partial void NavigationStoppedSessionEnded(ILogger logger, string uri);
251-
252-
[LoggerMessage(3, LogLevel.Debug, "Rendering of NotFound canceled", EventName = "NotFoundRenderCanceled")]
253-
public static partial void NotFoundRenderCanceled(ILogger logger);
254239
}
255240
}

src/Components/WebAssembly/WebAssembly/src/Services/WebAssemblyNavigationManager.cs

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -88,28 +88,13 @@ public override void Refresh(bool forceReload = false)
8888
/// <inheritdoc />
8989
protected override void NotFoundCore()
9090
{
91-
_ = PerformNotFoundAsync();
92-
93-
async Task PerformNotFoundAsync()
91+
try
9492
{
95-
try
96-
{
97-
var shouldContinueNotFound = await NotifyNotFoundAsync();
98-
99-
if (!shouldContinueNotFound)
100-
{
101-
Log.NotFoundRenderCanceled(_logger);
102-
return;
103-
}
104-
105-
NotifyNotFound();
106-
}
107-
catch (Exception ex)
108-
{
109-
// We shouldn't ever reach this since exceptions thrown from handlers are handled in HandleNotFoundHandlerException.
110-
// But if some other exception gets thrown, we still want to know about it.
111-
Log.NotFoundRenderFailed(_logger, ex);
112-
}
93+
NotifyNotFound();
94+
}
95+
catch (Exception ex)
96+
{
97+
Log.NotFoundRenderFailed(_logger, ex);
11398
}
11499
}
115100

@@ -118,11 +103,6 @@ protected override void HandleLocationChangingHandlerException(Exception ex, Loc
118103
Log.NavigationFailed(_logger, context.TargetLocation, ex);
119104
}
120105

121-
protected override void HandleNotFoundHandlerException(Exception ex, NotFoundContext context)
122-
{
123-
Log.NotFoundRenderFailed(_logger, ex);
124-
}
125-
126106
protected override void SetNavigationLockState(bool value)
127107
=> InternalJSImportMethods.Instance.NavigationManager_SetHasLocationChangingListeners((int)WebRendererId.WebAssembly, value);
128108

@@ -136,8 +116,5 @@ private static partial class Log
136116

137117
[LoggerMessage(5, LogLevel.Error, "Failed to render NotFound", EventName = "NotFoundRenderFailed")]
138118
public static partial void NotFoundRenderFailed(ILogger logger, Exception exception);
139-
140-
[LoggerMessage(1, LogLevel.Debug, "NotFound render canceled", EventName = "NotFoundRenderCanceled")]
141-
public static partial void NotFoundRenderCanceled(ILogger logger);
142119
}
143120
}

0 commit comments

Comments
 (0)