Skip to content

Commit

Permalink
Merge pull request #263 from dymaptic/bug/262-pro-wasm
Browse files Browse the repository at this point in the history
fix pro wasm bug, refactor JS module loading
  • Loading branch information
TimPurdum authored Nov 15, 2023
2 parents 8109857 + 06d0be3 commit 59a26ad
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public async Task Add(IEnumerable<Graphic> graphics, CancellationToken cancellat

ms.Seek(0, SeekOrigin.Begin);
#if NET7_0_OR_GREATER
MapView.AddGraphicsSyncInterop(ms.ToArray(), View!.Id.ToString(), Id.ToString());
View.AddGraphicsSyncInterop(ms.ToArray(), View!.Id.ToString(), Id.ToString());
await ms.DisposeAsync();
await Task.Delay(1, cancellationToken);
#else
Expand Down
7 changes: 4 additions & 3 deletions src/dymaptic.GeoBlazor.Core/Components/Layers/Layer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,12 @@ public async Task Load(CancellationToken cancellationToken = default)
{
AbortManager = new AbortManager(JsRuntime);
IJSObjectReference abortSignal = await AbortManager!.CreateAbortSignal(cancellationToken);
IJSObjectReference arcGisJsInterop = await GetArcGisJsInterop();
IJSObjectReference? arcGisPro = await JsModuleManager.GetArcGisJsPro(JsRuntime, cancellationToken);
IJSObjectReference arcGisJsInterop = await JsModuleManager.GetArcGisJsCore(JsRuntime, arcGisPro, cancellationToken);

if (GetType().Namespace!.Contains("Pro"))
if (arcGisPro is not null)
{
JsLayerReference = await (await GetArcGisJsPro())!.InvokeAsync<IJSObjectReference>("createProLayer",
JsLayerReference = await arcGisPro.InvokeAsync<IJSObjectReference>("createProLayer",
// ReSharper disable once RedundantCast
cancellationToken, (object)this, true, View?.Id);
}
Expand Down
40 changes: 0 additions & 40 deletions src/dymaptic.GeoBlazor.Core/Components/MapComponent.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,46 +327,6 @@ protected virtual async Task RenderView(bool forceRender = false)
}
}

/// <summary>
/// Retrieves the main entry point for the JavaScript interop.
/// </summary>
protected async Task<IJSObjectReference> GetArcGisJsInterop()
{
LicenseType licenseType = Licensing.GetLicenseType();

switch ((int)licenseType)
{
case >= 100:
// this is here to support the pro extension library
IJSObjectReference proModule = await JsRuntime
.InvokeAsync<IJSObjectReference>("import", CancellationTokenSource.Token,
"./_content/dymaptic.GeoBlazor.Pro/js/arcGisPro.js");

return await proModule.InvokeAsync<IJSObjectReference>("getCore");
default:
return await JsRuntime
.InvokeAsync<IJSObjectReference>("import", CancellationTokenSource.Token,
"./_content/dymaptic.GeoBlazor.Core/js/arcGisJsInterop.js");
}
}

/// <summary>
/// Retrieves the main entry point for the optional GeoBlazor Pro JavaScript module.
/// </summary>
protected async Task<IJSObjectReference?> GetArcGisJsPro()
{
LicenseType licenseType = Licensing.GetLicenseType();

switch ((int)licenseType)
{
case >= 100:
return await JsRuntime.InvokeAsync<IJSObjectReference>("import", CancellationTokenSource.Token,
"./_content/dymaptic.GeoBlazor.Pro/js/arcGisPro.js");
default:
return null;
}
}

private readonly Dictionary<string, (Delegate Handler, IJSObjectReference JsObjRef)> _watchers = new();
private readonly Dictionary<string, (Delegate Handler, IJSObjectReference JsObjRef)> _listeners = new();
private readonly Dictionary<string, (Delegate Handler, IJSObjectReference JsObjRef)> _waiters = new();
Expand Down
39 changes: 20 additions & 19 deletions src/dymaptic.GeoBlazor.Core/Components/Views/MapView.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1414,9 +1414,24 @@ public async Task AddGraphics(IEnumerable<Graphic> graphics, CancellationToken c
}

#if NET7_0_OR_GREATER
internal void AddGraphicsSyncInterop(byte[] graphics, string id, string? layerId = null)
{
if (ProJsViewModule is not null)
{
AddGraphicsProSyncInterop(graphics, id, layerId);
}
else
{
AddGraphicsCoreSyncInterop(graphics, id, layerId);
}
}

#pragma warning disable CA1416
[JSImport("addGraphicsSyncInterop", "arcGisJsInterop")]
internal static partial void AddGraphicsSyncInterop(byte[] graphics, string id, string? layerId = null);
[JSImport("addGraphicsCoreSyncInterop", "arcGisJsInterop")]
internal static partial void AddGraphicsCoreSyncInterop(byte[] graphics, string id, string? layerId = null);

[JSImport("addGraphicsProSyncInterop", "arcGisPro")]
internal static partial void AddGraphicsProSyncInterop(byte[] graphics, string id, string? layerId = null);
#pragma warning restore CA1416
#endif

Expand Down Expand Up @@ -2134,20 +2149,6 @@ protected override async Task OnParametersSetAsync()
await UpdateView();
}

