Skip to content

Commit

Permalink
Released alpha 1 to NuGet.
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferStrube committed Jun 20, 2024
1 parent 380a0ea commit 993e6c6
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ This looks like so:

Another more basic abstraction is the `JobWorker`. This simple abstraction runs some job with an input and an output on a worker. The `.csproj` look identical to the one used for the `SlimWorker`.

But what differs is that we need to create a class that implements the interface `Job<TInput, TOutput>` in the worker project. A simple way to do this is by extending the abstract class `JsonJob` which uses JSON as the format for transfering its input and output. This limits us to only use inputs and outputs that can be JSON serialized and deserialized.
But what differs is that we need to create a class that implements the interface `IJob<TInput, TOutput>` in the worker project. A simple way to do this is by extending the abstract class `JsonJob` which uses JSON as the format for transfering its input and output. This limits us to only use inputs and outputs that can be JSON serialized and deserialized.

Here were implement a job that can find the sum of the codes of each individual char in a string.

Expand Down
Binary file added docs/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace KristofferStrube.Blazor.WebWorkers;
/// <summary>
/// A job that takes some input and returns some output.
/// </summary>
public interface Job<TInput, TOutput>
public interface IJob<TInput, TOutput>
{
/// <summary>
/// How the job will send execute the job on the worker.
Expand All @@ -15,5 +15,5 @@ public interface Job<TInput, TOutput>
/// <param name="worker">The worker that the job should be runned on.</param>
/// <param name="pendingTasks">The dictionary that manages which executions finishes.</param>
/// <returns>The output of the job once it responds.</returns>
public abstract static Task<TOutput> ExecuteAsync<TJob>(TInput input, Worker worker, ConcurrentDictionary<string, TaskCompletionSource<TOutput>> pendingTasks) where TJob : Job<TInput, TOutput>;
public abstract static Task<TOutput> ExecuteAsync<TJob>(TInput input, Worker worker, ConcurrentDictionary<string, TaskCompletionSource<TOutput>> pendingTasks) where TJob : IJob<TInput, TOutput>;
}
2 changes: 1 addition & 1 deletion src/KristofferStrube.Blazor.WebWorkers/JobWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace KristofferStrube.Blazor.WebWorkers;
/// <typeparam name="TInput"></typeparam>
/// <typeparam name="TOutput"></typeparam>
/// <typeparam name="TJob"></typeparam>
public class JobWorker<TInput, TOutput, TJob> : Worker where TJob : Job<TInput, TOutput>
public class JobWorker<TInput, TOutput, TJob> : Worker where TJob : IJob<TInput, TOutput>
{
private readonly ConcurrentDictionary<string, TaskCompletionSource<TOutput>> pendingTasks = new();

Expand Down
4 changes: 2 additions & 2 deletions src/KristofferStrube.Blazor.WebWorkers/JsonJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace KristofferStrube.Blazor.WebWorkers;
/// </summary>
/// <typeparam name="TInput"></typeparam>
/// <typeparam name="TOutput"></typeparam>
public abstract class JsonJob<TInput, TOutput> : Job<TInput, TOutput>
public abstract class JsonJob<TInput, TOutput> : IJob<TInput, TOutput>
{
/// <summary>
/// The actual work being done by the job. This will be run when the job is executed.
Expand All @@ -39,7 +39,7 @@ public TOutput ExecuteWithoutUsingWorker(TInput input)
/// <summary>
/// How an input is transfered to the <see cref="JobWorker{TInput, TOutput, TJob}"/> for the <see cref="JsonJob{TInput, TOutput}"/>.
/// </summary>
public static async Task<TOutput> ExecuteAsync<TJob>(TInput input, Worker worker, ConcurrentDictionary<string, TaskCompletionSource<TOutput>> pendingTasks) where TJob : Job<TInput, TOutput>
public static async Task<TOutput> ExecuteAsync<TJob>(TInput input, Worker worker, ConcurrentDictionary<string, TaskCompletionSource<TOutput>> pendingTasks) where TJob : IJob<TInput, TOutput>
{
string requestIdentifier = Guid.NewGuid().ToString();
var tcs = new TaskCompletionSource<TOutput>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,31 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Title>Blazor Web Workers wrapper</Title>
<Description>Web Workers wrapper implementation for Blazor.</Description>
<PackageId>KristofferStrube.Blazor.WebWorkers</PackageId>
<PackageTags>Blazor;Web;Workers;Worker;Job;Threading;Wasm;Wrapper;JSInterop</PackageTags>
<RepositoryUrl>https://github.com/KristofferStrube/Blazor.WebAudio</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageVersion>0.1.0-alpha.1</PackageVersion>
<Authors>Kristoffer Strube</Authors>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageIcon>icon.png</PackageIcon>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
<None Include="..\..\docs\icon.png">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<None Include="..\..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<SupportedPlatform Include="browser" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/KristofferStrube.Blazor.WebWorkers/SlimWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class SlimWorker : Worker
/// <param name="args">The args to parse to the program in the specified assembly when running it.</param>
public static async Task<SlimWorker> CreateAsync(IJSRuntime jSRuntime, string assembly, string[]? args = null)
{
args ??= Array.Empty<string>();
args ??= [];

string scriptUrl = "_content/KristofferStrube.Blazor.WebWorkers/KristofferStrube.Blazor.WebWorkers.SlimWorker.js"
+ $"?assembly={assembly}"
Expand Down

0 comments on commit 993e6c6

Please sign in to comment.