Skip to content

Commit 7724407

Browse files
groogiamtpeczek
authored andcommitted
#4 Added support for singleton registration and BroadcastChannelService.IAsyncDisposable implementation.
1 parent b2db901 commit 7724407

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff 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+
3048
3. 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

src/Blazor.BroadcastChannel/BroadcastChannelService.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
using Microsoft.AspNetCore.Components;
2-
using Microsoft.JSInterop;
1+
using Microsoft.JSInterop;
32

43
namespace 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+
}

src/Blazor.BroadcastChannel/BroadcastChannelServiceCollectionExtensions.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)