Remove IComponentContext
Fixes part of: #12553 We don't believe that this is needed anymore. We no longer call OnAfterRender when you're prerendering, so the main use case of this type is gone.
This commit is contained in:
parent
a285e966e3
commit
1f7d59d8f1
|
|
@ -91,7 +91,6 @@ namespace Microsoft.AspNetCore.Blazor.Hosting
|
|||
services.AddSingleton(_BrowserHostBuilderContext);
|
||||
services.AddSingleton<IWebAssemblyHost, WebAssemblyHost>();
|
||||
services.AddSingleton<IJSRuntime>(WebAssemblyJSRuntime.Instance);
|
||||
services.AddSingleton<IComponentContext, WebAssemblyComponentContext>();
|
||||
services.AddSingleton<NavigationManager>(WebAssemblyNavigationManager.Instance);
|
||||
services.AddSingleton<INavigationInterception>(WebAssemblyNavigationInterception.Instance);
|
||||
services.AddSingleton<ILoggerFactory, WebAssemblyLoggerFactory>();
|
||||
|
|
|
|||
|
|
@ -1,12 +0,0 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using Microsoft.AspNetCore.Components;
|
||||
|
||||
namespace Microsoft.AspNetCore.Blazor.Services
|
||||
{
|
||||
internal class WebAssemblyComponentContext : IComponentContext
|
||||
{
|
||||
public bool IsConnected => true;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,16 +0,0 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNetCore.Blazor.Services.Test
|
||||
{
|
||||
public class WebAssemblyComponentContextTest
|
||||
{
|
||||
[Fact]
|
||||
public void IsConnected()
|
||||
{
|
||||
Assert.True(new WebAssemblyComponentContext().IsConnected);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -196,10 +196,6 @@ namespace Microsoft.AspNetCore.Components
|
|||
void Attach(Microsoft.AspNetCore.Components.RenderHandle renderHandle);
|
||||
System.Threading.Tasks.Task SetParametersAsync(Microsoft.AspNetCore.Components.ParameterView parameters);
|
||||
}
|
||||
public partial interface IComponentContext
|
||||
{
|
||||
bool IsConnected { get; }
|
||||
}
|
||||
public partial interface IHandleAfterRender
|
||||
{
|
||||
System.Threading.Tasks.Task OnAfterRenderAsync();
|
||||
|
|
|
|||
|
|
@ -196,10 +196,6 @@ namespace Microsoft.AspNetCore.Components
|
|||
void Attach(Microsoft.AspNetCore.Components.RenderHandle renderHandle);
|
||||
System.Threading.Tasks.Task SetParametersAsync(Microsoft.AspNetCore.Components.ParameterView parameters);
|
||||
}
|
||||
public partial interface IComponentContext
|
||||
{
|
||||
bool IsConnected { get; }
|
||||
}
|
||||
public partial interface IHandleAfterRender
|
||||
{
|
||||
System.Threading.Tasks.Task OnAfterRenderAsync();
|
||||
|
|
|
|||
|
|
@ -1,19 +0,0 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
namespace Microsoft.AspNetCore.Components
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides information about the environment in which components are executing.
|
||||
/// </summary>
|
||||
public interface IComponentContext
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a flag to indicate whether there is an active connection to the user's display.
|
||||
/// </summary>
|
||||
/// <example>During prerendering, the value will always be false.</example>
|
||||
/// <example>During server-side execution, the value can be true or false depending on whether there is an active SignalR connection.</example>
|
||||
/// <example>During client-side execution, the value will always be true.</example>
|
||||
bool IsConnected { get; }
|
||||
}
|
||||
}
|
||||
|
|
@ -30,8 +30,6 @@ namespace Microsoft.AspNetCore.Components.Routing
|
|||
|
||||
[Inject] private INavigationInterception NavigationInterception { get; set; }
|
||||
|
||||
[Inject] private IComponentContext ComponentContext { get; set; }
|
||||
|
||||
[Inject] private ILoggerFactory LoggerFactory { get; set; }
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -152,7 +150,7 @@ namespace Microsoft.AspNetCore.Components.Routing
|
|||
|
||||
Task IHandleAfterRender.OnAfterRenderAsync()
|
||||
{
|
||||
if (!_navigationInterceptionEnabled && ComponentContext.IsConnected)
|
||||
if (!_navigationInterceptionEnabled)
|
||||
{
|
||||
_navigationInterceptionEnabled = true;
|
||||
return NavigationInterception.EnableNavigationInterceptionAsync();
|
||||
|
|
|
|||
|
|
@ -51,9 +51,7 @@ namespace Microsoft.AspNetCore.Components.Server.Circuits
|
|||
|
||||
var scope = _scopeFactory.CreateScope();
|
||||
var jsRuntime = (RemoteJSRuntime)scope.ServiceProvider.GetRequiredService<IJSRuntime>();
|
||||
var componentContext = (RemoteComponentContext)scope.ServiceProvider.GetRequiredService<IComponentContext>();
|
||||
jsRuntime.Initialize(client);
|
||||
componentContext.Initialize(client);
|
||||
|
||||
var navigationManager = (RemoteNavigationManager)scope.ServiceProvider.GetRequiredService<NavigationManager>();
|
||||
var navigationInterception = (RemoteNavigationInterception)scope.ServiceProvider.GetRequiredService<INavigationInterception>();
|
||||
|
|
|
|||
|
|
@ -1,19 +0,0 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
|
||||
namespace Microsoft.AspNetCore.Components.Server.Circuits
|
||||
{
|
||||
internal class RemoteComponentContext : IComponentContext
|
||||
{
|
||||
private CircuitClientProxy _clientProxy;
|
||||
|
||||
public bool IsConnected => _clientProxy != null && _clientProxy.Connected;
|
||||
|
||||
internal void Initialize(CircuitClientProxy clientProxy)
|
||||
{
|
||||
_clientProxy = clientProxy ?? throw new ArgumentNullException(nameof(clientProxy));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -70,7 +70,6 @@ namespace Microsoft.Extensions.DependencyInjection
|
|||
services.AddScoped<NavigationManager, RemoteNavigationManager>();
|
||||
services.AddScoped<IJSRuntime, RemoteJSRuntime>();
|
||||
services.AddScoped<INavigationInterception, RemoteNavigationInterception>();
|
||||
services.AddScoped<IComponentContext, RemoteComponentContext>();
|
||||
services.AddScoped<AuthenticationStateProvider, ServerAuthenticationStateProvider>();
|
||||
|
||||
services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<CircuitOptions>, CircuitOptionsJSInteropDetailedErrorsConfiguration>());
|
||||
|
|
|
|||
|
|
@ -1,46 +0,0 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Components.Server.Circuits;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNetCore.Components.Web.Rendering
|
||||
{
|
||||
public class RemoteComponentContextTest
|
||||
{
|
||||
[Fact]
|
||||
public void IfNotInitialized_IsConnectedReturnsFalse()
|
||||
{
|
||||
Assert.False(new RemoteComponentContext().IsConnected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IfInitialized_IsConnectedValueDeterminedByCircuitProxy()
|
||||
{
|
||||
// Arrange
|
||||
var clientProxy = new FakeClientProxy();
|
||||
var circuitProxy = new CircuitClientProxy(clientProxy, "test connection");
|
||||
var remoteComponentContext = new RemoteComponentContext();
|
||||
|
||||
// Act/Assert: Can observe connected state
|
||||
remoteComponentContext.Initialize(circuitProxy);
|
||||
Assert.True(remoteComponentContext.IsConnected);
|
||||
|
||||
// Act/Assert: Can observe disconnected state
|
||||
circuitProxy.SetDisconnected();
|
||||
Assert.False(remoteComponentContext.IsConnected);
|
||||
}
|
||||
|
||||
private class FakeClientProxy : IClientProxy
|
||||
{
|
||||
public Task SendCoreAsync(string method, object[] args, CancellationToken cancellationToken = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,6 @@
|
|||
@page "/prerendered-interop"
|
||||
@using Microsoft.AspNetCore.Components
|
||||
@using Microsoft.JSInterop
|
||||
@inject IComponentContext ComponentContext
|
||||
@inject IJSRuntime JSRuntime
|
||||
|
||||
<p>
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
@page "/prerendered-transition"
|
||||
@using Microsoft.AspNetCore.Components
|
||||
@using Microsoft.AspNetCore.Components.Authorization
|
||||
@inject IComponentContext ComponentContext
|
||||
|
||||
<CascadingAuthenticationState>
|
||||
<AuthorizeView>
|
||||
|
|
@ -11,7 +10,7 @@
|
|||
|
||||
<p>
|
||||
Current state:
|
||||
<strong id="connected-state">@(ComponentContext.IsConnected ? "connected" : "not connected")</strong>
|
||||
<strong id="connected-state">@(hasRenderedInConnectedMode ? "connected" : "not connected")</strong>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
|
|
@ -22,11 +21,13 @@
|
|||
</CascadingAuthenticationState>
|
||||
|
||||
@code {
|
||||
bool hasRenderedInConnectedMode;
|
||||
int count;
|
||||
protected override Task OnAfterRenderAsync(bool firstRender)
|
||||
{
|
||||
if (firstRender)
|
||||
{
|
||||
hasRenderedInConnectedMode = true;
|
||||
// We need to queue another render when we connect, otherwise the
|
||||
// browser won't see anything.
|
||||
StateHasChanged();
|
||||
|
|
|
|||
|
|
@ -208,7 +208,6 @@ namespace Microsoft.Extensions.DependencyInjection
|
|||
services.TryAddScoped<StaticComponentRenderer>();
|
||||
services.TryAddScoped<NavigationManager, HttpNavigationManager>();
|
||||
services.TryAddScoped<IJSRuntime, UnsupportedJavaScriptRuntime>();
|
||||
services.TryAddScoped<IComponentContext, UnsupportedComponentContext>();
|
||||
services.TryAddScoped<INavigationInterception, UnsupportedNavigationInterception>();
|
||||
|
||||
services.TryAddTransient<ControllerSaveTempDataPropertyFilter>();
|
||||
|
|
|
|||
|
|
@ -1,12 +0,0 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using Microsoft.AspNetCore.Components;
|
||||
|
||||
namespace Microsoft.AspNetCore.Mvc.ViewFeatures
|
||||
{
|
||||
internal class UnsupportedComponentContext : IComponentContext
|
||||
{
|
||||
public bool IsConnected => false;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue