Skip to content

Commit

Permalink
introduce DurableInvoke helper which uses a method defined on window …
Browse files Browse the repository at this point in the history
…in index which will always be there, to call methods which might not always be there
  • Loading branch information
hahn-kev committed Dec 18, 2024
1 parent 3c3a487 commit bf4b63a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
9 changes: 6 additions & 3 deletions backend/FwLite/FwLiteDesktop/wwwroot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
<base href="/" />
<link rel="icon" type="image/png" href="_content/FwLiteShared/favicon.png"/>
<script>
window.onBlazorNavigate = (path) => {
if ('svelteNavigate' in window) {
window.svelteNavigate(path);
window.invokeOnWindow = function (methodName, args) {
if (!(methodName in window)) {
//todo not sure how to handle this for now, maybe we wait until the app is ready?
console.error(`Method ${methodName} not found`);
return;
}
window[methodName](...args);
};
</script>
</head>
Expand Down
11 changes: 11 additions & 0 deletions backend/FwLite/FwLiteShared/JsInteropInvokeHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Microsoft.JSInterop;

namespace FwLiteShared;

public static class JsInteropInvokeHelper
{
public static async ValueTask DurableInvokeVoidAsync(this IJSRuntime jsRuntime, string identifier, params object?[] args)
{
await jsRuntime.InvokeVoidAsync("invokeOnWindow", identifier, args);
}
}
2 changes: 1 addition & 1 deletion backend/FwLite/FwLiteShared/Routes.razor
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@code {
private async Task OnNavigateAsync(NavigationContext context)
{
await jsRuntime.InvokeVoidAsync("onBlazorNavigate", context.Path);
await jsRuntime.DurableInvokeVoidAsync("svelteNavigate", context.Path);
}
}
<Router AppAssembly="typeof(Layout.SvelteLayout).Assembly" OnNavigateAsync="OnNavigateAsync">
Expand Down
4 changes: 2 additions & 2 deletions backend/FwLite/FwLiteShared/Services/FwLiteProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public object GetService(DotnetService service)
logger.LogInformation("Clearing Service {Service}", service);
}

await jsRuntime.InvokeVoidAsync(OverrideServiceFunctionName, service.ToString(), reference);
await jsRuntime.DurableInvokeVoidAsync(OverrideServiceFunctionName, service.ToString(), reference);
return reference;
}

Expand All @@ -79,7 +79,7 @@ public async Task<IAsyncDisposable> InjectCrdtProject(IJSRuntime jsRuntime,
await lexboxProjectService.ListenForProjectChanges(projectData, CancellationToken.None);
var entryUpdatedSubscription = changeEventBus.OnProjectEntryUpdated(project).Subscribe(entry =>
{
_ = jsRuntime.InvokeVoidAsync("notifyEntryUpdated", projectName, entry);
_ = jsRuntime.DurableInvokeVoidAsync("notifyEntryUpdated", projectName, entry);
});
var service = ActivatorUtilities.CreateInstance<MiniLcmJsInvokable>(scopedServices, project);
var reference = await SetService(jsRuntime,DotnetService.MiniLcmApi, service);
Expand Down
10 changes: 10 additions & 0 deletions backend/FwLite/LocalWebApp/Components/App.razor
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
<base href="/"/>
<link rel="icon" type="image/png" href="_content/FwLiteShared/favicon.png"/>
<HeadOutlet @rendermode="InteractiveServer"/>
<script>
window.invokeOnWindow = function (methodName, args) {
if (!(methodName in window)) {
//todo not sure how to handle this for now, maybe we wait until the app is ready?
console.error(`Method ${methodName} not found`);
return;
}
window[methodName](...args);
};
</script>
</head>

<body>
Expand Down

0 comments on commit bf4b63a

Please sign in to comment.