-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Examples for calling static dotnet from worker, and fixes bug for cal…
…ling instance methods (#106) * dotnet8 fixes and some examples * Fix copy-paste error * version up JsRuntime
- Loading branch information
Showing
23 changed files
with
340 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/BlazorWorker.Demo/Net7/Client/BlazorWorker.Demo.Client/Pages/ComplexSerialization.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@page "/ComplexSerialization" | ||
<BlazorWorker.Demo.SharedPages.Pages.ComplexSerialization /> |
2 changes: 2 additions & 0 deletions
2
src/BlazorWorker.Demo/Net7/Client/BlazorWorker.Demo.Client/Pages/IndexedDb.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@page "/IndexedDb" | ||
<BlazorWorker.Demo.SharedPages.Pages.IndexedDb /> |
2 changes: 2 additions & 0 deletions
2
src/BlazorWorker.Demo/Net7/Client/BlazorWorker.Demo.Client/Pages/JsDirect.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@page "/JsDirect" | ||
<BlazorWorker.Demo.SharedPages.Pages.JsDirect /> |
2 changes: 2 additions & 0 deletions
2
src/BlazorWorker.Demo/Net7/Client/BlazorWorker.Demo.Client/Pages/JsInteractions.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@page "/JsInteractions" | ||
<BlazorWorker.Demo.SharedPages.Pages.JsInteractions /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/BlazorWorker.Demo/Net8/Client/BlazorWorker.Demo.Client/Pages/JsInteractions.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@page "/JsInteractions" | ||
<BlazorWorker.Demo.SharedPages.Pages.JsInteractions /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
using BlazorWorker.BackgroundServiceFactory; | ||
using BlazorWorker.Core; | ||
using BlazorWorker.Extensions.JSRuntime; | ||
using BlazorWorker.WorkerBackgroundService; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.JSInterop; | ||
using System; | ||
using System.Runtime.InteropServices.JavaScript; | ||
using System.Threading.Tasks; | ||
|
||
namespace BlazorWorker.Demo.Shared | ||
{ | ||
public partial class JsInteractionsExample | ||
{ | ||
private readonly IWorkerFactory workerFactory; | ||
private IWorkerBackgroundService<JsInteractionsExampleWorkerService> service; | ||
public event EventHandler<string> LogHandler; | ||
public event EventHandler<string> WorkerLogHandler; | ||
|
||
public JsInteractionsExample(IWorkerFactory workerFactory) | ||
{ | ||
this.workerFactory = workerFactory; | ||
} | ||
|
||
private void Log(string message) | ||
{ | ||
LogHandler?.Invoke(this, message); | ||
} | ||
|
||
private void WorkerLog(string message) | ||
{ | ||
WorkerLogHandler?.Invoke(this, message); | ||
} | ||
|
||
public async Task Execute() | ||
{ | ||
if (this.service == null) | ||
{ | ||
Log("Execute: Creating worker..."); | ||
var worker = await this.workerFactory.CreateAsync(); | ||
Log("Execute: Creating service..."); | ||
this.service = await worker | ||
.CreateBackgroundServiceUsingFactoryAsync<JsInteractionsExampleStartup, JsInteractionsExampleWorkerService>(x => | ||
x.Resolve<JsInteractionsExampleWorkerService>()); | ||
Log("Execute: Registering log event..."); | ||
await this.service.RegisterEventListenerAsync<string>("Log", (s, log) => WorkerLog(log)); | ||
Log("Execute: Service Created."); | ||
} | ||
|
||
Log($"Execute: Calling ExecuteJsInteractionWithCallback on worker..."); | ||
await service.RunAsync(s => s.ExecuteJsInteractionWithCallback()); | ||
Log("Execute: Done"); | ||
} | ||
|
||
} | ||
|
||
#region Runs on web worker | ||
public class JsInteractionsExampleStartup | ||
{ | ||
private readonly IServiceProvider serviceProvider; | ||
|
||
public JsInteractionsExampleStartup() | ||
{ | ||
var serviceCollection = new ServiceCollection(); | ||
Configure(serviceCollection); | ||
this.serviceProvider = serviceCollection.BuildServiceProvider(); | ||
} | ||
|
||
public T Resolve<T>() => serviceProvider.GetService<T>(); | ||
|
||
public void Configure(IServiceCollection services) | ||
{ | ||
services.AddBlazorWorkerJsRuntime() | ||
.AddTransient<JsInteractionsExampleWorkerService>(); | ||
} | ||
|
||
} | ||
|
||
public partial class JsInteractionsExampleWorkerService : IDisposable, IAsyncDisposable | ||
{ | ||
private readonly IJSRuntime blazorJsRuntime; | ||
|
||
public event EventHandler<string> Log; | ||
|
||
private DotNetObjectReference<JsInteractionsExampleWorkerService> selfRef; | ||
|
||
public JsInteractionsExampleWorkerService(IJSRuntime blazorJsRuntime) | ||
{ | ||
this.blazorJsRuntime = blazorJsRuntime; | ||
} | ||
public async Task ExecuteJsInteractionWithCallback() | ||
{ | ||
// Method setupJsDirectForWorker is defined in BlazorWorker.Demo.SharedPages/wwwroot/JsDirectExample.js | ||
await this.blazorJsRuntime.InvokeVoidAsync("importLocalScripts", "_content/BlazorWorker.Demo.SharedPages/JsInteractionsExample.js"); | ||
|
||
await this.blazorJsRuntime.InvokeVoidAsync("jsInteractionsExample", selfRef ??= DotNetObjectReference.Create(this)); | ||
} | ||
|
||
/// <summary> | ||
/// This instance method will be called from JsInteractionsExample.js. | ||
/// </summary> | ||
/// <param name="arg"></param> | ||
[JSInvokable] | ||
public void CallbackFromJavascript(string arg) | ||
{ | ||
Console.WriteLine($"Worker Console: {nameof(CallbackFromJavascript)}('{arg}')"); | ||
Log?.Invoke(this, $"{nameof(CallbackFromJavascript)}('{arg}')"); | ||
} | ||
|
||
[JSExport] | ||
public static int StaticCallbackFromJs(string arg) | ||
{ | ||
Console.WriteLine($"Worker Console: {nameof(StaticCallbackFromJs)}('{arg}')"); | ||
return 5; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
selfRef?.Dispose(); | ||
} | ||
|
||
public async ValueTask DisposeAsync() | ||
{ | ||
this.Dispose(); | ||
} | ||
} | ||
|
||
#endregion | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using BlazorWorker.Core; | ||
using BlazorWorker.Demo.IoCExample; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace BlazorWorker.Demo.Shared | ||
{ | ||
public static class SharedDemoExtensions | ||
{ | ||
public static IServiceCollection AddDemoDependencies(this IServiceCollection serviceCollection) | ||
{ | ||
serviceCollection.AddWorkerFactory(); | ||
serviceCollection.AddIndexedDbDemoPersonConfig(); | ||
serviceCollection.AddTransient<JsDirectExample>(); | ||
serviceCollection.AddTransient<JsInteractionsExample>(); | ||
return serviceCollection; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/BlazorWorker.Demo/SharedPages/Pages/JsInteractions.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
@inject JsInteractionsExample jsDirectExample | ||
|
||
<div class="row"> | ||
<div class="col-9 col-xs-12"> | ||
<h1>Interacting with Js Calls</h1> | ||
|
||
Demonstrates how to call to js from a worker using IJsRuntime, | ||
and how call back directly to a dotnet instance. | ||
|
||
|
||
<br /> | ||
<br /> | ||
<button disabled=@RunDisabled @onclick=OnClick class="btn btn-primary">Run test</button><br /><br /> | ||
<br /> | ||
<br /> | ||
<div class="row"> | ||
<div class="col-6 col-xs-12"> | ||
<strong>JsInteractions.razor Output:</strong> | ||
|
||
<hr /> | ||
<pre style="text-wrap: pretty">@output</pre> | ||
</div> | ||
<div class="col-6 col-xs-12"> | ||
<strong>Worker dotnet Output:</strong> | ||
<hr /> | ||
<pre style="text-wrap: pretty">@workeroutput</pre> | ||
</div> | ||
</div> | ||
|
||
</div> | ||
<div class="col-3 col-xs-12"> | ||
<GithubSource RelativePath="Pages/JsDirect.razor" /> | ||
</div> | ||
</div> | ||
@code { | ||
string output; | ||
string workeroutput; | ||
string RunDisabled => Running ? "disabled" : null; | ||
bool Running = false; | ||
|
||
protected override void OnInitialized() | ||
{ | ||
jsDirectExample.LogHandler += (s, e) => log(e); | ||
jsDirectExample.WorkerLogHandler += (s, e) => workerlog(e); | ||
output = ""; | ||
workeroutput = ""; | ||
base.OnInitialized(); | ||
} | ||
|
||
public async Task OnClick(EventArgs _) | ||
{ | ||
Running = true; | ||
try | ||
{ | ||
await jsDirectExample.Execute(); | ||
} | ||
catch (Exception e) | ||
{ | ||
log($"Error = {e}"); | ||
} | ||
finally | ||
{ | ||
Running = false; | ||
} | ||
} | ||
|
||
void log(string logStr){ | ||
output += $"{Environment.NewLine}{LogDate()} {logStr}"; | ||
StateHasChanged(); | ||
} | ||
|
||
void workerlog(string logStr) | ||
{ | ||
workeroutput += $"{Environment.NewLine}{LogDate()} {logStr}"; | ||
StateHasChanged(); | ||
} | ||
|
||
private string LogDate() | ||
{ | ||
return DateTime.Now.ToString("HH:mm:ss:fff"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.