From e37862c896fff818eb5a6d2adc82e71bd3f9d7a2 Mon Sep 17 00:00:00 2001 From: Lars Arvidsson Date: Thu, 29 Jan 2026 16:28:02 +0100 Subject: [PATCH 1/3] GEOIN-142 Using a throttle to limit terra draw events --- Community.Blazor.MapLibre/MapLibre.razor.js | 30 ++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/Community.Blazor.MapLibre/MapLibre.razor.js b/Community.Blazor.MapLibre/MapLibre.razor.js index 7babb13..179dfb3 100644 --- a/Community.Blazor.MapLibre/MapLibre.razor.js +++ b/Community.Blazor.MapLibre/MapLibre.razor.js @@ -264,15 +264,39 @@ export function onTerraDrawDelete(container, dotnetReference) { export function onTerraDrawChange(container, dotnetReference) { const draw = drawControls[container]; + const throttledInvoke = throttle(() => { + const features = draw.getSnapshot(); + const featuresAsJson = JSON.stringify(features); + dotnetReference.invokeMethodAsync('Invoke', featuresAsJson); + }, 1000); + draw.on('change', (ids, type) => { if (type === 'create' || type === 'update' || type === 'delete') { - const features = draw.getSnapshot(); - const featuresAsJson = JSON.stringify(features); - dotnetReference.invokeMethodAsync('Invoke', featuresAsJson); + throttledInvoke(); } }); } +function throttle(func, intervalInMs) { + let lastFunc; + let lastRan; + + return function(...args) { + if (!lastRan) { + func.apply(this, args); + lastRan = Date.now(); + } else { + clearTimeout(lastFunc); + lastFunc = setTimeout(() => { + if ((Date.now() - lastRan) >= intervalInMs) { + func.apply(this, args); + lastRan = Date.now(); + } + }, intervalInMs - (Date.now() - lastRan)); + } + } +} + /** * Adds a scale control to the given map container. * From 92b857c5f967ebf8d7dff382ac54b395c11031b2 Mon Sep 17 00:00:00 2001 From: Lars Arvidsson Date: Thu, 29 Jan 2026 16:35:59 +0100 Subject: [PATCH 2/3] GEOIN-142 Made the throttle time configurable --- Community.Blazor.MapLibre/MapLibre.razor.cs | 4 ++-- Community.Blazor.MapLibre/MapLibre.razor.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Community.Blazor.MapLibre/MapLibre.razor.cs b/Community.Blazor.MapLibre/MapLibre.razor.cs index dbd2652..320ead8 100644 --- a/Community.Blazor.MapLibre/MapLibre.razor.cs +++ b/Community.Blazor.MapLibre/MapLibre.razor.cs @@ -294,13 +294,13 @@ public async Task AddTerraDrawDeleteListener(Action handler) return new Listener(callback); } - public async Task AddTerraDrawChangeListener(Action handler) + public async Task AddTerraDrawChangeListener(Action handler, int throttleTime) { var callback = new CallbackHandler(_jsModule, "change", handler, typeof(T)); var reference = DotNetObjectReference.Create(callback); _references.TryAdd(Guid.NewGuid(), reference); - await _jsModule.InvokeVoidAsync("onTerraDrawChange", MapId, reference); + await _jsModule.InvokeVoidAsync("onTerraDrawChange", MapId, reference, throttleTime); return new Listener(callback); } diff --git a/Community.Blazor.MapLibre/MapLibre.razor.js b/Community.Blazor.MapLibre/MapLibre.razor.js index 179dfb3..694f244 100644 --- a/Community.Blazor.MapLibre/MapLibre.razor.js +++ b/Community.Blazor.MapLibre/MapLibre.razor.js @@ -261,14 +261,14 @@ export function onTerraDrawDelete(container, dotnetReference) { }); } -export function onTerraDrawChange(container, dotnetReference) { +export function onTerraDrawChange(container, dotnetReference, throttleTime = 100) { const draw = drawControls[container]; const throttledInvoke = throttle(() => { const features = draw.getSnapshot(); const featuresAsJson = JSON.stringify(features); dotnetReference.invokeMethodAsync('Invoke', featuresAsJson); - }, 1000); + }, throttleTime); draw.on('change', (ids, type) => { if (type === 'create' || type === 'update' || type === 'delete') { From c71682fa84fbf82a8cd12c39ae4cb46efe1cc08c Mon Sep 17 00:00:00 2001 From: Lars Arvidsson Date: Thu, 5 Feb 2026 11:08:03 +0100 Subject: [PATCH 3/3] GEOIN-116 Fix of client exception when clicking on the map --- Community.Blazor.MapLibre/Models/Feature/IFeature.cs | 1 + Community.Blazor.MapLibre/Models/Feature/IGeometry.cs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Community.Blazor.MapLibre/Models/Feature/IFeature.cs b/Community.Blazor.MapLibre/Models/Feature/IFeature.cs index a734305..0be200f 100644 --- a/Community.Blazor.MapLibre/Models/Feature/IFeature.cs +++ b/Community.Blazor.MapLibre/Models/Feature/IFeature.cs @@ -2,6 +2,7 @@ namespace Community.Blazor.MapLibre.Models.Feature; +[JsonPolymorphic(TypeDiscriminatorPropertyName = "$type")] [JsonDerivedType(typeof(FeatureCollection))] [JsonDerivedType(typeof(FeatureFeature))] public interface IFeature diff --git a/Community.Blazor.MapLibre/Models/Feature/IGeometry.cs b/Community.Blazor.MapLibre/Models/Feature/IGeometry.cs index d871bfa..9580878 100644 --- a/Community.Blazor.MapLibre/Models/Feature/IGeometry.cs +++ b/Community.Blazor.MapLibre/Models/Feature/IGeometry.cs @@ -2,7 +2,7 @@ namespace Community.Blazor.MapLibre.Models.Feature; -[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")] +[JsonPolymorphic(TypeDiscriminatorPropertyName = "$type")] [JsonDerivedType(typeof(LineGeometry), typeDiscriminator: "LineString")] [JsonDerivedType(typeof(MultiLineGeometry), typeDiscriminator: "MultiLineString")] [JsonDerivedType(typeof(MultiPointGeometry), typeDiscriminator: "MultiPoint")]