Skip to content

Commit 02e377e

Browse files
committed
Fix 2D Skeleton example thanks to @shunter 's higher order function tutorial.
1 parent 505cec5 commit 02e377e

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

Diff for: Source/Scene/BingMapsTileProvider.js

+30-6
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,16 @@ define([
200200
this._requestTemplate();
201201
}
202202

203-
function deferredQueueContains(deferredQueue, tile) {
203+
//for a given tile, if we have an element with the same tile in the queue, return the element.
204+
function findInDeferredQueue(deferredQueue, tile) {
204205
for ( var i = 0, len = deferredQueue.length; i < len; ++i) {
205-
var t = deferredQueue[i].tile;
206+
var element = deferredQueue[i];
207+
var t = element.tile;
206208
if (t.zoom === tile.zoom && t.x === tile.x && t.y === tile.y) {
207-
return true;
209+
return element;
208210
}
209211
}
210-
return false;
212+
return undefined;
211213
}
212214

213215
/**
@@ -339,17 +341,39 @@ define([
339341
};
340342

341343
if (typeof this._url === 'undefined') {
342-
if (!deferredQueueContains(this._deferredQueue, tile)) {
344+
var existingElement = findInDeferredQueue(this._deferredQueue, tile);
345+
if (typeof existingElement === 'undefined') {
343346
this._deferredQueue.push(element);
347+
return image;
344348
}
345349

346-
return image;
350+
//add the callbacks to the existing element so both are called
351+
existingElement.onload = combineFunctions(existingElement.onload, onload);
352+
existingElement.onerror = combineFunctions(existingElement.onerror, onerror);
353+
existingElement.oninvalid = combineFunctions(existingElement.oninvalid, oninvalid);
354+
return existingElement.image;
347355
}
348356

349357
this._loadImage(element);
350358
return image;
351359
};
352360

361+
function combineFunctions(a, b) {
362+
if (typeof a !== 'function' && typeof b !== 'function') {
363+
return undefined;
364+
}
365+
if (typeof a !== 'function' && typeof b === 'function') {
366+
return b;
367+
}
368+
if (typeof a === 'function' && typeof b !== 'function') {
369+
return a;
370+
}
371+
return function() {
372+
a();
373+
b();
374+
};
375+
}
376+
353377
BingMapsTileProvider.prototype._loadImage = function(element) {
354378
var tile = element.tile;
355379
var lat = CesiumMath.toDegrees((tile.extent.north + tile.extent.south) * 0.5);

0 commit comments

Comments
 (0)