Permalink
Browse files

Fix 2D Skeleton example thanks to @shunter 's higher order function t…

…utorial.
  • Loading branch information...
1 parent 505cec5 commit 02e377e1f606eba196eeda76ffe235006908916c @bagnell bagnell committed May 17, 2012
Showing with 30 additions and 6 deletions.
  1. +30 −6 Source/Scene/BingMapsTileProvider.js
@@ -200,14 +200,16 @@ define([
this._requestTemplate();
}
- function deferredQueueContains(deferredQueue, tile) {
+ //for a given tile, if we have an element with the same tile in the queue, return the element.
+ function findInDeferredQueue(deferredQueue, tile) {
for ( var i = 0, len = deferredQueue.length; i < len; ++i) {
- var t = deferredQueue[i].tile;
+ var element = deferredQueue[i];
+ var t = element.tile;
if (t.zoom === tile.zoom && t.x === tile.x && t.y === tile.y) {
- return true;
+ return element;
}
}
- return false;
+ return undefined;
}
/**
@@ -339,17 +341,39 @@ define([
};
if (typeof this._url === 'undefined') {
- if (!deferredQueueContains(this._deferredQueue, tile)) {
+ var existingElement = findInDeferredQueue(this._deferredQueue, tile);
+ if (typeof existingElement === 'undefined') {
this._deferredQueue.push(element);
+ return image;
}
- return image;
+ //add the callbacks to the existing element so both are called
+ existingElement.onload = combineFunctions(existingElement.onload, onload);
+ existingElement.onerror = combineFunctions(existingElement.onerror, onerror);
+ existingElement.oninvalid = combineFunctions(existingElement.oninvalid, oninvalid);
+ return existingElement.image;
}
this._loadImage(element);
return image;
};
+ function combineFunctions(a, b) {
+ if (typeof a !== 'function' && typeof b !== 'function') {
+ return undefined;
+ }
+ if (typeof a !== 'function' && typeof b === 'function') {
+ return b;
+ }
+ if (typeof a === 'function' && typeof b !== 'function') {
+ return a;
+ }
+ return function() {
+ a();
+ b();
+ };
+ }
+
BingMapsTileProvider.prototype._loadImage = function(element) {
var tile = element.tile;
var lat = CesiumMath.toDegrees((tile.extent.north + tile.extent.south) * 0.5);

3 comments on commit 02e377e

Member

pjcozzi replied May 21, 2012

I'm not sure that this change is needed. Right now, scenes are completely separate. They will remain this way for the foreseeable future until the WebGL shared contexts extension is finished, implemented, and widely available. I'd rather see a fix in the Skeleton2D example where a separate BingMapsTileProvider is used for each scene just like a separate CentralBody primitive and night lights images are used. Otherwise, we have an inconsistent API where users don't know what is shared among scenes and what is not.

Bigger picture, some types like cartesians and matrices, can, of course, be used among multiple scenes, and perhaps we need to document that at some point.

Member

bagnell replied May 21, 2012

The reasoning here was that a tile provider should return the right image no matter who requests it, but I'll change the example to be consistent.

Member

pjcozzi replied May 21, 2012

Sounds good.

Please sign in to comment.