Update: performance improve of event listeners
MODIFY
plugin.xml
plugin.xml
<?xml version='1.0' encoding='utf-8'?>
-<plugin id="cordova-plugin-googlemaps" version="2.0.0-beta2-20170716-1535" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
+<plugin id="cordova-plugin-googlemaps" version="2.0.0-beta2-20170716-2155" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<name>cordova-plugin-googlemaps</name>
<js-module name="BaseClass" src="www/BaseClass.js">
<runs />

MODIFY
www/BaseClass.js
www/BaseClass.js
+
+var _globalListeners = {};
+
+function globalEventListener(e) {
+ var eventName = e.type;
+ if (!_globalListeners[eventName]) {
+ return;
+ }
+
+ var hashCode = e.myself.hashCode;
+ if (!hashCode || !_globalListeners[eventName][hashCode]) {
+ return;
+ }
+ var callbacks = _globalListeners[eventName][hashCode];
+ for (var i = 0; i < callbacks.length; i++) {
+ callbacks[i].listener(e);
+ }
+}
+
var BaseClass = function() {
var self = this;
var _vars = {};
- var _listeners = {};
+ var hashCode = Math.floor(Date.now() * Math.random());
+
+ Object.defineProperty(self, "hashCode", {
+ value: hashCode,
+ writable: false
+ });
self.empty = function() {
for (var key in Object.keys(_vars)) {
_vars[key] = null;
@@ -28,9 +52,10 @@ var BaseClass = function() {
};
self.trigger = function(eventName) {
- if (!eventName) {
+ if (!eventName || !_globalListeners[eventName] || !_globalListeners[eventName][hashCode]) {
return;
}
+
var args = [];
for (var i = 1; i < arguments.length; i++) {
args.push(arguments[i]);
@@ -39,13 +64,18 @@ var BaseClass = function() {
event.initEvent(eventName, false, false);
event.mydata = args;
event.myself = self;
- document.dispatchEvent(event);
+
+ var callbacks = _globalListeners[eventName][hashCode];
+ for (var i = 0; i < callbacks.length; i++) {
+ callbacks[i].listener(event);
+ }
};
self.on = function(eventName, callback) {
if (!eventName || !callback || typeof callback !== "function") {
return;
}
- _listeners[eventName] = _listeners[eventName] || [];
+
+ _globalListeners[eventName] = _globalListeners[eventName] || {};
var listener = function(e) {
if (!e.myself || e.myself !== self) {
@@ -58,8 +88,11 @@ var BaseClass = function() {
self.event = evt;
callback.apply(self, mydata);
};
- document.addEventListener(eventName, listener, false);
- _listeners[eventName].push({
+ if (!(hashCode in _globalListeners[eventName])) {
+ document.addEventListener(eventName, globalEventListener, false);
+ _globalListeners[eventName][hashCode] = [];
+ }
+ _globalListeners[eventName][hashCode].push({
'callback': callback,
'listener': listener
});
@@ -67,36 +100,38 @@ var BaseClass = function() {
self.addEventListener = self.on;
self.off = function(eventName, callback) {
- var i;
+ var i, j, callbacks;
if (typeof eventName === "string") {
- if (eventName in _listeners) {
+ if (eventName in _globalListeners) {
if (typeof callback === "function") {
- for (i = 0; i < _listeners[eventName].length; i++) {
- if (_listeners[eventName][i].callback === callback) {
- document.removeEventListener(eventName, _listeners[eventName][i].listener);
- _listeners[eventName].splice(i, 1);
+ callbacks = _globalListeners[eventName][hashCode] ||[];
+ for (i = 0; i < callbacks.length; i++) {
+ if (callbacks[i].callback === callback) {
+ callbacks.splice(i, 1);
break;
}
}
+ if (callbacks.length === 0) {
+ delete _globalListeners[eventName][hashCode];
+ }
} else {
- for (i = 0; i < _listeners[eventName].length; i++) {
- document.removeEventListener(eventName, _listeners[eventName][i].listener);
+ delete _globalListeners[eventName][hashCode];
}
- delete _listeners[eventName];
+ if (Object.keys(_globalListeners[eventName]) === 0) {
+ document.removeEventListener(eventName, globalEventListener);
}
}
} else {
//Remove all event listeners
- var eventNames = Object.keys(_listeners);
- for (i = 0; i < eventNames.length; i++) {
- eventName = eventNames[i];
- for (var j = 0; j < _listeners[eventName].length; j++) {
- document.removeEventListener(eventName, _listeners[eventName][j].listener);
+ var eventNames = Object.keys(_globalListeners);
+ for (j = 0; j < eventNames.length; j++) {
+ eventName = eventNames[j];
+ delete _globalListeners[eventName][hashCode];
+ if (Object.keys(_globalListeners[eventName]) === 0) {
+ document.removeEventListener(eventName, globalEventListener);
}
- delete _listeners[eventName];
}
- _listeners = {};
}
};
@@ -104,7 +139,6 @@ var BaseClass = function() {
self.one = function(eventName, callback) {
- _listeners[eventName] = _listeners[eventName] || [];
var listener = function(e) {
if (!e.myself || e.myself !== self) {
@@ -118,8 +152,14 @@ var BaseClass = function() {
callback.apply(self, mydata);
self.off(eventName, callback);
};
- document.addEventListener(eventName, listener, false);
- _listeners[eventName].push({
+
+ _globalListeners[eventName] = _globalListeners[eventName] || {};
+
+ if (!(hashCode in _globalListeners[eventName])) {
+ document.addEventListener(eventName, globalEventListener, false);
+ _globalListeners[eventName][hashCode] = [];
+ }
+ _globalListeners[eventName][hashCode].push({
'callback': callback,
'listener': listener
});

MODIFY
www/Circle.js
www/Circle.js
@@ -24,10 +24,6 @@ var Circle = function(map, circleId, circleOptions) {
value: "Circle",
writable: false
});
- Object.defineProperty(self, "hashCode", {
- value: circleOptions.hashCode,
- writable: false
- });
//-----------------------------------------------
// Sets the initialize option to each property

MODIFY
www/GroundOverlay.js
www/GroundOverlay.js
@@ -29,10 +29,6 @@ var GroundOverlay = function(map, groundOverlayId, groundOverlayOptions) {
value: map,
writable: false
});
- Object.defineProperty(self, "hashCode", {
- value: groundOverlayOptions.hashCode,
- writable: false
- });
var ignores = ["map", "id", "hashCode", "type"];
for (var key in groundOverlayOptions) {
if (ignores.indexOf(key) === -1) {

MODIFY
www/KmlOverlay.js
www/KmlOverlay.js
@@ -26,10 +26,6 @@ var KmlOverlay = function(map, kmlOverlayId, kmlOverlayOptions) {
value: map,
writable: false
});
- Object.defineProperty(self, "hashCode", {
- value: kmlOverlayOptions.hashCode,
- writable: false
- });
var ignores = ["map", "id", "hashCode", "type"];
for (var key in kmlOverlayOptions) {
if (ignores.indexOf(key) === -1) {

MODIFY
www/Map.js
www/Map.js
@@ -728,7 +728,6 @@ Map.prototype.addGroundOverlay = function(groundOverlayOptions, callback) {
groundOverlayOptions.bounds = common.convertToPositionArray(groundOverlayOptions.bounds);
exec(function(result) {
- groundOverlayOptions.hashCode = result.hashCode;
var groundOverlay = new GroundOverlay(self, result.id, groundOverlayOptions);
self.OVERLAYS[result.id] = groundOverlay;
groundOverlay.one(result.id + "_remove", function() {
@@ -785,7 +784,6 @@ Map.prototype.addTileOverlay = function(tilelayerOptions, callback) {
});
exec(function(result) {
- tilelayerOptions.hashCode = result.hashCode;
var tileOverlay = new TileOverlay(self, result.id, tilelayerOptions);
self.OVERLAYS[result.id] = tileOverlay;
tileOverlay.one(result.id + "_remove", function() {
@@ -830,7 +828,6 @@ Map.prototype.addPolygon = function(polygonOptions, callback) {
polygonOptions.geodesic = polygonOptions.geodesic === true;
exec(function(result) {
- polygonOptions.hashCode = result.hashCode;
polygonOptions.points = _orgs;
var polygon = new Polygon(self, result.id, polygonOptions);
self.OVERLAYS[result.id] = polygon;
@@ -861,7 +858,6 @@ Map.prototype.addPolyline = function(polylineOptions, callback) {
polylineOptions.geodesic = polylineOptions.geodesic === true;
exec(function(result) {
polylineOptions.points = _orgs;
- polylineOptions.hashCode = result.hashCode;
var polyline = new Polyline(self, result.id, polylineOptions);
self.OVERLAYS[result.id] = polyline;
polyline.one(result.id + "_remove", function() {
@@ -891,7 +887,6 @@ Map.prototype.addCircle = function(circleOptions, callback) {
circleOptions.radius = circleOptions.radius || 1;
exec(function(result) {
- circleOptions.hashCode = result.hashCode;
var circle = new Circle(self, result.id, circleOptions);
self.OVERLAYS[result.id] = circle;
@@ -939,7 +934,6 @@ Map.prototype.addMarker = function(markerOptions, callback) {
}
exec(function(result) {
- markerOptions.hashCode = result.hashCode;
var marker = new Marker(self, result.id, markerOptions);
self.MARKERS[result.id] = marker;
@@ -982,10 +976,9 @@ Map.prototype._onMarkerEvent = function(eventName, markerId, position) {
}
};
-
-Map.prototype._onOverlayEvent = function(eventName, hashCode) {
+Map.prototype._onOverlayEvent = function(eventName, overlayId) {
var self = this;
- var overlay = self.OVERLAYS[hashCode] || null;
+ var overlay = self.OVERLAYS[overlayId] || null;
if (overlay) {
var args = [eventName];
for (var i = 2; i < arguments.length; i++) {
@@ -995,6 +988,7 @@ Map.prototype._onOverlayEvent = function(eventName, hashCode) {
}
};
+/*
Map.prototype._onKmlEvent = function(eventName, objectType, kmlLayerId, result, options) {
var self = this;
var kmlLayer = self.KML_LAYERS[kmlLayerId] || null;
@@ -1061,7 +1055,7 @@ Map.prototype._onKmlEvent = function(eventName, objectType, kmlLayerId, result,
}
//kmlLayer.trigger.apply(kmlLayer, args);
};
-
+*/
Map.prototype.getCameraTarget = function() {
return this.get("camera_target");

MODIFY
www/Marker.js
www/Marker.js
@@ -18,10 +18,6 @@ var Marker = function(map, id, markerOptions) {
value: map,
writable: false
});
- Object.defineProperty(self, "hashCode", {
- value: markerOptions.hashCode,
- writable: false
- });
Object.defineProperty(self, "id", {
value: id,
writable: false

MODIFY
www/Polygon.js
www/Polygon.js
@@ -16,10 +16,6 @@ var Polygon = function(map, polygonId, polygonOptions) {
value: map,
writable: false
});
- Object.defineProperty(self, "hashCode", {
- value: polygonOptions.hashCode,
- writable: false
- });
Object.defineProperty(self, "id", {
value: polygonId,
writable: false

MODIFY
www/Polyline.js
www/Polyline.js
@@ -16,10 +16,6 @@ var Polyline = function(map, polylineId, polylineOptions) {
value: map,
writable: false
});
- Object.defineProperty(self, "hashCode", {
- value: polylineOptions.hashCode,
- writable: false
- });
Object.defineProperty(self, "id", {
value: polylineId,
writable: false

MODIFY
www/TileOverlay.js
www/TileOverlay.js
@@ -23,10 +23,6 @@ var TileOverlay = function(map, tileOverlayId, tileOverlayOptions) {
value: map,
writable: false
});
- Object.defineProperty(self, "hashCode", {
- value: tileOverlayOptions.hashCode,
- writable: false
- });
//-----------------------------------------------
// Sets the initialize option to each property
//-----------------------------------------------

Update: performance improve of event listeners