File tree Expand file tree Collapse file tree 3 files changed +52
-8
lines changed
src/Blazor.BroadcastChannel Expand file tree Collapse file tree 3 files changed +52
-8
lines changed Original file line number Diff line number Diff line change @@ -13,7 +13,9 @@ The Broadcast Channel API allows sending messages to other browsing contexts on
1313 ```
1414 dotnet add package Blazor.BroadcastChannel
1515 ```
16- 2. In `Program.cs` add `builder.Services.AddBroadcastChannel`.
16+ 2. In `Program.cs` register the `BroadcastChannelService` service.
17+
18+ Either as a transient service.
1719 ```
1820 ...
1921
@@ -22,11 +24,27 @@ The Broadcast Channel API allows sending messages to other browsing contexts on
2224 ...
2325
2426 builder.Services.AddBroadcastChannel();
27+
28+ ...
29+
30+ await builder.Build().RunAsync();
31+ ```
32+
33+ Or as a singleton service (preferred for WASM projects).
34+ ```
35+ ...
36+
37+ var builder = WebAssemblyHostBuilder.CreateDefault(args);
2538
2639 ...
2740
41+ bulder.Services.AddBroadcastChannelAsSingleton();
42+
43+ ...
44+
2845 await builder.Build().RunAsync();
2946 ```
47+
30483. Add the `Blazor.BroadcastChannel` namespace in `_Imports.razor` or the component in which you want to use the Broadcast Channel API.
3149 ```
3250 @using Blazor.BroadcastChannel
Original file line number Diff line number Diff line change 1- using Microsoft . AspNetCore . Components ;
2- using Microsoft . JSInterop ;
1+ using Microsoft . JSInterop ;
32
43namespace Blazor . BroadcastChannel
54{
6- internal class BroadcastChannelService : IBroadcastChannelService
5+ internal class BroadcastChannelService : IBroadcastChannelService , IAsyncDisposable
76 {
87 private readonly Lazy < ValueTask < IJSObjectReference > > _moduleTask ;
98
109 public BroadcastChannelService ( IJSRuntime jsRuntime )
11- {
12- string path = "./_content/Blazor.BroadcastChannel/Blazor.BroadcastChannel.js" ;
10+ {
11+ const string path = "./_content/Blazor.BroadcastChannel/Blazor.BroadcastChannel.js" ;
1312
1413 _moduleTask = new ( ( ) => jsRuntime . InvokeAsync < IJSObjectReference > ( "import" , path ) ) ;
1514 }
@@ -44,5 +43,14 @@ public async Task AddMessageEventListener(BroadcastChannel channel)
4443
4544 await module . InvokeVoidAsync ( "addMessageEventListener" , channel . Id , DotNetObjectReference . Create ( channel ) ) ;
4645 }
46+
47+ public async ValueTask DisposeAsync ( )
48+ {
49+ if ( _moduleTask . IsValueCreated )
50+ {
51+ IJSObjectReference module = await _moduleTask . Value ;
52+ await module . DisposeAsync ( ) ;
53+ }
54+ }
4755 }
48- }
56+ }
Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ namespace Microsoft.Extensions.DependencyInjection
88 public static class BroadcastChannelServiceCollectionExtensions
99 {
1010 /// <summary>
11- /// Registers a default service which provides support for Broadcast Channel API.
11+ /// Registers a default transient service which provides support for Broadcast Channel API.
1212 /// </summary>
1313 /// <param name="services">The collection of service descriptors.</param>
1414 /// <returns>The collection of service descriptors.</returns>
@@ -24,5 +24,23 @@ public static IServiceCollection AddBroadcastChannel(this IServiceCollection ser
2424
2525 return services ;
2626 }
27+
28+ /// <summary>
29+ /// Registers a default singleton service which provides support for Broadcast Channel API.
30+ /// </summary>
31+ /// <param name="services">The collection of service descriptors.</param>
32+ /// <returns>The collection of service descriptors.</returns>
33+ public static IServiceCollection AddBroadcastChannelAsSingleton ( this IServiceCollection services )
34+ {
35+ if ( services is null )
36+ {
37+ throw new ArgumentNullException ( nameof ( services ) ) ;
38+ }
39+
40+ services . AddOptions ( ) ;
41+ services . AddSingleton < IBroadcastChannelService , BroadcastChannelService > ( ) ;
42+
43+ return services ;
44+ }
2745 }
2846}
You can’t perform that action at this time.
0 commit comments