@@ -5,15 +5,15 @@ namespace Microsoft.AspNetCore.Components.Sections;
5
5
6
6
internal sealed class SectionRegistry
7
7
{
8
- private readonly Dictionary < string , ISectionContentSubscriber > _subscribersByName = new ( ) ;
9
- private readonly Dictionary < string , List < ISectionContentProvider > > _providersByName = new ( ) ;
8
+ private readonly Dictionary < object , ISectionContentSubscriber > _subscribersBySectionId = new ( ) ;
9
+ private readonly Dictionary < object , List < ISectionContentProvider > > _providersBySectionId = new ( ) ;
10
10
11
- public void AddProvider ( string name , ISectionContentProvider provider , bool isDefaultProvider )
11
+ public void AddProvider ( object sectionId , ISectionContentProvider provider , bool isDefaultProvider )
12
12
{
13
- if ( ! _providersByName . TryGetValue ( name , out var providers ) )
13
+ if ( ! _providersBySectionId . TryGetValue ( sectionId , out var providers ) )
14
14
{
15
15
providers = new ( ) ;
16
- _providersByName . Add ( name , providers ) ;
16
+ _providersBySectionId . Add ( sectionId , providers ) ;
17
17
}
18
18
19
19
if ( isDefaultProvider )
@@ -26,18 +26,18 @@ public void AddProvider(string name, ISectionContentProvider provider, bool isDe
26
26
}
27
27
}
28
28
29
- public void RemoveProvider ( string name , ISectionContentProvider provider )
29
+ public void RemoveProvider ( object sectionId , ISectionContentProvider provider )
30
30
{
31
- if ( ! _providersByName . TryGetValue ( name , out var providers ) )
31
+ if ( ! _providersBySectionId . TryGetValue ( sectionId , out var providers ) )
32
32
{
33
- throw new InvalidOperationException ( $ "There are no content providers with the name ' { name } '.") ;
33
+ throw new InvalidOperationException ( $ "There are no content providers with the given section ID ' { sectionId } '.") ;
34
34
}
35
35
36
36
var index = providers . LastIndexOf ( provider ) ;
37
37
38
38
if ( index < 0 )
39
39
{
40
- throw new InvalidOperationException ( $ "The provider was not found in the providers list of name ' { name } '.") ;
40
+ throw new InvalidOperationException ( $ "The provider was not found in the providers list of the given section ID ' { sectionId } '.") ;
41
41
}
42
42
43
43
providers . RemoveAt ( index ) ;
@@ -47,44 +47,44 @@ public void RemoveProvider(string name, ISectionContentProvider provider)
47
47
// We just removed the most recently added provider, meaning we need to change
48
48
// the current content to that of second most recently added provider.
49
49
var content = GetCurrentProviderContentOrDefault ( providers ) ;
50
- NotifyContentChangedForSubscriber ( name , content ) ;
50
+ NotifyContentChangedForSubscriber ( sectionId , content ) ;
51
51
}
52
52
}
53
53
54
- public void Subscribe ( string name , ISectionContentSubscriber subscriber )
54
+ public void Subscribe ( object sectionId , ISectionContentSubscriber subscriber )
55
55
{
56
- if ( _subscribersByName . ContainsKey ( name ) )
56
+ if ( _subscribersBySectionId . ContainsKey ( sectionId ) )
57
57
{
58
- throw new InvalidOperationException ( $ "There is already a subscriber to the content ' { name } '.") ;
58
+ throw new InvalidOperationException ( $ "There is already a subscriber to the content with the given section ID ' { sectionId } '.") ;
59
59
}
60
60
61
61
// Notify the new subscriber with any existing content.
62
- var content = GetCurrentProviderContentOrDefault ( name ) ;
62
+ var content = GetCurrentProviderContentOrDefault ( sectionId ) ;
63
63
subscriber . ContentChanged ( content ) ;
64
64
65
- _subscribersByName . Add ( name , subscriber ) ;
65
+ _subscribersBySectionId . Add ( sectionId , subscriber ) ;
66
66
}
67
67
68
- public void Unsubscribe ( string name )
68
+ public void Unsubscribe ( object sectionId )
69
69
{
70
- if ( ! _subscribersByName . Remove ( name ) )
70
+ if ( ! _subscribersBySectionId . Remove ( sectionId ) )
71
71
{
72
- throw new InvalidOperationException ( $ "The subscriber with name ' { name } ' is already unsubscribed.") ;
72
+ throw new InvalidOperationException ( $ "The subscriber with the given section ID ' { sectionId } ' is already unsubscribed.") ;
73
73
}
74
74
}
75
75
76
- public void NotifyContentChanged ( string name , ISectionContentProvider provider )
76
+ public void NotifyContentChanged ( object sectionId , ISectionContentProvider provider )
77
77
{
78
- if ( ! _providersByName . TryGetValue ( name , out var providers ) )
78
+ if ( ! _providersBySectionId . TryGetValue ( sectionId , out var providers ) )
79
79
{
80
- throw new InvalidOperationException ( $ "There are no content providers with the name ' { name } '.") ;
80
+ throw new InvalidOperationException ( $ "There are no content providers with the given section ID ' { sectionId } '.") ;
81
81
}
82
82
83
83
// We only notify content changed for subscribers when the content of the
84
84
// most recently added provider changes.
85
85
if ( providers . Count != 0 && providers [ ^ 1 ] == provider )
86
86
{
87
- NotifyContentChangedForSubscriber ( name , provider . Content ) ;
87
+ NotifyContentChangedForSubscriber ( sectionId , provider . Content ) ;
88
88
}
89
89
}
90
90
@@ -93,14 +93,14 @@ public void NotifyContentChanged(string name, ISectionContentProvider provider)
93
93
? providers [ ^ 1 ] . Content
94
94
: null ;
95
95
96
- private RenderFragment ? GetCurrentProviderContentOrDefault ( string name )
97
- => _providersByName . TryGetValue ( name , out var existingList )
96
+ private RenderFragment ? GetCurrentProviderContentOrDefault ( object sectionId )
97
+ => _providersBySectionId . TryGetValue ( sectionId , out var existingList )
98
98
? GetCurrentProviderContentOrDefault ( existingList )
99
99
: null ;
100
100
101
- private void NotifyContentChangedForSubscriber ( string name , RenderFragment ? content )
101
+ private void NotifyContentChangedForSubscriber ( object sectionId , RenderFragment ? content )
102
102
{
103
- if ( _subscribersByName . TryGetValue ( name , out var subscriber ) )
103
+ if ( _subscribersBySectionId . TryGetValue ( sectionId , out var subscriber ) )
104
104
{
105
105
subscriber . ContentChanged ( content ) ;
106
106
}
0 commit comments