1212using Microsoft . AspNetCore . Internal ;
1313using Microsoft . Extensions . DependencyInjection ;
1414
15- namespace Microsoft . AspNetCore . Components ;
15+ namespace Microsoft . AspNetCore . Components . Infrastructure ;
1616
1717internal class PersistentServicesRegistry
1818{
@@ -24,10 +24,11 @@ internal class PersistentServicesRegistry
2424 private List < PersistingComponentStateSubscription > _subscriptions = [ ] ;
2525 private static readonly ConcurrentDictionary < Type , PropertiesAccessor > _cachedAccessorsByType = new ( ) ;
2626
27- public PersistentServicesRegistry (
28- IServiceProvider serviceProvider ,
29- IEnumerable < IPersistentComponentRegistration > registrations )
27+ public IComponentRenderMode ? RenderMode { get ; internal set ; }
28+
29+ public PersistentServicesRegistry ( IServiceProvider serviceProvider )
3030 {
31+ var registrations = serviceProvider . GetRequiredService < IEnumerable < IPersistentComponentRegistration > > ( ) ;
3132 _serviceProvider = serviceProvider ;
3233 _persistentServiceTypeCache = new PersistentServiceTypeCache ( ) ;
3334 _registrations = [ .. registrations . Distinct ( ) . Order ( ) ] ;
@@ -67,7 +68,7 @@ private void RestoreRegistrationsIfAvailable(PersistentComponentState state)
6768 [ RequiresUnreferencedCode ( "Calls Microsoft.AspNetCore.Components.PersistentComponentState.TryTakeFromJson(String, Type, out Object)" ) ]
6869 private static void RestoreInstanceState ( object instance , Type type , PersistentComponentState state )
6970 {
70- var accessors = _cachedAccessorsByType . GetOrAdd ( instance . GetType ( ) , static ( Type runtimeType , Type declaredType ) => new PropertiesAccessor ( runtimeType , declaredType ) , type ) ;
71+ var accessors = _cachedAccessorsByType . GetOrAdd ( instance . GetType ( ) , static ( runtimeType , declaredType ) => new PropertiesAccessor ( runtimeType , declaredType ) , type ) ;
7172 foreach ( var ( key , propertyType ) in accessors . KeyTypePairs )
7273 {
7374 if ( state . TryTakeFromJson ( key , propertyType , out var result ) )
@@ -81,7 +82,7 @@ private static void RestoreInstanceState(object instance, Type type, PersistentC
8182 [ RequiresUnreferencedCode ( "Calls Microsoft.AspNetCore.Components.PersistentComponentState.PersistAsJson(String, Object, Type)" ) ]
8283 private static void PersistInstanceState ( object instance , Type type , PersistentComponentState state )
8384 {
84- var accessors = _cachedAccessorsByType . GetOrAdd ( instance . GetType ( ) , static ( Type runtimeType , Type declaredType ) => new PropertiesAccessor ( runtimeType , declaredType ) , type ) ;
85+ var accessors = _cachedAccessorsByType . GetOrAdd ( instance . GetType ( ) , static ( runtimeType , declaredType ) => new PropertiesAccessor ( runtimeType , declaredType ) , type ) ;
8586 foreach ( var ( key , propertyType ) in accessors . KeyTypePairs )
8687 {
8788 var ( setter , getter ) = accessors . GetAccessor ( key ) ;
@@ -116,17 +117,8 @@ internal void RegisterForPersistence(PersistentComponentState state)
116117 return ;
117118 }
118119
119- var comparer = EqualityComparer < IComponentRenderMode ? > . Create ( ( x , y ) =>
120- {
121- var xType = x ? . GetType ( ) ;
122- var yType = y ? . GetType ( ) ;
123- return xType == yType ;
124- } ) ;
125-
126- var renderModes = new HashSet < IComponentRenderMode ? > ( comparer ) ;
127-
128120 var subscriptions = new List < PersistingComponentStateSubscription > ( _registrations . Length + 1 ) ;
129- for ( var i = 1 ; i < _registrations . Length ; i ++ )
121+ for ( var i = 0 ; i < _registrations . Length ; i ++ )
130122 {
131123 var registration = _registrations [ i ] ;
132124 var type = ResolveType ( registration . Assembly , registration . FullTypeName ) ;
@@ -136,10 +128,6 @@ internal void RegisterForPersistence(PersistentComponentState state)
136128 }
137129
138130 var renderMode = registration . GetRenderModeOrDefault ( ) ;
139- if ( renderMode != null )
140- {
141- renderModes . Add ( renderMode ) ;
142- }
143131
144132 var instance = _serviceProvider . GetRequiredService ( type ) ;
145133 subscriptions . Add ( state . RegisterOnPersisting ( ( ) =>
@@ -149,13 +137,13 @@ internal void RegisterForPersistence(PersistentComponentState state)
149137 } , renderMode ) ) ;
150138 }
151139
152- foreach ( var renderMode in renderModes )
140+ if ( RenderMode != null )
153141 {
154142 subscriptions . Add ( state . RegisterOnPersisting ( ( ) =>
155143 {
156144 state . PersistAsJson ( _registryKey , _registrations ) ;
157145 return Task . CompletedTask ;
158- } , renderMode ) ) ;
146+ } , RenderMode ) ) ;
159147 }
160148
161149 _subscriptions = subscriptions ;
@@ -233,4 +221,16 @@ internal static IEnumerable<PropertyInfo> GetCandidateBindableProperties(
233221 return _underlyingAccessors . TryGetValue ( key , out var result ) ? result : default ;
234222 }
235223 }
224+
225+ private class RenderModeComparer : IEqualityComparer < IComponentRenderMode ? >
226+ {
227+ public static RenderModeComparer Instance { get ; } = new RenderModeComparer ( ) ;
228+
229+ public bool Equals ( IComponentRenderMode ? x , IComponentRenderMode ? y )
230+ {
231+ return x ? . GetType ( ) == y ? . GetType ( ) ;
232+ }
233+
234+ public int GetHashCode ( [ DisallowNull ] IComponentRenderMode ? obj ) => obj ? . GetHashCode ( ) ?? 1 ;
235+ }
236236}
0 commit comments