/// <inheritdoc />
protected override async Task OnInitializedAsync()
{
#if NET7_0_OR_GREATER
if (IsWebAssembly)
{
#pragma warning disable CA1416
await JSHost.ImportAsync("arcGisJsInterop", "../_content/dymaptic.GeoBlazor.Core/js/arcGisJsInterop.js");
#pragma warning restore CA1416
}
#else
await Task.Run(() => Task.CompletedTask);
#endif
}

/// <inheritdoc />
protected override async Task OnAfterRenderAsync(bool firstRender)
Expand All @@ -2161,11 +2162,11 @@ protected override async Task OnAfterRenderAsync(bool firstRender)

if (firstRender)
{
ViewJsModule = await GetArcGisJsInterop();
ProJsViewModule = await JsModuleManager.GetArcGisJsPro(JsRuntime, CancellationTokenSource.Token);

JsModule = ViewJsModule;
ViewJsModule = await JsModuleManager.GetArcGisJsCore(JsRuntime, ProJsViewModule, CancellationTokenSource.Token);

ProJsViewModule = await GetArcGisJsPro();
JsModule = ViewJsModule;

// the first render never has all the child components registered
Rendering = false;
Expand Down
57 changes: 40 additions & 17 deletions src/dymaptic.GeoBlazor.Core/JsModuleManager.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,56 @@
using Microsoft.JSInterop;

#if NET7_0_OR_GREATER
using System.Runtime.InteropServices.JavaScript;
#endif

namespace dymaptic.GeoBlazor.Core;

internal static class JsModuleManager
{
public static async Task<IJSObjectReference> GetJsModule(IJSRuntime jsRuntime)
public static async Task<IJSObjectReference> GetArcGisJsCore(IJSRuntime jsRuntime, IJSObjectReference? proModule, CancellationToken cancellationToken)
{
LicenseType licenseType = Licensing.GetLicenseType();

if (proModule is null)
{
#if NET7_0_OR_GREATER
if (OperatingSystem.IsBrowser())
{
#pragma warning disable CA1416
await JSHost.ImportAsync("arcGisJsInterop", "../_content/dymaptic.GeoBlazor.Core/js/arcGisJsInterop.js");
#pragma warning restore CA1416
}
#endif
return await jsRuntime
.InvokeAsync<IJSObjectReference>("import", cancellationToken, "./_content/dymaptic.GeoBlazor.Core/js/arcGisJsInterop.js");
}

return await proModule.InvokeAsync<IJSObjectReference>("getCore");
}

/// <summary>
/// Retrieves the main entry point for the optional GeoBlazor Pro JavaScript module.
/// </summary>
public static async Task<IJSObjectReference?> GetArcGisJsPro(IJSRuntime jsRuntime, CancellationToken cancellationToken)
{
LicenseType licenseType = Licensing.GetLicenseType();
IJSObjectReference jsModule;

switch ((int)licenseType)
{
case >= 100:
// this is here to support the pro extension library
IJSObjectReference proModule = await jsRuntime
.InvokeAsync<IJSObjectReference>("import",
"./_content/dymaptic.GeoBlazor.Pro/js/arcGisPro.js");
jsModule = await proModule.InvokeAsync<IJSObjectReference>("getCore");

break;

#if NET7_0_OR_GREATER
if (OperatingSystem.IsBrowser())
{
#pragma warning disable CA1416
await JSHost.ImportAsync("arcGisPro", "../_content/dymaptic.GeoBlazor.Pro/js/arcGisPro.js");
#pragma warning restore CA1416
}
#endif
return await jsRuntime.InvokeAsync<IJSObjectReference>("import", cancellationToken,
"./_content/dymaptic.GeoBlazor.Pro/js/arcGisPro.js");
default:
jsModule = await jsRuntime
.InvokeAsync<IJSObjectReference>("import",
"./_content/dymaptic.GeoBlazor.Core/js/arcGisJsInterop.js");

break;
return null;
}

return jsModule;
}
}
2 changes: 1 addition & 1 deletion src/dymaptic.GeoBlazor.Core/Scripts/arcGisJsInterop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1342,7 +1342,7 @@ export async function addGraphicsFromStream(streamRef: any, viewId: string, abor
}
}

export function addGraphicsSyncInterop(graphicsArray: Uint8Array, viewId: string, layerId?: string | null): void {
export function addGraphicsCoreSyncInterop(graphicsArray: Uint8Array, viewId: string, layerId?: string | null): void {
try {
let graphics = decodeProtobufGraphics(graphicsArray);
let jsGraphics: Graphic[] = [];
Expand Down

0 comments on commit 59a26ad

Please sign in to comment.