(JS) update: Prevent executing the remove method after the instance is removed
(Why JS developers do not manage the instance by themselves!)
MODIFY
www/Circle.js
www/Circle.js
@@ -144,12 +144,15 @@ Circle.prototype.setRadius = function(radius) {
Circle.prototype.remove = function() {
var self = this;
- self.trigger(self.id + "_remove");
- exec.call(self, null, self.errorHandler, self.getPluginName(), 'remove', [self.getId()]);
+ if (self._isRemoved) {
+ return;
+ }
Object.defineProperty(self, "_isRemoved", {
value: true,
writable: false
});
+ self.trigger(self.id + "_remove");
+ exec.call(self, null, self.errorHandler, self.getPluginName(), 'remove', [self.getId()]);
self.destroy();
};

MODIFY
www/GroundOverlay.js
www/GroundOverlay.js
@@ -147,12 +147,15 @@ GroundOverlay.prototype.getClickable = function() {
GroundOverlay.prototype.remove = function() {
var self = this;
- self.trigger(self.id + "_remove");
- exec.call(self, null, self.errorHandler, self.getPluginName(), 'remove', [self.getId()]);
+ if (self._isRemoved) {
+ return;
+ }
Object.defineProperty(self, "_isRemoved", {
value: true,
writable: false
});
+ self.trigger(self.id + "_remove");
+ exec.call(self, null, self.errorHandler, self.getPluginName(), 'remove', [self.getId()]);
self.destroy();
};

MODIFY
www/Map.js
www/Map.js
@@ -249,6 +249,9 @@ Map.prototype.panBy = function(x, y) {
*/
Map.prototype.clear = function(callback) {
var self = this;
+ if (self._isRemoved) {
+ return;
+ }
// Close the active infoWindow
var active_marker_id = self.get("active_marker_id");
@@ -517,6 +520,13 @@ Map.prototype.getCameraPosition = function() {
*/
Map.prototype.remove = function(callback) {
var self = this;
+ if (self._isRemoved) {
+ return;
+ }
+ Object.defineProperty(self, "_isRemoved", {
+ value: true,
+ writable: false
+ });
var div = this.get('div');
if (div) {
while (div) {
@@ -568,11 +578,6 @@ Map.prototype.remove = function(callback) {
callback.call(self);
}
}, self.errorHandler, 'CordovaGoogleMaps', 'removeMap', [self.id], {sync: true});
-
- Object.defineProperty(self, "_isRemoved", {
- value: true,
- writable: false
- });
};

MODIFY
www/Marker.js
www/Marker.js
@@ -107,6 +107,13 @@ utils.extend(Marker, BaseClass);
Marker.prototype.remove = function(callback) {
var self = this;
+ if (self._isRemoved) {
+ return;
+ }
+ Object.defineProperty(self, "_isRemoved", {
+ value: true,
+ writable: false
+ });
self.trigger(event.INFO_CLOSE); // close open infowindow, otherwise it will stay
self.trigger(self.id + "_remove");
exec.call(self, function() {
@@ -116,10 +123,6 @@ Marker.prototype.remove = function(callback) {
}
}, self.errorHandler, self.getPluginName(), 'remove', [self.getId()]);
- Object.defineProperty(self, "_isRemoved", {
- value: true,
- writable: false
- });
self.destroy();
};

MODIFY
www/MarkerCluster.js
www/MarkerCluster.js
@@ -91,7 +91,6 @@ var MarkerCluster = function(map, markerClusterId, markerClusterOptions, _exec)
}
self.taskQueue = [];
self._stopRequest = false;
- self._isRemove = false;
self._isWorking = false;
//----------------------------------------------------
@@ -187,7 +186,7 @@ var MarkerCluster = function(map, markerClusterId, markerClusterOptions, _exec)
self.on("nextTask", function(){
self._isWorking = false;
if (self._stopRequest ||
- self._isRemove || self.taskQueue.length === 0) {
+ self._isRemoved || self.taskQueue.length === 0) {
return;
}
sel.redraw.call(self);
@@ -220,7 +219,7 @@ MarkerCluster.prototype.getHashCode = function() {
};
MarkerCluster.prototype.onClusterClicked = function(cluster) {
- if (this._isRemove) {
+ if (this._isRemoved) {
return null;
}
var self = this;
@@ -250,7 +249,7 @@ MarkerCluster.prototype.onClusterClicked = function(cluster) {
MarkerCluster.prototype._onCameraMoved = function() {
var self = this;
- if (self._isRemove || self._stopRequest) {
+ if (self._isRemoved || self._stopRequest) {
return null;
}
@@ -263,7 +262,7 @@ MarkerCluster.prototype._onCameraMoved = function() {
MarkerCluster.prototype.remove = function() {
var self = this;
self._stopRequest = self.hashCode;
- if (self._isRemove) {
+ if (self._isRemoved) {
return;
}
if (self.debug) {
@@ -284,7 +283,10 @@ MarkerCluster.prototype.remove = function() {
self.trigger("remove");
self.taskQueue = [];
- self._isRemove = true;
+ Object.defineProperty(self, "_isRemoved", {
+ value: true,
+ writable: false
+ });
if (resolution === self.OUT_OF_RESOLUTION) {
while (self._clusters[resolution].length > 0) {
@@ -326,10 +328,6 @@ MarkerCluster.prototype.remove = function() {
});
}
exec.call(self, null, self.errorHandler, self.getPluginName(), 'remove', [self.getId()], {sync: true});
- Object.defineProperty(self, "_isRemoved", {
- value: true,
- writable: false
- });
keys = Object.keys(self._markerMap);
keys.forEach(function(markerId) {
@@ -343,7 +341,7 @@ MarkerCluster.prototype.remove = function() {
};
MarkerCluster.prototype.removeMarkerById = function(markerId) {
var self = this;
- if (self._isRemove) {
+ if (self._isRemoved) {
return null;
}
//if (markerId.indexOf(self.id + "-") === -1) {
@@ -385,7 +383,7 @@ MarkerCluster.prototype.removeMarkerById = function(markerId) {
};
MarkerCluster.prototype.getMarkerById = function(markerId) {
var self = this;
- if (self._isRemove) {
+ if (self._isRemoved) {
return null;
}
//if (markerId.indexOf(self.id + "-") === -1) {
@@ -406,7 +404,7 @@ MarkerCluster.prototype.getMarkerById = function(markerId) {
MarkerCluster.prototype.getClusterByClusterId = function(clusterId) {
var self = this;
- if (self._isRemove) {
+ if (self._isRemoved) {
return null;
}
var resolution = self.get("resolution");
@@ -422,7 +420,7 @@ MarkerCluster.prototype.getClusterByClusterId = function(clusterId) {
MarkerCluster.prototype.redraw = function(params) {
var self = this;
- if (self._isRemove || self._stopRequest) {
+ if (self._isRemoved || self._stopRequest) {
return null;
}
@@ -430,7 +428,7 @@ MarkerCluster.prototype.redraw = function(params) {
if (self.debug) {
console.log("self.taskQueue.push = " + self.taskQueue.length);
}
- if (self._isRemove || self._stopRequest || self.taskQueue.length > 1) {
+ if (self._isRemoved || self._stopRequest || self.taskQueue.length > 1) {
return;
}
if (self.debug) {
@@ -461,7 +459,7 @@ MarkerCluster.prototype.redraw = function(params) {
MarkerCluster.prototype._redraw = function(params) {
var self = this;
- if (self._isRemove || self._stopRequest || self._isWorking) {
+ if (self._isRemoved || self._stopRequest || self._isWorking) {
return null;
}
self._isWorking = true;
@@ -519,7 +517,7 @@ MarkerCluster.prototype._redraw = function(params) {
var targetMarkers = [];
- if (self._isRemove || self._stopRequest) {
+ if (self._isRemoved || self._stopRequest) {
self._isWorking = false;
return;
}
@@ -566,7 +564,7 @@ MarkerCluster.prototype._redraw = function(params) {
keys = Object.keys(self._markerMap);
keys.forEach(function(markerId) {
var markerOpts = self._markerMap[markerId];
- if (self._isRemove ||
+ if (self._isRemoved ||
self._stopRequest ||
markerOpts._cluster.isRemoved ||
markerOpts._cluster.isAdded) {
@@ -613,7 +611,7 @@ MarkerCluster.prototype._redraw = function(params) {
keys = Object.keys(self._markerMap);
keys.forEach(function(markerId) {
var markerOpts = self._markerMap[markerId];
- if (self._isRemove ||
+ if (self._isRemoved ||
self._stopRequest ||
markerOpts._cluster.isRemoved) {
return;
@@ -646,14 +644,14 @@ MarkerCluster.prototype._redraw = function(params) {
keys = Object.keys(self._clusters[prevResolution]);
keys.forEach(function(geocell) {
- if (self._isRemove || self._stopRequest) {
+ if (self._isRemoved || self._stopRequest) {
return;
}
var cluster = self._clusters[prevResolution][geocell];
var bounds = cluster.getBounds();
- if (!self._isRemove &&
+ if (!self._isRemoved &&
!expandedRegion.contains(bounds.northeast) &&
!expandedRegion.contains(bounds.southwest)) {
ignoreGeocells.push(geocell);
@@ -689,7 +687,7 @@ MarkerCluster.prototype._redraw = function(params) {
keys.forEach(function(markerId) {
var markerOpts = self._markerMap[markerId];
var geocell = markerOpts._cluster.geocell.substr(0, cellLen);
- if (self._isRemove ||
+ if (self._isRemoved ||
self._stopRequest ||
markerOpts._cluster.isRemoved ||
ignoreGeocells.indexOf(geocell) > -1 ||
@@ -720,13 +718,13 @@ MarkerCluster.prototype._redraw = function(params) {
//--------------
keys = Object.keys(self._clusters[prevResolution]);
keys.forEach(function(geocell) {
- if (self._isRemove) {
+ if (self._isRemoved) {
return;
}
var cluster = self._clusters[prevResolution][geocell];
var noClusterMode = cluster.getMode() === cluster.NO_CLUSTER_MODE;
cluster.getMarkers().forEach(function(markerOpts, idx) {
- if (self._isRemove ||
+ if (self._isRemoved ||
self._stopRequest) {
return;
}
@@ -763,7 +761,7 @@ MarkerCluster.prototype._redraw = function(params) {
keys = Object.keys(self._clusters[prevResolution]);
keys.forEach(function(geocell) {
if (self._stopRequest ||
- self._isRemove) {
+ self._isRemoved) {
return;
}
var cluster = self._clusters[prevResolution][geocell];
@@ -801,7 +799,7 @@ MarkerCluster.prototype._redraw = function(params) {
keys = Object.keys(self._markerMap);
keys.forEach(function(markerId) {
if (self._stopRequest ||
- self._isRemove) {
+ self._isRemoved) {
return;
}
var markerOpts = self._markerMap[markerId];
@@ -834,7 +832,7 @@ MarkerCluster.prototype._redraw = function(params) {
keys = Object.keys(self._markerMap);
keys.forEach(function(markerId) {
if (self._stopRequest ||
- self._isRemove) {
+ self._isRemoved) {
return;
}
var markerOpts = self._markerMap[markerId];
@@ -858,7 +856,7 @@ MarkerCluster.prototype._redraw = function(params) {
}
if (self._stopRequest ||
- self._isRemove) {
+ self._isRemoved) {
self._isWorking = false;
return;
}
@@ -1043,7 +1041,7 @@ MarkerCluster.prototype._redraw = function(params) {
}
if (self._stopRequest ||
- self._isRemove) {
+ self._isRemoved) {
self._isWorking = false;
return;
}

MODIFY
www/Polygon.js
www/Polygon.js
@@ -128,12 +128,15 @@ utils.extend(Polygon, BaseClass);
Polygon.prototype.remove = function() {
var self = self;
- self.trigger(this.id + "_remove");
- exec.call(self, null, self.errorHandler, self.getPluginName(), 'remove', [self.getId()]);
+ if (self._isRemoved) {
+ return;
+ }
Object.defineProperty(self, "_isRemoved", {
value: true,
writable: false
});
+ self.trigger(this.id + "_remove");
+ exec.call(self, null, self.errorHandler, self.getPluginName(), 'remove', [self.getId()]);
self.destroy();
};
Polygon.prototype.getPluginName = function() {

MODIFY
www/Polyline.js
www/Polyline.js
@@ -167,16 +167,19 @@ Polyline.prototype.getMap = function() {
Polyline.prototype.remove = function() {
var self = this;
+ if (self._isRemoved) {
+ return;
+ }
+ Object.defineProperty(self, "_isRemoved", {
+ value: true,
+ writable: false
+ });
exec.call(self, null, self.errorHandler, self.getPluginName(), 'remove', [self.getId()]);
self.trigger(self.id + "_remove");
var points = self.get("points");
if (points) {
points.clear();
}
- Object.defineProperty(self, "_isRemoved", {
- value: true,
- writable: false
- });
self.destroy();
};
module.exports = Polyline;

MODIFY
www/TileOverlay.js
www/TileOverlay.js
@@ -108,12 +108,15 @@ TileOverlay.prototype.getVisible = function() {
TileOverlay.prototype.remove = function() {
var self = this;
- self.trigger(self.id + "_remove");
- exec.call(self, null, self.errorHandler, self.getPluginName(), 'remove', [self.getId()]);
+ if (self._isRemoved) {
+ return;
+ }
Object.defineProperty(self, "_isRemoved", {
value: true,
writable: false
});
+ self.trigger(self.id + "_remove");
+ exec.call(self, null, self.errorHandler, self.getPluginName(), 'remove', [self.getId()]);
self.destroy();
};

(JS) update: Prevent executing the remove method after the instance is removed

(Why JS developers do not manage the instance by themselves!)