Skip to content

Commit 9f5276d

Browse files
Update RCL template to use 5.0 features (#25613)
* Update JS interop to use auto-loaded ES6 module * Test update * Make it lazy for compatibility with prerendering * Update comment * Code style: go back to .AsTask - it's probably easier to read
1 parent d5a477c commit 9f5276d

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
lines changed
Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,39 @@
1-
using Microsoft.JSInterop;
1+
using System;
22
using System.Threading.Tasks;
3+
using Microsoft.JSInterop;
34

45
namespace Company.RazorClassLibrary1
56
{
6-
public class ExampleJsInterop
7+
// This class provides an example of how JavaScript functionality can be wrapped
8+
// in a .NET class for easy consumption. The associated JavaScript module is
9+
// loaded on demand when first needed.
10+
//
11+
// This class can be registered as scoped DI service and then injected into Blazor
12+
// components for use.
13+
14+
public class ExampleJsInterop : IAsyncDisposable
715
{
8-
public static ValueTask<string> Prompt(IJSRuntime jsRuntime, string message)
16+
private readonly Lazy<Task<IJSObjectReference>> moduleTask;
17+
18+
public ExampleJsInterop(IJSRuntime jsRuntime)
19+
{
20+
moduleTask = new (() => jsRuntime.InvokeAsync<IJSObjectReference>(
21+
"import", "./_content/Company.RazorClassLibrary1/exampleJsInterop.js").AsTask());
22+
}
23+
24+
public async ValueTask<string> Prompt(string message)
25+
{
26+
var module = await moduleTask.Value;
27+
return await module.InvokeAsync<string>("showPrompt", message);
28+
}
29+
30+
public async ValueTask DisposeAsync()
931
{
10-
// Implemented in exampleJsInterop.js
11-
return jsRuntime.InvokeAsync<string>(
12-
"exampleJsFunctions.showPrompt",
13-
message);
32+
if (moduleTask.IsValueCreated)
33+
{
34+
var module = await moduleTask.Value;
35+
await module.DisposeAsync();
36+
}
1437
}
1538
}
1639
}
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
// This file is to show how a library package may provide JavaScript interop features
2-
// wrapped in a .NET API
1+
// This is a JavaScript module that is loaded on demand. It can export any number of
2+
// functions, and may import other JavaScript modules if required.
33

4-
window.exampleJsFunctions = {
5-
showPrompt: function (message) {
6-
return prompt(message, 'Type anything here');
7-
}
8-
};
4+
export function showPrompt(message) {
5+
return prompt(message, 'Type anything here');
6+
}

src/ProjectTemplates/test/template-baselines.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -877,9 +877,9 @@
877877
"Files": [
878878
"wwwroot/background.png",
879879
"wwwroot/exampleJsInterop.js",
880-
"wwwroot/styles.css",
881880
"_Imports.razor",
882881
"Component1.razor",
882+
"Component1.razor.css",
883883
"ExampleJsInterop.cs"
884884
]
885885
},

0 commit comments

Comments
 (0)