Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions Community.Blazor.MapLibre/MapLibre.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,13 +294,13 @@ public async Task<Listener> AddTerraDrawDeleteListener<T>(Action<T> handler)
return new Listener(callback);
}

public async Task<Listener> AddTerraDrawChangeListener<T>(Action<T> handler)
public async Task<Listener> AddTerraDrawChangeListener<T>(Action<T> 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);
}
Expand Down
32 changes: 28 additions & 4 deletions Community.Blazor.MapLibre/MapLibre.razor.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,18 +261,42 @@ 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);
}, throttleTime);

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.
*
Expand Down
1 change: 1 addition & 0 deletions Community.Blazor.MapLibre/Models/Feature/IFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Community.Blazor.MapLibre.Models.Feature;

[JsonPolymorphic(TypeDiscriminatorPropertyName = "$type")]
[JsonDerivedType(typeof(FeatureCollection))]
[JsonDerivedType(typeof(FeatureFeature))]
public interface IFeature
Expand Down
2 changes: 1 addition & 1 deletion Community.Blazor.MapLibre/Models/Feature/IGeometry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down