Change completionCallback with 'tile-loaded' event to support original scenario of async completion notification with additional guarding flags.

This commit is contained in:
Aiosa 2023-01-31 08:05:02 +01:00
parent 81d86570da
commit 55e7d2439a

View File

@ -1655,17 +1655,23 @@ $.extend($.TiledImage.prototype, $.EventSource.prototype, /** @lends OpenSeadrag
* @param {XMLHttpRequest|undefined} tileRequest * @param {XMLHttpRequest|undefined} tileRequest
*/ */
_setTileLoaded: function(tile, data, cutoff, tileRequest) { _setTileLoaded: function(tile, data, cutoff, tileRequest) {
var stopper = 1, var increment = 0,
cached = false, completed = false,
eventFinished = false,
_this = this; _this = this;
function getCompletionCallback() { function getCompletionCallback() {
if (eventFinished) {
$.console.error("Event 'tile-loaded' argument getCompletionCallback must not be called asynchronously.");
}
increment++;
return completionCallback; return completionCallback;
} }
function completionCallback() { function completionCallback() {
stopper--; increment--;
if (stopper >= 0) { if (increment === 0 && !completed) {
completed = true;
tile.loading = false; tile.loading = false;
tile.loaded = true; tile.loaded = true;
tile.hasTransparency = _this.source.hasTransparency( tile.hasTransparency = _this.source.hasTransparency(
@ -1678,13 +1684,8 @@ $.extend($.TiledImage.prototype, $.EventSource.prototype, /** @lends OpenSeadrag
cutoff: cutoff, cutoff: cutoff,
tiledImage: _this tiledImage: _this
}); });
cached = true;
} }
_this._needsDraw = true; _this._needsDraw = true;
} else if (tile.context2D && cached) {
$.console.error("The tile has been cached, yet tile.context2D was set afterwards." +
" The cache is orphaned now. To avoid this, increase the priority of 'tile-loaded' event " +
"attaching context2D property.");
} }
} }
@ -1717,6 +1718,7 @@ $.extend($.TiledImage.prototype, $.EventSource.prototype, /** @lends OpenSeadrag
getCompletionCallback: getCompletionCallback getCompletionCallback: getCompletionCallback
}); });
// In case the completion callback is never called, we at least force it once. // In case the completion callback is never called, we at least force it once.
eventFinished = true;
getCompletionCallback()(); getCompletionCallback()();
}, },