Skip to content

Commit 54d3e97

Browse files
committed
Use promises
1 parent 807d876 commit 54d3e97

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

build/ExportMap.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ExportMap.js

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ OpenLayers.Control.ExportMap = OpenLayers.Class(OpenLayers.Control, {
101101
stitchTiles: function (layer) {
102102

103103
var that = this;
104+
this.imagePromises = [];
104105
layer.grid.forEach(function (grid) {
105106
grid.forEach(function (tile) {
106107
var url = layer.getURL(tile.bounds);
@@ -112,9 +113,15 @@ OpenLayers.Control.ExportMap = OpenLayers.Class(OpenLayers.Control, {
112113
};
113114
}
114115

115-
that.loadImage(url);
116+
that.imagePromises.push(new Promise(function (resolve, reject) {
117+
that.loadImage(resolve, url)
118+
}));
116119
})
117120
});
121+
122+
Promise.all(this.imagePromises).then(function(){
123+
that.imagesLoaded();
124+
})
118125
},
119126
/**
120127
* A function to load the tile image from a URL. When all the images have been loaded
@@ -124,29 +131,31 @@ OpenLayers.Control.ExportMap = OpenLayers.Class(OpenLayers.Control, {
124131
* @param {String} url
125132
* @returns {undefined}
126133
*/
127-
loadImage: function (url) {
134+
loadImage: function (resolve, url) {
128135
var image = new Image();
129136
var that = this;
130137

131138
image.onload = function () {
132-
var canvasComponents = that.canvasComponents;
133139
// Add the tile to the front of the array
134-
canvasComponents.unshift(this);
135-
// Check to see if we have finished loading all the images
136-
if (canvasComponents.length >= Object.keys(that.tileData).length) {
137-
canvasComponents.forEach(function (canvasComponent) {
138-
if (canvasComponent.toString().indexOf('HTMLCanvasElement') > -1) {
139-
that.drawCanvasComponent(canvasComponent, 0, 0);
140-
return true;
141-
}
142-
143-
var pos = that.tileData[canvasComponent.src];
144-
that.drawCanvasComponent(canvasComponent, pos.x, pos.y);
145-
});
146-
}
140+
that.canvasComponents.unshift(this);
141+
resolve(image);
147142
};
143+
148144
image.src = url;
149145
},
146+
147+
imagesLoaded: function () {
148+
var that = this;
149+
this.canvasComponents.forEach(function (canvasComponent) {
150+
if (canvasComponent.toString().indexOf('HTMLCanvasElement') > -1) {
151+
that.drawCanvasComponent(canvasComponent, 0, 0);
152+
return true;
153+
}
154+
155+
var pos = that.tileData[canvasComponent.src];
156+
that.drawCanvasComponent(canvasComponent, pos.x, pos.y);
157+
});
158+
},
150159
/**
151160
* Draws a canvas component onto the canvas context
152161
*

0 commit comments

Comments
 (0)