diff --git a/src/dymaptic.GeoBlazor.Core/Components/Layers/GraphicsLayer.cs b/src/dymaptic.GeoBlazor.Core/Components/Layers/GraphicsLayer.cs index b8952633..fb7a51e3 100644 --- a/src/dymaptic.GeoBlazor.Core/Components/Layers/GraphicsLayer.cs +++ b/src/dymaptic.GeoBlazor.Core/Components/Layers/GraphicsLayer.cs @@ -144,7 +144,7 @@ public async Task Add(IEnumerable 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 diff --git a/src/dymaptic.GeoBlazor.Core/Components/Layers/Layer.cs b/src/dymaptic.GeoBlazor.Core/Components/Layers/Layer.cs index 24b49c40..9d5645f0 100644 --- a/src/dymaptic.GeoBlazor.Core/Components/Layers/Layer.cs +++ b/src/dymaptic.GeoBlazor.Core/Components/Layers/Layer.cs @@ -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("createProLayer", + JsLayerReference = await arcGisPro.InvokeAsync("createProLayer", // ReSharper disable once RedundantCast cancellationToken, (object)this, true, View?.Id); } diff --git a/src/dymaptic.GeoBlazor.Core/Components/MapComponent.razor.cs b/src/dymaptic.GeoBlazor.Core/Components/MapComponent.razor.cs index 14ece9b0..64a56247 100644 --- a/src/dymaptic.GeoBlazor.Core/Components/MapComponent.razor.cs +++ b/src/dymaptic.GeoBlazor.Core/Components/MapComponent.razor.cs @@ -327,46 +327,6 @@ protected virtual async Task RenderView(bool forceRender = false) } } - /// - /// Retrieves the main entry point for the JavaScript interop. - /// - protected async Task GetArcGisJsInterop() - { - LicenseType licenseType = Licensing.GetLicenseType(); - - switch ((int)licenseType) - { - case >= 100: - // this is here to support the pro extension library - IJSObjectReference proModule = await JsRuntime - .InvokeAsync("import", CancellationTokenSource.Token, - "./_content/dymaptic.GeoBlazor.Pro/js/arcGisPro.js"); - - return await proModule.InvokeAsync("getCore"); - default: - return await JsRuntime - .InvokeAsync("import", CancellationTokenSource.Token, - "./_content/dymaptic.GeoBlazor.Core/js/arcGisJsInterop.js"); - } - } - - /// - /// Retrieves the main entry point for the optional GeoBlazor Pro JavaScript module. - /// - protected async Task GetArcGisJsPro() - { - LicenseType licenseType = Licensing.GetLicenseType(); - - switch ((int)licenseType) - { - case >= 100: - return await JsRuntime.InvokeAsync("import", CancellationTokenSource.Token, - "./_content/dymaptic.GeoBlazor.Pro/js/arcGisPro.js"); - default: - return null; - } - } - private readonly Dictionary _watchers = new(); private readonly Dictionary _listeners = new(); private readonly Dictionary _waiters = new(); diff --git a/src/dymaptic.GeoBlazor.Core/Components/Views/MapView.razor.cs b/src/dymaptic.GeoBlazor.Core/Components/Views/MapView.razor.cs index 9a550684..37dfe921 100644 --- a/src/dymaptic.GeoBlazor.Core/Components/Views/MapView.razor.cs +++ b/src/dymaptic.GeoBlazor.Core/Components/Views/MapView.razor.cs @@ -1414,9 +1414,24 @@ public async Task AddGraphics(IEnumerable 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 @@ -2134,20 +2149,6 @@ protected override async Task OnParametersSetAsync() await UpdateView(); } - /// - 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 - } /// protected override async Task OnAfterRenderAsync(bool firstRender) @@ -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; diff --git a/src/dymaptic.GeoBlazor.Core/JsModuleManager.cs b/src/dymaptic.GeoBlazor.Core/JsModuleManager.cs index b7f41097..99a3bf13 100644 --- a/src/dymaptic.GeoBlazor.Core/JsModuleManager.cs +++ b/src/dymaptic.GeoBlazor.Core/JsModuleManager.cs @@ -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 GetJsModule(IJSRuntime jsRuntime) + public static async Task 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("import", cancellationToken, "./_content/dymaptic.GeoBlazor.Core/js/arcGisJsInterop.js"); + } + + return await proModule.InvokeAsync("getCore"); + } + + /// + /// Retrieves the main entry point for the optional GeoBlazor Pro JavaScript module. + /// + public static async Task 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("import", - "./_content/dymaptic.GeoBlazor.Pro/js/arcGisPro.js"); - jsModule = await proModule.InvokeAsync("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("import", cancellationToken, + "./_content/dymaptic.GeoBlazor.Pro/js/arcGisPro.js"); default: - jsModule = await jsRuntime - .InvokeAsync("import", - "./_content/dymaptic.GeoBlazor.Core/js/arcGisJsInterop.js"); - - break; + return null; } - - return jsModule; } } \ No newline at end of file diff --git a/src/dymaptic.GeoBlazor.Core/Scripts/arcGisJsInterop.ts b/src/dymaptic.GeoBlazor.Core/Scripts/arcGisJsInterop.ts index 24667059..4eccb6a5 100644 --- a/src/dymaptic.GeoBlazor.Core/Scripts/arcGisJsInterop.ts +++ b/src/dymaptic.GeoBlazor.Core/Scripts/arcGisJsInterop.ts @@ -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[] = [];