Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.0.0-preview4, batch rendering, async, server-side #24

Merged
merged 11 commits into from
Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Both Canvas 2D and WebGL are supported.

Both client and server-side scenarios using either Blazor or Razor Components are supported.

**NOTE** Currently targets the v0.9.0 preview version of Blazor/Razor Components, which has a limitation regarding static files included in component libraries (aspnet/AspNetCore#6349). As a temporary workaround, manually add the `blazor.extensions.canvas.js` file in a `<script>` tag in the `<head>` element of your project website.
**NOTE** Currently targets the v3.0.0-preview4 version of Blazor/Razor Components, which has a limitation regarding static files included in component libraries (aspnet/AspNetCore#6349). As a temporary workaround, manually add the `blazor.extensions.canvas.js` file in a `<script>` tag in the `<head>` element of your project website.

# Installation

Expand Down Expand Up @@ -64,7 +64,7 @@ protected override async Task OnAfterRenderAsync()
}
```

**NOTE** You cannot call `CreateCanvas2DAsync` in `OnInitAsync`, because the underlying `&lt;canvas&gt;` element is not yet present in the generated markup.
**NOTE** You cannot call `CreateCanvas2DAsync` in `OnInitAsync`, because the underlying `<canvas>` element is not yet present in the generated markup.

### WebGL

Expand All @@ -84,7 +84,7 @@ protected override async Task OnAfterRenderAsync()
}
```

**NOTE** You cannot call `CreateWebGLAsync` in `OnInitAsync`, because the underlying `&lt;canvas&gt;` element is not yet present in the generated markup.
**NOTE** You cannot call `CreateWebGLAsync` in `OnInitAsync`, because the underlying `<canvas>` element is not yet present in the generated markup.

### Call Batching

Expand Down
4 changes: 2 additions & 2 deletions blazor.extensions.canvas.js

Large diffs are not rendered by default.

18 changes: 7 additions & 11 deletions src/Blazor.Extensions.Canvas.JS/src/CanvasContextManager.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
interface BatchedCall {
call: string;
name: string;
parameters: any[];
}
export class ContextManager {
private readonly contexts = new Map<string, any>();
private readonly webGLObject = new Array<any>();
Expand Down Expand Up @@ -58,16 +53,17 @@ export class ContextManager {
return this.callWithContext(context, method, args);
}

public callBatch = (canvas: HTMLCanvasElement, batchedCalls: BatchedCall[]) => {
public callBatch = (canvas: HTMLCanvasElement, batchedCalls: any[][]) => {
const context = this.getContext(canvas);
for (let i = 0; i < batchedCalls.length; i++) {
if (batchedCalls[i].call === "call") {
this.callWithContext(context, batchedCalls[i].name, batchedCalls[i].parameters);
} else if (batchedCalls[i].call === "setProperty") {
let params = batchedCalls[i].slice(2);
if (batchedCalls[i][1]) {
this.callWithContext(context, batchedCalls[i][0], params);
} else {
this.setPropertyWithContext(
context,
batchedCalls[i].name,
Array.isArray(batchedCalls[i].parameters) && batchedCalls[i].parameters.length > 0 ? batchedCalls[i].parameters[0] : null);
batchedCalls[i][0],
Array.isArray(params) && params.length > 0 ? params[0] : null);
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/Blazor.Extensions.Canvas/Blazor.Extensions.Canvas.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);IncludeP2POutput</TargetsForTfmSpecificBuildOutput>
</PropertyGroup>

<PropertyGroup>
<LangVersion>Preview</LangVersion>
<RazorLangVersion>3.0</RazorLangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Blazor" Version="0.9.0-preview3-19154-02" />
<PackageReference Include="Microsoft.AspNetCore.Blazor.Build" Version="0.9.0-preview3-19154-02" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Blazor" Version="3.0.0-preview4-19216-03" />
<PackageReference Include="Microsoft.AspNetCore.Blazor.Build" Version="3.0.0-preview4-19216-03" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
Expand Down
88 changes: 44 additions & 44 deletions src/Blazor.Extensions.Canvas/Canvas2D/Canvas2DContext.cs

Large diffs are not rendered by default.

18 changes: 0 additions & 18 deletions src/Blazor.Extensions.Canvas/CanvasBatchedCallInfo.cs

This file was deleted.

62 changes: 20 additions & 42 deletions src/Blazor.Extensions.Canvas/RenderingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,18 @@ namespace Blazor.Extensions
public abstract class RenderingContext : IDisposable
{
private const string NAMESPACE_PREFIX = "BlazorExtensions";
private const string SET_PROPERTY_ACTION = "setProperty";
private const string GET_PROPERTY_ACTION = "getProperty";
private const string CALL_METHOD_ACTION = "call";
private const string CALL_BATCH_ACTION = "callBatch";
private const string ADD_ACTION = "add";
private const string REMOVE_ACTION = "remove";
private readonly List<object[]> _batchedCallObjects = new List<object[]>();
private readonly string _contextName;
private readonly IJSRuntime _jsRuntime;
private readonly object _parameters;
private readonly SemaphoreSlim _semaphoreSlim = new SemaphoreSlim(1, 1);

private bool _awaitingBatchedCall;
private List<CanvasBatchedCallInfo> _batchedCalls = new List<CanvasBatchedCallInfo>();
private bool _batching;
private bool _initialized;

Expand Down Expand Up @@ -69,9 +68,24 @@ public async Task EndBatchAsync()
await this.BatchCallInnerAsync();
}

protected async Task SetPropertyAsync(string property, object value)
protected async Task BatchCallAsync(string name, bool isMethodCall, params object[] value)
{
await this.BatchPropertySetAsync(property, value);
await this._semaphoreSlim.WaitAsync();

var callObject = new object[value.Length + 2];
callObject[0] = name;
callObject[1] = isMethodCall;
Array.Copy(value, 0, callObject, 2, value.Length);
this._batchedCallObjects.Add(callObject);

if (this._batching || this._awaitingBatchedCall)
{
this._semaphoreSlim.Release();
}
else
{
await this.BatchCallInnerAsync();
}
}

protected async Task<T> GetPropertyAsync<T>(string property)
Expand All @@ -89,11 +103,6 @@ protected async Task<T> CallMethodAsync<T>(string method)
return await this._jsRuntime.InvokeAsync<T>($"{NAMESPACE_PREFIX}.{this._contextName}.{CALL_METHOD_ACTION}", this.Canvas, method);
}

protected async Task CallMethodAsync(string method)
{
await this.BatchMethodCallAsync(method);
}

protected T CallMethod<T>(string method, params object[] value)
{
return this.CallMethodAsync<T>(method, value).GetAwaiter().GetResult();
Expand All @@ -104,16 +113,11 @@ protected async Task<T> CallMethodAsync<T>(string method, params object[] value)
return await this._jsRuntime.InvokeAsync<T>($"{NAMESPACE_PREFIX}.{this._contextName}.{CALL_METHOD_ACTION}", this.Canvas, method, value);
}

protected async Task CallMethodAsync(string method, params object[] value)
{
await this.BatchMethodCallAsync(method, value);
}

private async Task BatchCallInnerAsync()
{
this._awaitingBatchedCall = true;
var currentBatch = this._batchedCalls.ToArray();
this._batchedCalls.Clear();
var currentBatch = this._batchedCallObjects.ToArray();
this._batchedCallObjects.Clear();
this._semaphoreSlim.Release();

_ = await this._jsRuntime.InvokeAsync<object>($"{NAMESPACE_PREFIX}.{this._contextName}.{CALL_BATCH_ACTION}", this.Canvas, currentBatch);
Expand All @@ -124,32 +128,6 @@ private async Task BatchCallInnerAsync()
this._semaphoreSlim.Release();
}

private async Task BatchCallAsync(string call, string name, params object[] value)
{
await this._semaphoreSlim.WaitAsync();

this._batchedCalls.Add(new CanvasBatchedCallInfo(call, name, value));

if (this._batching || this._awaitingBatchedCall)
{
this._semaphoreSlim.Release();
}
else
{
await this.BatchCallInnerAsync();
}
}

private async Task BatchMethodCallAsync(string method, object[] value = null)
{
await this.BatchCallAsync(CALL_METHOD_ACTION, method, value);
}

private async Task BatchPropertySetAsync(string property, object value)
{
await this.BatchCallAsync(SET_PROPERTY_ACTION, property, value);
}

public void Dispose()
{
Task.Run(async () => await this._jsRuntime.InvokeAsync<object>($"{NAMESPACE_PREFIX}.{this._contextName}.{REMOVE_ACTION}", this.Canvas));
Expand Down
Loading