Skip to content

Commit a5ec699

Browse files
author
Per Liedman
committed
Abort ongoing request before starting a new; close #292
1 parent 777f912 commit a5ec699

File tree

3 files changed

+33
-25
lines changed

3 files changed

+33
-25
lines changed

src/L.Routing.Control.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@
270270
routes = routes.slice();
271271
var selected = routes.splice(this._selectedRoute.routesIndex, 1)[0];
272272
this._updateLines({route: selected, alternatives: routes });
273-
} else {
273+
} else if (err.type !== 'abort') {
274274
this._clearLines();
275275
}
276276
},
@@ -279,6 +279,11 @@
279279
var ts = ++this._requestCount,
280280
wps;
281281

282+
if (this._pendingRequest && this._pendingRequest.abort) {
283+
this._pendingRequest.abort();
284+
this._pendingRequest = null;
285+
}
286+
282287
options = options || {};
283288

284289
if (this._plan.isReady()) {
@@ -288,15 +293,21 @@
288293

289294
wps = options && options.waypoints || this._plan.getWaypoints();
290295
this.fire('routingstart', {waypoints: wps});
291-
this._router.route(wps, options.callback || function(err, routes) {
296+
this._pendingRequest = this._router.route(wps, function(err, routes) {
297+
this._pendingRequest = null;
298+
299+
if (options.callback) {
300+
return options.callback.call(this, err, routes);
301+
}
302+
292303
// Prevent race among multiple requests,
293-
// by checking the current request's timestamp
304+
// by checking the current request's count
294305
// against the last request's; ignore result if
295-
// this isn't the latest request.
306+
// this isn't the last request.
296307
if (ts === this._requestCount) {
297308
this._clearLines();
298309
this._clearAlts();
299-
if (err) {
310+
if (err && err.type !== 'abort') {
300311
this.fire('routingerror', {error: err});
301312
return;
302313
}

src/L.Routing.OSRM.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
wps.push(new L.Routing.Waypoint(wp.latLng, wp.name, wp.options));
5858
}
5959

60-
corslite(url, L.bind(function(err, resp) {
60+
return corslite(url, L.bind(function(err, resp) {
6161
var data,
6262
errorMessage,
6363
statusCode;
@@ -88,8 +88,6 @@
8888
});
8989
}
9090
}, this));
91-
92-
return this;
9391
},
9492

9593
_routeDone: function(response, inputWaypoints, callback, context) {

src/L.Routing.OSRMv1.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
url,
4343
timer,
4444
wp,
45-
i;
45+
i,
46+
xhr;
4647

4748
options = L.extend({}, this.options.routingOptions, options);
4849
url = this.buildRouteUrl(waypoints, options);
@@ -66,39 +67,37 @@
6667
wps.push(new L.Routing.Waypoint(wp.latLng, wp.name, wp.options));
6768
}
6869

69-
corslite(url, L.bind(function(err, resp) {
70+
return xhr = corslite(url, L.bind(function(err, resp) {
7071
var data,
71-
errorMessage,
72-
statusCode;
72+
error = {};
7373

7474
clearTimeout(timer);
7575
if (!timedOut) {
76-
errorMessage = 'HTTP request failed: ' + err;
77-
statusCode = -1;
78-
7976
if (!err) {
8077
try {
8178
data = JSON.parse(resp.responseText);
8279
try {
8380
return this._routeDone(data, wps, options, callback, context);
8481
} catch (ex) {
85-
statusCode = -3;
86-
errorMessage = ex.toString();
82+
error.status = -3;
83+
error.error = ex.toString();
8784
}
8885
} catch (ex) {
89-
statusCode = -2;
90-
errorMessage = 'Error parsing OSRM response: ' + ex.toString();
86+
error.status = -2;
87+
error.error = 'Error parsing OSRM response: ' + ex.toString();
9188
}
89+
} else {
90+
error = L.extend({}, err, {
91+
error: 'HTTP request failed: ' + err.type,
92+
status: -1
93+
})
9294
}
9395

94-
callback.call(context || callback, {
95-
status: statusCode,
96-
message: errorMessage
97-
});
96+
callback.call(context || callback, error);
97+
} else {
98+
xhr.abort();
9899
}
99100
}, this));
100-
101-
return this;
102101
},
103102

104103
requiresMoreDetail: function(route, zoom, bounds) {

0 commit comments

Comments
 (0)