22// The .NET Foundation licenses this file to you under the MIT license.
33
44using System . Collections . Concurrent ;
5+ using Microsoft . AspNetCore . Components . HotReload ;
56
67namespace Microsoft . AspNetCore . Components ;
78
89internal sealed class ChangeDetection
910{
10- private static readonly ConcurrentDictionary < Type , bool > _immutableNonObjectTypesCache = new ( ) ;
11+ private static readonly ConcurrentDictionary < Type , bool > _immutableObjectTypesCache = new ( ) ;
12+
13+ static ChangeDetection ( )
14+ {
15+ if ( HotReloadManager . Default . MetadataUpdateSupported )
16+ {
17+ HotReloadManager . Default . OnDeltaApplied += _immutableObjectTypesCache . Clear ;
18+ }
19+ }
1120
1221 public static bool MayHaveChanged < T1 , T2 > ( T1 oldValue , T2 newValue )
1322 {
@@ -34,35 +43,18 @@ public static bool MayHaveChanged<T1, T2>(T1 oldValue, T2 newValue)
3443 return false ;
3544 }
3645
37- // The contents of this list need to trade off false negatives against computation
38- // time. So we don't want a huge list of types to check (or would have to move to
39- // a hashtable lookup, which is differently expensive). It's better not to include
40- // uncommon types here even if they are known to be immutable.
4146 // This logic assumes that no new System.TypeCode enum entries have been declared since 7.0, or at least that any new ones
4247 // represent immutable types. If System.TypeCode changes, review this logic to ensure it is still correct.
4348 // Supported immutable types : bool, byte, sbyte, short, ushort, int, uint, long, ulong, char, double,
4449 // string, DateTime, decimal, Guid, Enum, EventCallback, EventCallback<>.
4550 // For performance reasons, the following immutable types are not supported: IntPtr, UIntPtr, Type.
4651 private static bool IsKnownImmutableType ( Type type )
4752 => Type . GetTypeCode ( type ) != TypeCode . Object
48- || _immutableNonObjectTypesCache . GetOrAdd ( type , IsImmutableNonObjectCore ) ;
53+ || _immutableObjectTypesCache . GetOrAdd ( type , IsImmutableObjectTypeCore ) ;
4954
50- private static bool IsImmutableNonObjectCore ( Type type )
55+ private static bool IsImmutableObjectTypeCore ( Type type )
5156 => type == typeof ( Guid )
5257 || type == typeof ( DateOnly )
5358 || type == typeof ( TimeOnly )
5459 || typeof ( IEventCallback ) . IsAssignableFrom ( type ) ;
55-
56- public static event Action ? OnHotReload ;
57-
58- public static void ClearImmutableTypeCache ( )
59- {
60- _immutableNonObjectTypesCache . Clear ( ) ;
61- }
62-
63- public static void HotReloadHandler ( )
64- {
65- ClearImmutableTypeCache ( ) ;
66- OnHotReload ? . Invoke ( ) ;
67- }
6860}
0 commit comments