Skip to content

Commit

Permalink
Updated WindowService to return new instance.
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferStrube committed Apr 8, 2024
1 parent aa19147 commit 9e3a670
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@page "/"
@implements IAsyncDisposable
@inject IWindowService WindowService

@if (!Popup.HasValue)
Expand Down Expand Up @@ -66,4 +67,12 @@ else
{
await window.PostMessageAsync($"From Pop-up {Popup} \"{message}\"");
}

public async ValueTask DisposeAsync()
{
if (window is not null)
{
await window.DisposeAsync();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@
await popup.DisposeAsync();
}
await messageEventListener.DisposeAsync();
if (window is not null)
{
await window.DisposeAsync();
}
}

public record LogEntry(string message, string origin)
Expand Down
2 changes: 1 addition & 1 deletion src/KristofferStrube.Blazor.Window/IWindowService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface IWindowService
/// Returns a <see cref="Window"/>.
/// </summary>
/// <remarks>
/// Following calls to this method on the same instance of a <see cref="IWindowService"/> will return the same instance.
/// You should dispose of this <see cref="Window"/> once you are done using it to free the reference.
/// </remarks>
Task<Window> GetWindowAsync();
}
27 changes: 5 additions & 22 deletions src/KristofferStrube.Blazor.Window/WindowService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,23 @@
namespace KristofferStrube.Blazor.Window;

/// <inheritdoc cref="IWindowService"/>
public class WindowService : IAsyncDisposable, IWindowService
public class WindowService : IWindowService
{
private readonly Lazy<Task<Window>> windowTask;
private readonly IJSRuntime jSRuntime;

/// <summary>
/// Constructs a new instance of the service.
/// </summary>
/// <param name="jSRuntime">An <see cref="IJSRuntime"/> instance.</param>
public WindowService(IJSRuntime jSRuntime)
{
windowTask = new(async () =>
{
IJSObjectReference jSInstance = await jSRuntime.InvokeAsync<IJSObjectReference>("window.valueOf");
return await Window.CreateAsync(jSRuntime, jSInstance);
});
this.jSRuntime = jSRuntime;
}

/// <inheritdoc/>
public async Task<Window> GetWindowAsync()
{
return await windowTask.Value;
}

/// <summary>
/// Disposes the service.
/// </summary>
public async ValueTask DisposeAsync()
{
if (windowTask.IsValueCreated)
{
Window window = await windowTask.Value;
await window.JSReference.DisposeAsync();
await (await windowTask.Value).DisposeAsync();
}
GC.SuppressFinalize(this);
IJSObjectReference jSInstance = await jSRuntime.InvokeAsync<IJSObjectReference>("window.valueOf");
return await Window.CreateAsync(jSRuntime, jSInstance, new() { DisposesJSReference = true });
}
}

0 comments on commit 9e3a670

Please sign in to comment.