Skip to content

Commit

Permalink
Merge pull request Mehigh17#40 from chucker/fixes/39-key-already-exists
Browse files Browse the repository at this point in the history
Make LayerReferences a ConcurrentDictionary. Possibly fixes Mehigh17#39
  • Loading branch information
Mehigh17 committed Jun 21, 2020
2 parents 409d19a + 1f33f3a commit 95ecfe9
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions BlazorLeaflet/BlazorLeaflet/LeafletInterops.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.AspNetCore.Components.Web;
using Microsoft.JSInterop;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Drawing;
using System.Threading.Tasks;
Expand All @@ -12,8 +13,8 @@ namespace BlazorLeaflet
public static class LeafletInterops
{

private static Dictionary<string, (IDisposable, string, Layer)> LayerReferences { get; }
= new Dictionary<string, (IDisposable, string, Layer)>();
private static ConcurrentDictionary<string, (IDisposable, string, Layer)> LayerReferences { get; }
= new ConcurrentDictionary<string, (IDisposable, string, Layer)>();

private static readonly string _BaseObjectContainer = "window.leafletBlazor";

Expand All @@ -23,17 +24,14 @@ public static ValueTask Create(IJSRuntime jsRuntime, Map map) =>
private static DotNetObjectReference<T> CreateLayerReference<T>(string mapId, T layer) where T : Layer
{
var result = DotNetObjectReference.Create(layer);
LayerReferences.Add(layer.Id, (result, mapId, layer));
LayerReferences.TryAdd(layer.Id, (result, mapId, layer));
return result;
}

private static void DisposeLayerReference(string layerId)
{
if (LayerReferences.TryGetValue(layerId, out var value))
{
if (LayerReferences.TryRemove(layerId, out var value))
value.Item1.Dispose();
LayerReferences.Remove(layerId);
}
}

public static ValueTask AddLayer(IJSRuntime jsRuntime, string mapId, Layer layer)
Expand Down

0 comments on commit 95ecfe9

Please sign in to comment.