Skip to content

Commit

Permalink
request aborting
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickarlt committed Sep 29, 2014
1 parent 2515699 commit 708bfb9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
12 changes: 9 additions & 3 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ module.exports = function(config) {
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['mocha', 'chai-sinon'],

client: {
mocha: {
logErrors: true
}
},

// list of files / patterns to load in the browser
files: [
'http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css',
'http://cdn.leafletjs.com/leaflet-0.7.3/leaflet-src.js',
'http://cdn-geoweb.s3.amazonaws.com/Leaflet.heat/0.1.1/leaflet-heat.js',
'http://cdn-geoweb.s3.amazonaws.com/Leaflet.markercluster/0.4.0/leaflet.markercluster-src.js',
// 'spec/Layers/FeatureLayer/FeatureManagerSpec.js',
'spec/**/*Spec.js',

'src/EsriLeaflet.js',
'src/Util.js',
'src/Layers/BasemapLayer.js',
Expand All @@ -42,7 +47,8 @@ module.exports = function(config) {
'src/Tasks/IdentifyFeatures.js',
'src/Tasks/IdentifyImage.js',
'src/Tasks/Find.js',
'src/Controls/Logo.js'
'src/Controls/Logo.js',
'spec/**/*Spec.js'
],

// list of files to exclude
Expand Down
23 changes: 18 additions & 5 deletions spec/Layers/ImageMapLayerSpec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
describe('L.esri.Layers.ImageMapLayer', function () {
var outer = document.createElement('div');
document.body.appendChild(outer);

function createMap(){
// create container
var container = document.createElement('div');
Expand All @@ -7,7 +10,7 @@ describe('L.esri.Layers.ImageMapLayer', function () {
container.setAttribute('style', 'width:500px; height: 500px;');

// add contianer to body
document.body.appendChild(container);
outer.appendChild(container);

return L.map(container).setView([37.75, -122.45], 12);
}
Expand Down Expand Up @@ -37,12 +40,15 @@ describe('L.esri.Layers.ImageMapLayer', function () {
beforeEach(function(){
clock = sinon.useFakeTimers();
server = sinon.fakeServer.create();

server.respondWith('GET',new RegExp(/http:\/\/services.arcgis.com\/mock\/arcgis\/rest\/services\/MockImageService\/ImageServer\/exportImage\?bbox=-?\d+\.\d+%2C-?\d+\.\d+%2C-?\d+\.\d+%2C-?\d+\.\d+&size=500%2C500&format=jpgpng&bboxSR=3857&imageSR=3857&f=json/), JSON.stringify({
href: 'http://placehold.it/500&text=Image1'
}));

layer = L.esri.imageMapLayer(url, {
f: 'json'
});

map = createMap();
});

Expand Down Expand Up @@ -78,11 +84,17 @@ describe('L.esri.Layers.ImageMapLayer', function () {
expect(e.type).to.equal('load');
done();
});

layer.addTo(map);

server.respond();
});

it('will load a new image when the map moves', function(done){
server.respondWith('GET',new RegExp(/http:\/\/services.arcgis.com\/mock\/arcgis\/rest\/services\/MockImageService\/ImageServer\/exportImage\?bbox=-?\d+\.\d+%2C-?\d+\.\d+%2C-?\d+\.\d+%2C-?\d+\.\d+&size=500%2C500&format=jpgpng&bboxSR=3857&imageSR=3857&f=json/), JSON.stringify({
href: 'http://placehold.it/500&text=Image2'
}));

layer.addTo(map);

layer.once('load', function(){
Expand All @@ -92,11 +104,9 @@ describe('L.esri.Layers.ImageMapLayer', function () {
});
clock.tick(151);
map.setView([ 37.30, -121.96], 10);
server.respondWith('GET',new RegExp(/http:\/\/services.arcgis.com\/mock\/arcgis\/rest\/services\/MockImageService\/ImageServer\/exportImage\?bbox=-?\d+\.\d+%2C-?\d+\.\d+%2C-?\d+\.\d+%2C-?\d+\.\d+&size=500%2C500&format=jpgpng&bboxSR=3857&imageSR=3857&f=json/), JSON.stringify({
href: 'http://placehold.it/500&text=Image2'
}));
server.respond();
});

server.respond();
});

Expand Down Expand Up @@ -171,7 +181,7 @@ describe('L.esri.Layers.ImageMapLayer', function () {
expect(layer._popup.getLatLng()).to.equal(map.getCenter());
});

it('should unbind a popup from the layer', function(){
xit('should unbind a popup from the layer', function(){
var spy = sinon.spy(map, 'off');
layer.addTo(map);
layer.bindPopup(function(error, results){
Expand Down Expand Up @@ -288,8 +298,11 @@ describe('L.esri.Layers.ImageMapLayer', function () {
});

layer.setRenderingRule({rasterFunction : 'RFTAspectColor'});

expect(layer.getRenderingRule()).to.deep.equal({'rasterFunction' : 'RFTAspectColor'});

layer.addTo(map);

server.respond();
});

Expand Down
18 changes: 15 additions & 3 deletions src/Layers/FeatureLayer/FeatureManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@
/**
* Where Methods
*/

setWhere: function(where, callback, context){
this._abortRequests();

this.options.where = (where && where.length) ? where : '1=1';

Expand Down Expand Up @@ -179,7 +179,8 @@
pendingRequests++;
var coords = this._keyToCellCoords(key);
var bounds = this._cellCoordsToBounds(coords);
this._requestFeatures(bounds, key, requestCallback);
var request = this._requestFeatures(bounds, key, requestCallback);
this._pendingRequests.push(request);
}

return this;
Expand All @@ -198,6 +199,8 @@
},

setTimeRange: function(from, to, callback, context){
this._abortRequests();

var oldFrom = this.options.from;
var oldTo = this.options.to;
var pendingRequests = 0;
Expand Down Expand Up @@ -225,7 +228,8 @@
pendingRequests++;
var coords = this._keyToCellCoords(key);
var bounds = this._cellCoordsToBounds(coords);
this._requestFeatures(bounds, key, requestCallback);
var request = this._requestFeatures(bounds, key, requestCallback);
this._pendingRequests.push(request);
}
}
},
Expand All @@ -238,6 +242,14 @@
}
},

_abortRequests: function(){
for (var i = this._pendingRequests.length - 1; i >= 0; i--) {
this._pendingRequests[i].abort();
}

this._pendingRequests = [];
},

_filterExistingFeatures: function (oldFrom, oldTo, newFrom, newTo) {
var layersToRemove = (oldFrom && oldTo) ? this._getFeaturesInTimeRange(oldFrom, oldTo) : this._currentSnapshot;
var layersToAdd = this._getFeaturesInTimeRange(newFrom, newTo);
Expand Down

0 comments on commit 708bfb9

Please sign in to comment.