Skip to content

Commit e9b1f6f

Browse files
committed
feat: 增加 BootstrapBlazorRootRegisterService 服务
1 parent fdd3902 commit e9b1f6f

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed

src/BootstrapBlazor/Extensions/BootstrapBlazorServiceCollectionExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ public static IServiceCollection AddBootstrapBlazor(this IServiceCollection serv
3636
services.TryAddSingleton<IZipArchiveService, DefaultZipArchiveService>();
3737
services.TryAddSingleton(typeof(IDispatchService<>), typeof(DefaultDispatchService<>));
3838

39+
// BootstrapBlazorRootRegisterService 服务
40+
services.AddScoped<BootstrapBlazorRootRegisterService>();
41+
3942
// Html2Pdf 服务
4043
services.TryAddSingleton<IHtml2Pdf, DefaultHtml2PdfService>();
4144

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the Apache 2.0 License
3+
// See the LICENSE file in the project root for more information.
4+
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
5+
6+
namespace BootstrapBlazor.Components;
7+
8+
/// <summary>
9+
/// BootstrapBlazorRootRegisterService
10+
/// </summary>
11+
public class BootstrapBlazorRootRegisterService
12+
{
13+
private readonly Dictionary<object, BootstrapBlazorRootOutlet> _subscribersByIdentifier = [];
14+
private readonly Dictionary<object, List<BootstrapBlazorRootContent>> _providersByIdentifier = [];
15+
16+
/// <summary>
17+
/// add provider
18+
/// </summary>
19+
/// <param name="identifier"></param>
20+
/// <param name="provider"></param>
21+
public void AddProvider(object identifier, BootstrapBlazorRootContent provider)
22+
{
23+
if (!_providersByIdentifier.TryGetValue(identifier, out var providers))
24+
{
25+
providers = [];
26+
_providersByIdentifier.Add(identifier, providers);
27+
}
28+
29+
providers.Add(provider);
30+
}
31+
32+
/// <summary>
33+
/// remove provider
34+
/// </summary>
35+
/// <param name="identifier"></param>
36+
/// <param name="provider"></param>
37+
public void RemoveProvider(object identifier, BootstrapBlazorRootContent provider)
38+
{
39+
if (!_providersByIdentifier.TryGetValue(identifier, out var providers))
40+
{
41+
throw new InvalidOperationException($"There are no content providers with the given root ID '{identifier}'.");
42+
}
43+
44+
var index = providers.LastIndexOf(provider);
45+
if (index < 0)
46+
{
47+
throw new InvalidOperationException($"The provider was not found in the providers list of the given root ID '{identifier}'.");
48+
}
49+
50+
providers.RemoveAt(index);
51+
if (index == providers.Count)
52+
{
53+
// We just removed the most recently added provider, meaning we need to change
54+
// the current content to that of second most recently added provider.
55+
var contentProvider = GetCurrentProviderContentOrDefault(providers);
56+
NotifyContentChangedForSubscriber(identifier, contentProvider);
57+
}
58+
}
59+
60+
/// <summary>
61+
/// get all providers by identifier
62+
/// </summary>
63+
/// <param name="identifier"></param>
64+
/// <returns></returns>
65+
public List<BootstrapBlazorRootContent> GetProviders(object identifier)
66+
{
67+
_providersByIdentifier.TryGetValue(identifier, out var providers);
68+
return providers ?? [];
69+
}
70+
71+
/// <summary>
72+
/// subscribe
73+
/// </summary>
74+
/// <param name="identifier"></param>
75+
/// <param name="subscriber"></param>
76+
public void Subscribe(object identifier, BootstrapBlazorRootOutlet subscriber)
77+
{
78+
if (_subscribersByIdentifier.ContainsKey(identifier))
79+
{
80+
throw new InvalidOperationException($"There is already a subscriber to the content with the given root ID '{identifier}'.");
81+
}
82+
83+
_subscribersByIdentifier.Add(identifier, subscriber);
84+
}
85+
86+
/// <summary>
87+
/// 取消订阅
88+
/// </summary>
89+
/// <param name="identifier"></param>
90+
public void Unsubscribe(object identifier)
91+
{
92+
if (!_subscribersByIdentifier.Remove(identifier))
93+
{
94+
throw new InvalidOperationException($"The subscriber with the given root ID '{identifier}' is already unsubscribed.");
95+
}
96+
}
97+
98+
/// <summary>
99+
/// Notify content provider changed
100+
/// </summary>
101+
/// <param name="identifier"></param>
102+
/// <param name="provider"></param>
103+
public void NotifyContentProviderChanged(object identifier, BootstrapBlazorRootContent provider)
104+
{
105+
if (!_providersByIdentifier.TryGetValue(identifier, out var providers))
106+
{
107+
throw new InvalidOperationException($"There are no content providers with the given root ID '{identifier}'.");
108+
}
109+
110+
// We only notify content changed for subscribers when the content of the
111+
// most recently added provider changes.
112+
if (providers.Count != 0 && providers[^1] == provider)
113+
{
114+
NotifyContentChangedForSubscriber(identifier, provider);
115+
}
116+
}
117+
118+
private static BootstrapBlazorRootContent? GetCurrentProviderContentOrDefault(List<BootstrapBlazorRootContent> providers)
119+
=> providers.Count != 0
120+
? providers[^1]
121+
: null;
122+
123+
private void NotifyContentChangedForSubscriber(object identifier, BootstrapBlazorRootContent? provider)
124+
{
125+
if (_subscribersByIdentifier.TryGetValue(identifier, out var subscriber))
126+
{
127+
subscriber.ContentUpdated(provider);
128+
}
129+
}
130+
}

0 commit comments

Comments
 (0)