Skip to content

Commit

Permalink
fix issue where MiniLcmApiProvider would be shared by all clients, it…
Browse files Browse the repository at this point in the history
…'s now shared per circuit
  • Loading branch information
hahn-kev committed Jan 13, 2025
1 parent e290570 commit 4b28702
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
4 changes: 3 additions & 1 deletion backend/FwLite/FwLiteShared/FwLiteSharedKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public static IServiceCollection AddFwLiteShared(this IServiceCollection service
services.AddScoped<SyncService>();
services.AddSingleton<LexboxProjectService>();
services.AddSingleton<CombinedProjectsService>();
services.AddSingleton<FwLiteProvider>();
//this is scoped so that there will be once instance per blazor circuit, this prevents issues where the same instance is used when reloading the page.
//it also avoids issues if there's multiple blazor circuits running at the same time
services.AddScoped<FwLiteProvider>();

services.AddSingleton<ChangeEventBus>();
services.AddSingleton<BackgroundSyncService>();
Expand Down
6 changes: 4 additions & 2 deletions backend/FwLite/FwLiteShared/Pages/CrdtProject.razor
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
@inherits OwningComponentBaseAsync
@layout SvelteLayout;
@inject IJSRuntime JS;
@* injecting here means we get a provider scoped to the current circuit *@
@inject FwLiteProvider FwLiteProvider;

@code {

Expand All @@ -16,8 +18,8 @@
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (!firstRender) return;
var fwLiteProvider = ScopedServices.GetRequiredService<FwLiteProvider>();
_disposable = await fwLiteProvider.InjectCrdtProject(JS, ScopedServices, ProjectName);
//scoped services here are per page render, meaning they will get cleaned up when the page is disposed
_disposable = await FwLiteProvider.InjectCrdtProject(JS, ScopedServices, ProjectName);
}

protected override async ValueTask DisposeAsyncCore()
Expand Down
4 changes: 2 additions & 2 deletions backend/FwLite/FwLiteShared/Pages/FwdataProject.razor
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
@inherits OwningComponentBaseAsync
@layout SvelteLayout;
@inject IJSRuntime JS;
@inject FwLiteProvider FwLiteProvider;

@code {

Expand All @@ -16,8 +17,7 @@
protected override Task OnAfterRenderAsync(bool firstRender)
{
if (!firstRender) return Task.CompletedTask;
var fwLiteProvider = ScopedServices.GetRequiredService<FwLiteProvider>();
_disposable = fwLiteProvider.InjectFwDataProject(ScopedServices, ProjectName);
_disposable = FwLiteProvider.InjectFwDataProject(ScopedServices, ProjectName);
return Task.CompletedTask;
}

Expand Down
17 changes: 13 additions & 4 deletions backend/FwLite/FwLiteShared/Services/FwLiteProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ public IAsyncDisposable InjectFwDataProject(IServiceProvider scopedServices, str
private IDisposable ProvideMiniLcmApi(MiniLcmJsInvokable miniLcmApi)
{
var reference = DotNetObjectReference.Create(miniLcmApi);
_miniLcmApiProvider.SetMiniLcmApi(reference);
var cleanup = _miniLcmApiProvider.SetMiniLcmApi(reference);
return Defer.Action(() =>
{
reference?.Dispose();
_miniLcmApiProvider.ClearMiniLcmApi();
reference.Dispose();
cleanup.Dispose();
});
}
}
Expand All @@ -151,10 +151,19 @@ public async Task<DotNetObjectReference<MiniLcmJsInvokable>> GetMiniLcmApi()
#pragma warning restore VSTHRD003
}

public void SetMiniLcmApi(DotNetObjectReference<MiniLcmJsInvokable> miniLcmApi)
public IDisposable SetMiniLcmApi(DotNetObjectReference<MiniLcmJsInvokable> miniLcmApi)
{
logger.LogInformation("Setting MiniLcmApi");
_tcs.SetResult(miniLcmApi);
var currentTask = _tcs.Task;
return Defer.Action(() =>
{
//if the tcs has been reset, then we don't want to clear it again
if (_tcs.Task == currentTask)
{
ClearMiniLcmApi();
}
});
}

[JSInvokable]
Expand Down

0 comments on commit 4b28702

Please sign in to comment.