Clean up fmMapSocket and use promises there

pull/54/merge
Candid Dauth 2016-10-20 12:11:04 +03:00
rodzic 7973d92510
commit f3859374b9
9 zmienionych plików z 231 dodań i 271 usunięć

Wyświetl plik

@ -22,12 +22,11 @@
return function(map) {
return {
exportGpx : function() {
map.socket.emit("exportGpx", { useTracks: true }, function(err, gpx) {
if(err)
return map.messages.showMessage("danger", err);
map.socket.emit("exportGpx", { useTracks: true }).then(function(gpx) {
saveAs(new Blob([ gpx ], { type: "application/gpx+xml" }), map.socket.padData.name.replace(/[\\\/:*?"<>|]+/g, '_') + '.gpx');
})
}).catch(function(err) {
map.messages.showMessage("danger", err);
});
}
};
};

Wyświetl plik

@ -262,9 +262,7 @@
addLine: function(type) {
map.map.closePopup();
map.socket.emit("getLineTemplate", { typeId: type.id }, function(err, line) {
if(err)
return map.messages.showMessage("danger", err);
map.socket.emit("getLineTemplate", { typeId: type.id }).then(function(line) {
line.routePoints = [ ];
line.trackPoints = [ ];
@ -308,13 +306,12 @@
};
handler = map.addClickListener(mapClick, mouseMove);
}).catch(function(err) {
map.messages.showMessage("danger", err);
});
},
createLine: function(type, routePoints, properties) {
map.socket.emit("addLine", $.extend({ routePoints: routePoints, typeId: type.id }, properties), function(err, line) {
if(err)
return map.messages.showMessage("danger", err);
map.socket.emit("addLine", $.extend({ routePoints: routePoints, typeId: type.id }, properties)).then(function(line) {
linesUi.editLine(line);
// We have to wait until the server sends us the trackPoints of the line
@ -324,6 +321,8 @@
removeWatcher();
}
});
}).catch(function(err) {
map.messages.showMessage("danger", err);
});
},
moveLine: function(line) {
@ -347,17 +346,15 @@
if(save) {
line.trackPoints = { };
map.socket.emit("editLine", { id: line.id, routePoints: newPoints }, function(err) {
if(err)
map.messages.showMessage("danger", err);
map.socket.emit("editLine", { id: line.id, routePoints: newPoints }).catch(function(err) {
map.messages.showMessage("danger", err);
});
}
}
},
deleteLine: function(line) {
map.socket.emit("deleteLine", line, function(err) {
if(err)
map.messages.showMessage("danger", err);
map.socket.emit("deleteLine", line).catch(function(err) {
map.messages.showMessage("danger", err);
});
}
};
@ -379,11 +376,10 @@
var lineObj = ng.copy($scope.line);
delete lineObj.trackPoints;
map.socket.emit("editLine", lineObj, function(err) {
if(err)
return $scope.error = err;
map.socket.emit("editLine", lineObj).then(function() {
$scope.$close();
}).catch(function(err) {
return $scope.error = err;
});
};

Wyświetl plik

@ -104,11 +104,10 @@
if(save) {
var pos = markersById[marker.id].getLatLng();
map.socket.emit("editMarker", { id: marker.id, lat: pos.lat, lon: pos.lng }, function(err) {
if(err)
return map.messages.showMessage("danger", err);
map.socket.emit("editMarker", { id: marker.id, lat: pos.lat, lon: pos.lng }).then(function() {
markersById[marker.id].openPopup();
}).catch(function(err) {
map.messages.showMessage("danger", err);
});
}
}
@ -123,9 +122,8 @@
markersById[marker.id].dragging.enable();
},
deleteMarker: function(marker) {
map.socket.emit("deleteMarker", marker, function(err) {
if(err)
map.messages.showMessage("danger", err);
map.socket.emit("deleteMarker", marker).catch(function(err) {
map.messages.showMessage("danger", err);
});
},
addMarker: function(type) {
@ -145,14 +143,13 @@
});
},
createMarker: function(pos, type, properties) {
map.socket.emit("addMarker", $.extend({ lon: pos.lon, lat: pos.lat, typeId: type.id }, properties), function(err, marker) {
if(err)
return map.messages.showMessage("danger", err);
map.socket.emit("addMarker", $.extend({ lon: pos.lon, lat: pos.lat, typeId: type.id }, properties)).then(function(marker) {
markersUi._addMarker(marker);
markersById[marker.id].openPopup();
markersUi.editMarker(marker);
}).catch(function(err) {
map.messages.showMessage("danger", err);
});
}
};
@ -170,11 +167,10 @@
$scope.save = function() {
$scope.error = null;
map.socket.emit("editMarker", $scope.marker, function(err) {
if(err)
return $scope.error = err;
map.socket.emit("editMarker", $scope.marker).then(function() {
$scope.$close();
}).catch(function(err) {
$scope.error = err;
});
};

Wyświetl plik

@ -85,20 +85,18 @@
};
if(create) {
map.socket.emit("createPad", newData, function(err) {
if(err)
return $scope.error = err;
map.socket.emit("createPad", newData).then(function() {
map.socket.updateBbox(fmUtils.leafletToFmBbox(map.map.getBounds(), map.map.getZoom()));
$scope.$close();
}).catch(function(err) {
$scope.error = err;
});
} else {
map.socket.emit("editPad", newData, function(err) {
if(err)
return $scope.error = err;
map.socket.emit("editPad", newData).then(function() {
$scope.$close();
}).catch(function(err) {
$scope.error = err;
});
}
};

Wyświetl plik

@ -41,33 +41,23 @@
};
scope.loadSuggestions = function(destination) {
return $q(function(resolve, reject) {
if(destination.suggestionQuery == destination.query)
return resolve();
if(destination.suggestionQuery == destination.query)
return $q.resolve();
if(destination.query.trim() != "") {
var query = destination.query;
if(destination.query.trim() != "") {
var query = destination.query;
map.loadStart();
map.socket.emit("find", { query: query }, function(err, results) {
map.loadEnd();
return map.socket.emit("find", { query: query }).then(function(results) {
if(fmUtils.isSearchId(query) && results.length > 0 && results[0].display_name)
destination.query = query = results[0].display_name;
if(err) {
map.messages.showMessage("danger", err);
return reject(err);
}
if(fmUtils.isSearchId(query) && results.length > 0 && results[0].display_name)
destination.query = query = results[0].display_name;
destination.suggestions = results;
destination.suggestionQuery = query;
destination.selectedSuggestionIdx = 0;
resolve();
});
}
});
destination.suggestions = results;
destination.suggestionQuery = query;
destination.selectedSuggestionIdx = 0;
}).catch(function(err) {
map.messages.showMessage("danger", err);
});
}
};
scope.route = function(dragging, noZoom) {
@ -85,11 +75,7 @@
return destination.suggestions[destination.selectedSuggestionIdx] || destination.suggestions[0];
});
return $q(function(resolve, reject) {
map.socket.emit("getRoute", { destinations: points.map(function(point) { return { lat: point.lat, lon: point.lon }; }), mode: mode }, function(err, res) {
err ? reject(err) : resolve(res);
});
});
return map.socket.emit("getRoute", { destinations: points.map(function(point) { return { lat: point.lat, lon: point.lon }; }), mode: mode });
}).then(function(route) {
route.routePoints = points;
route.routeMode = mode;

Wyświetl plik

@ -27,13 +27,7 @@
return map.map.flyTo([ lonlat.lat, lonlat.lon ], lonlat.zoom);
var q = scope.searchString;
map.loadStart();
map.socket.emit("find", { query: scope.searchString, loadUrls: true }, function(err, results) {
map.loadEnd();
if(err)
return map.messages.showMessage("danger", err);
map.socket.emit("find", { query: scope.searchString, loadUrls: true }).then(function(results) {
if(fmUtils.isSearchId(q) && results.length > 0 && results[0].display_name)
scope.searchString = q = results[0].display_name;
@ -45,6 +39,8 @@
loadSearchResults(parseFiles([ results ]), noZoom);
else
loadSearchResults(results, noZoom);
}).catch(function(err) {
map.messages.showMessage("danger", err);
});
}
};
@ -118,17 +114,13 @@
map.mapEvents.$on("longclick", function(e, latlng) {
clickMarker.clearLayers();
map.loadStart();
map.socket.emit("find", { query: "geo:" + latlng.lat + "," + latlng.lng + "?z=" + map.map.getZoom(), loadUrls: false }, function(err, results) {
map.loadEnd();
if(err)
return map.messages.showMessage("danger", err);
map.socket.emit("find", { query: "geo:" + latlng.lat + "," + latlng.lng + "?z=" + map.map.getZoom(), loadUrls: false }).then(function(results) {
clickMarker.clearLayers();
if(results.length > 0)
renderResult(fmUtils.round(latlng.lat, 5) + "," + fmUtils.round(latlng.lng, 5), results, results[0], true, clickMarker);
}).catch(function(err) {
map.messages.showMessage("danger", err);
});
});

Wyświetl plik

@ -79,9 +79,8 @@
$scope['delete'] = function(type) {
$scope.error = null;
map.socket.emit("deleteType", { id: type.id }, function(err) {
if(err)
$scope.error = err;
map.socket.emit("deleteType", { id: type.id }).catch(function(err) {
$scope.error = err;
});
};
});
@ -105,11 +104,10 @@
$scope.save = function() {
$scope.error = null;
map.socket.emit($scope.type.id == null ? "addType" : "editType", $scope.type, function(err) {
if(err)
return $scope.error = err;
map.socket.emit($scope.type.id == null ? "addType" : "editType", $scope.type).then(function() {
$scope.$close();
}).catch(function(err) {
$scope.error = err;
});
};
});

Wyświetl plik

@ -37,20 +37,13 @@
$scope.save = function(makeDefault) {
var view = map.getCurrentView();
view.name = $scope.name;
map.socket.emit("addView", view, function(err, view) {
if(err)
return $scope.error = err;
if(makeDefault) {
map.socket.emit("editPad", { defaultViewId: view.id }, function(err) {
if(err)
return $scope.error = err;
$scope.$close();
});
}
else
$scope.$close();
map.socket.emit("addView", view).then(function(view) {
if(makeDefault)
return map.socket.emit("editPad", { defaultViewId: view.id });
}).then(function() {
$scope.$close();
}).catch(function(err) {
$scope.error = err;
});
};
});
@ -61,16 +54,14 @@
};
$scope.makeDefault = function(view) {
map.socket.emit("editPad", { defaultViewId: view.id }, function(err) {
if(err)
$scope.error = err;
map.socket.emit("editPad", { defaultViewId: view.id }).catch(function(err) {
$scope.error = err;
});
};
$scope['delete'] = function(view) {
map.socket.emit("deleteView", { id: view.id }, function(err) {
if(err)
$scope.error = err;
map.socket.emit("deleteView", { id: view.id }).catch(function(err) {
$scope.error = err;
});
};
});

Wyświetl plik

@ -1,180 +1,184 @@
(function(fm, $, ng, undefined) {
// From http://stackoverflow.com/a/11277751/242365
fm.app.factory("fmSocket", function($rootScope) {
fm.app.factory("fmSocket", function($rootScope, $q) {
return function(padId) {
var scope = $rootScope.$new();
scope.padData = null;
scope.readonly = null;
scope.markers = { };
scope.lines = { };
scope.views = { };
scope.types = { };
var socket = io.connect(fm.SERVER, { 'force new connection': true });
scope.on = function(eventName, fn) {
if(fn)
fn = fn.fmWrapApply(scope);
var fmSocket = $rootScope.$new();
$.extend(fmSocket, {
padData: null,
readonly: null,
markers: { },
lines: { },
views: { },
types: { },
return socket.on.apply(socket, [ eventName, fn ]);
};
on : function(eventName, fn) {
if(fn)
fn = fn.fmWrapApply(fmSocket);
scope.removeListener = socket.removeListener.bind(socket);
return socket.on.apply(socket, [ eventName, fn ]);
},
scope.emit = function(eventName, data, cb) {
if(cb) {
scope.$emit("loadStart");
var cb2 = cb;
arguments[2] = function() {
scope.$emit("loadEnd");
var context = this;
var args = arguments;
scope.$apply(function() {
cb2.apply(context, args);
removeListener: socket.removeListener.bind(socket),
emit: function(eventName, data) {
return $q(function(resolve, reject) {
fmSocket.$emit("loadStart");
socket.emit(eventName, data, function(err, data) {
fmSocket.$emit("loadEnd");
if(err)
reject(err);
else
resolve(data);
});
};
}
});
},
return socket.emit.apply(socket, arguments);
};
scope.on("padData", function(data) {
setPadData(data);
});
scope.on("marker", function(data) {
if(scope.markers[data.id] == null)
scope.markers[data.id] = { };
scope.markers[data.id] = data;
});
scope.on("deleteMarker", function(data) {
delete scope.markers[data.id];
});
scope.on("line", function(data) {
if(scope.lines[data.id])
data.trackPoints = scope.lines[data.id].trackPoints;
else
scope.lines[data.id] = { };
scope.lines[data.id] = data;
});
scope.on("deleteLine", function(data) {
delete scope.lines[data.id];
});
scope.on("linePoints", function(data) {
var line = scope.lines[data.id];
if(line == null)
return console.error("Received line points for non-existing line "+data.id+".");
if(line.trackPoints == null || data.reset)
line.trackPoints = { };
for(var i=0; i<data.trackPoints.length; i++) {
line.trackPoints[data.trackPoints[i].idx] = data.trackPoints[i];
}
line.trackPoints.length = 0;
for(var i in line.trackPoints) {
if(i != "length" && i >= line.trackPoints.length)
line.trackPoints.length = 1*i+1;
}
});
scope.on("view", function(data) {
if(scope.views[data.id] == null)
scope.views[data.id] = { };
scope.views[data.id] = data;
});
scope.on("deleteView", function(data) {
delete scope.views[data.id];
if(scope.padData.defaultViewId == data.id)
scope.padData.defaultViewId = null;
});
scope.on("type", function(data) {
if(scope.types[data.id] == null)
scope.types[data.id] = { };
scope.types[data.id] = data;
});
scope.on("deleteType", function(data) {
delete scope.types[data.id];
});
scope.on("disconnect", function() {
scope.disconnected = true;
scope.markers = { };
scope.lines = { };
scope.views = { };
});
scope.on("reconnect", function() {
if(scope.padId)
setPadId(scope.padId);
else
scope.disconnected = false; // Otherwise it gets set when padData arrives
if(scope.bbox)
scope.emit("updateBbox", scope.bbox);
});
function setPadData(data) {
scope.padData = data;
if(data.writable != null)
scope.readonly = !data.writable;
var id = scope.readonly ? data.id : data.writeId;
if(id != null)
scope.padId = id;
}
scope.setPadId = function(padId) {
if(scope.padId != null)
return;
setPadId(padId);
};
function setPadId(padId) {
scope.padId = padId;
scope.emit("setPadId", padId, function(err, padData) {
if(err) {
scope.serverError = err;
socket.disconnect();
setPadId: function(padId) {
if(fmSocket.padId != null)
return;
return setPadId(padId);
},
updateBbox : function(bbox) {
fmSocket.bbox = bbox;
return fmSocket.emit("updateBbox", bbox);
}
});
var handlers = {
padData: function(data) {
setPadData(data);
},
marker: function(data) {
if(fmSocket.markers[data.id] == null)
fmSocket.markers[data.id] = { };
fmSocket.markers[data.id] = data;
},
deleteMarker: function(data) {
delete fmSocket.markers[data.id];
},
line: function(data) {
if(fmSocket.lines[data.id])
data.trackPoints = fmSocket.lines[data.id].trackPoints;
else
fmSocket.lines[data.id] = { };
fmSocket.lines[data.id] = data;
},
deleteLine: function(data) {
delete fmSocket.lines[data.id];
},
linePoints: function(data) {
var line = fmSocket.lines[data.id];
if(line == null)
return console.error("Received line points for non-existing line "+data.id+".");
if(line.trackPoints == null || data.reset)
line.trackPoints = { };
for(var i=0; i<data.trackPoints.length; i++) {
line.trackPoints[data.trackPoints[i].idx] = data.trackPoints[i];
}
setPadData(padData);
scope.disconnected = false;
line.trackPoints.length = 0;
for(var i in line.trackPoints) {
if(i != "length" && i >= line.trackPoints.length)
line.trackPoints.length = 1*i+1;
}
},
view: function(data) {
if(fmSocket.views[data.id] == null)
fmSocket.views[data.id] = { };
fmSocket.views[data.id] = data;
},
deleteView: function(data) {
delete fmSocket.views[data.id];
if(fmSocket.padData.defaultViewId == data.id)
fmSocket.padData.defaultViewId = null;
},
type: function(data) {
if(fmSocket.types[data.id] == null)
fmSocket.types[data.id] = { };
fmSocket.types[data.id] = data;
},
deleteType: function(data) {
delete fmSocket.types[data.id];
},
disconnect: function() {
fmSocket.disconnected = true;
fmSocket.markers = { };
fmSocket.lines = { };
fmSocket.views = { };
},
reconnect: function() {
if(fmSocket.padId)
setPadId(fmSocket.padId);
else
fmSocket.disconnected = false; // Otherwise it gets set when padData arrives
if(fmSocket.bbox)
fmSocket.emit("updateBbox", fmSocket.bbox);
}
};
for(var i in handlers)
fmSocket.on(i, handlers[i]);
if(padId) {
// Run with delay, so that loadStart event handler can register before
fmSocket.$applyAsync(function() {
fmSocket.setPadId(padId);
});
}
scope.updateBbox = function(bbox) {
scope.emit("updateBbox", bbox);
scope.bbox = bbox;
};
if(padId)
scope.setPadId(padId);
scope.$on("$destroy", function() {
fmSocket.$on("$destroy", function() {
socket.removeAllListeners();
socket.disconnect();
});
return scope;
function setPadData(data) {
fmSocket.padData = data;
if(data.writable != null)
fmSocket.readonly = !data.writable;
var id = fmSocket.readonly ? data.id : data.writeId;
if(id != null)
fmSocket.padId = id;
}
function setPadId(padId) {
fmSocket.padId = padId;
return fmSocket.emit("setPadId", padId).then(function(padData) {
setPadData(padData);
fmSocket.disconnected = false;
}).catch(function(err) {
fmSocket.serverError = err;
socket.disconnect();
});
}
return fmSocket;
};
});