Skip to content

Commit 562666c

Browse files
Copilotjaviercn
andcommitted
Refactor RootTypeCache hot reload to use instance Clear method
Co-authored-by: javiercn <[email protected]>
1 parent 62e8bab commit 562666c

File tree

4 files changed

+23
-46
lines changed

4 files changed

+23
-46
lines changed

src/Components/Components/src/PersistentState/PersistentServicesRegistry.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ static PersistentServicesRegistry()
3030
if (HotReloadManager.Default.MetadataUpdateSupported)
3131
{
3232
HotReloadManager.Default.OnDeltaApplied += _cachedAccessorsByType.Clear;
33+
HotReloadManager.Default.OnDeltaApplied += _persistentServiceTypeCache.Clear;
3334
}
3435
}
3536

src/Components/Server/src/DependencyInjection/ComponentServiceCollectionExtensions.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.AspNetCore.Components.Authorization;
77
using Microsoft.AspNetCore.Components.Endpoints;
88
using Microsoft.AspNetCore.Components.Forms;
9+
using Microsoft.AspNetCore.Components.HotReload;
910
using Microsoft.AspNetCore.Components.Routing;
1011
using Microsoft.AspNetCore.Components.Server;
1112
using Microsoft.AspNetCore.Components.Server.BlazorPack;
@@ -67,7 +68,12 @@ public static IServerSideBlazorBuilder AddServerSideBlazor(this IServiceCollecti
6768
services.TryAddSingleton<CircuitMetrics>();
6869
services.TryAddSingleton<ICircuitFactory, CircuitFactory>();
6970
services.TryAddSingleton<ICircuitHandleRegistry, CircuitHandleRegistry>();
70-
services.TryAddSingleton<RootTypeCache>();
71+
services.TryAddSingleton<RootTypeCache>(serviceProvider =>
72+
{
73+
var cache = new RootTypeCache();
74+
RegisterForHotReload(cache);
75+
return cache;
76+
});
7177
services.TryAddSingleton<ComponentParameterDeserializer>();
7278
services.TryAddSingleton<ComponentParametersTypeCache>();
7379
services.TryAddSingleton<CircuitIdFactory>();
@@ -124,6 +130,14 @@ public static IServerSideBlazorBuilder AddServerSideBlazor(this IServiceCollecti
124130
return builder;
125131
}
126132

133+
private static void RegisterForHotReload(RootTypeCache cache)
134+
{
135+
if (HotReloadManager.Default.MetadataUpdateSupported)
136+
{
137+
HotReloadManager.Default.OnDeltaApplied += cache.Clear;
138+
}
139+
}
140+
127141
private sealed class DefaultServerSideBlazorBuilder : IServerSideBlazorBuilder
128142
{
129143
public DefaultServerSideBlazorBuilder(IServiceCollection services)

src/Components/Shared/src/RootTypeCache.cs

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,64 +4,22 @@
44
using System.Collections.Concurrent;
55
using System.Diagnostics.CodeAnalysis;
66
using System.Reflection;
7-
#if COMPONENTS
8-
using Microsoft.AspNetCore.Components.HotReload;
9-
#endif
107

11-
#if COMPONENTS
12-
namespace Microsoft.AspNetCore.Components.Infrastructure;
13-
#else
148
namespace Microsoft.AspNetCore.Components;
15-
#endif
169

1710
// A cache for root component types
1811
internal sealed class RootTypeCache
1912
{
20-
#if COMPONENTS
21-
private static readonly List<WeakReference> _instances = new();
22-
23-
static RootTypeCache()
24-
{
25-
if (HotReloadManager.Default.MetadataUpdateSupported)
26-
{
27-
HotReloadManager.Default.OnDeltaApplied += ClearCache;
28-
}
29-
}
30-
#endif
31-
3213
private readonly ConcurrentDictionary<Key, Type?> _typeToKeyLookUp = new();
3314

3415
public RootTypeCache()
3516
{
36-
#if COMPONENTS
37-
lock (_instances)
38-
{
39-
_instances.Add(new WeakReference(this));
40-
}
41-
#endif
4217
}
4318

44-
#if COMPONENTS
45-
private static void ClearCache()
19+
internal void Clear()
4620
{
47-
lock (_instances)
48-
{
49-
for (int i = _instances.Count - 1; i >= 0; i--)
50-
{
51-
var weakRef = _instances[i];
52-
if (weakRef.Target is RootTypeCache instance)
53-
{
54-
instance._typeToKeyLookUp.Clear();
55-
}
56-
else
57-
{
58-
// Remove dead reference
59-
_instances.RemoveAt(i);
60-
}
61-
}
62-
}
21+
_typeToKeyLookUp.Clear();
6322
}
64-
#endif
6523

6624
public Type? GetRootType(string assembly, string type)
6725
{

src/Components/WebAssembly/WebAssembly/src/Hosting/WebAssemblyHostBuilder.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,11 @@ internal void InitializeDefaultServices()
323323
Services.AddSingleton<IScrollToLocationHash>(WebAssemblyScrollToLocationHash.Instance);
324324
Services.AddSingleton(_jsMethods);
325325
Services.AddSingleton(new LazyAssemblyLoader(DefaultWebAssemblyJSRuntime.Instance));
326-
Services.AddSingleton(_ => _rootComponentCache ?? new());
326+
Services.AddSingleton(serviceProvider =>
327+
{
328+
var cache = _rootComponentCache ?? new RootTypeCache();
329+
return cache;
330+
});
327331
Services.AddSingleton<ComponentStatePersistenceManager>();
328332
Services.AddSingleton(sp => sp.GetRequiredService<ComponentStatePersistenceManager>().State);
329333
Services.AddSupplyValueFromPersistentComponentStateProvider();

0 commit comments

Comments
 (0)