Skip to content

Commit

Permalink
Merge pull request #357 from Nickztar/managed_dispose
Browse files Browse the repository at this point in the history
Improve disposing of mapObjects by making sure that guidString is registered for objects
  • Loading branch information
valentasm1 authored Aug 28, 2024
2 parents bdc3125 + 6b93eca commit 77d976a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 29 deletions.
49 changes: 21 additions & 28 deletions GoogleMapsComponents/wwwroot/js/objectManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@
let controlParents = {};
const dateFormat = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/;


// Add the object to the map for "managed" objects, tries to set the guidString to
// be able to dispose of them later on
function addMapObject(uuid, obj) {
if ("set" in obj) {
obj.set("guidString", uuid);
}
mapObjects[uuid] = obj;
}

//Strip circular dependencies, map object and functions
//https://stackoverflow.com/questions/11616630/how-can-i-print-a-circular-structure-in-a-json-like-format
const getCircularReplacer = () => {
Expand Down Expand Up @@ -345,12 +355,7 @@
let constructor = stringToFunction(functionName);
let obj = new constructor(...args2);
let guid = args[0];

if ("set" in obj) {
obj.set("guidString", guid);
}

mapObjects[guid] = obj;
addMapObject(guid, obj)
},

//Used to create multiple objects of the same type passing a set of creation parameters coherent
Expand All @@ -375,12 +380,7 @@
}

let obj = new constructor(constructorArgs);

if ("set" in obj) {
obj.set("guidString", guids[i]);
}

mapObjects[guids[i]] = obj;
addMapObject(guids[i], obj);
}
},

Expand All @@ -392,7 +392,7 @@
}

mapObjects = mapObjects || [];
mapObjects[guid] = obj;
addMapObject(guid, obj);

return guid;
},
Expand Down Expand Up @@ -605,12 +605,12 @@

case "getProjection":
const projection = obj[functionToInvoke](...formattedArgs);
mapObjects[restArgs[0]] = projection;
addMapObject(restArgs[0], projection);
return;

case "createPath":
const pathProjection = obj.getPath();
mapObjects[restArgs[0]] = pathProjection;
addMapObject(restArgs[0], pathProjection);
return;

case "fromLatLngToPoint":
Expand Down Expand Up @@ -711,13 +711,11 @@

return results;
},

invokeWithReturnedObjectRef: async function (args) {
invokeWithReturnedObjectRef: async function (args) {
const result = await blazorGoogleMaps.objectManager.invoke(args);
const uuid = uuidv4();

// This is needed to be able to remove events from map
mapObjects[uuid] = result;
addMapObject(uuid, result)

return uuid;
},
Expand All @@ -727,7 +725,7 @@

google.maps.event.addListener(drawingManager, "overlaycomplete", event => {
const overlayUuid = uuidv4();
mapObjects[overlayUuid] = event.overlay;
addMapObject(overlayUuid, event.overlay)

const returnObj = extendableStringify([{ type: event.type, uuid: overlayUuid.toString() }]);

Expand Down Expand Up @@ -784,8 +782,8 @@
const obj = mapObjects[objectId];
const result = obj?.[property];
const uuid = uuidv4();

mapObjects[uuid] = result;
addMapObject(uuid, result)

return uuid;
},
Expand Down Expand Up @@ -863,12 +861,7 @@
markers: newMarkers
});
*/

if ("set" in markerCluster) {
markerCluster.set("guidString", guid);
}

mapObjects[guid] = markerCluster;
addMapObject(guid, markerCluster)
},

removeClusteringMarkers(guid, markers, noDraw) {
Expand Down
1 change: 1 addition & 0 deletions ServerSideDemo/Pages/MapMarker.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@page "/mapMarker"
@using GoogleMapsComponents
@implements IDisposable

<h1>Google Map Markers</h1>

Expand Down
6 changes: 5 additions & 1 deletion ServerSideDemo/Pages/MapMarker.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,5 +349,9 @@ private async Task FitBounds()
await _map1.InteropObject.FitBounds(boundsLiteral, OneOf.OneOf<int, Padding>.FromT0(5));
}


public void Dispose()
{
// Just to show that _bounds can be removed, but has be done manually since it doesn't relate to the map
_map1.Dispose();
}
}

0 comments on commit 77d976a

Please sign in to comment.