Commit before merging master v5.0

This commit is contained in:
Aiosa 2024-08-15 12:58:01 +02:00
parent 0a035afc2d
commit 1b6f79661b
2 changed files with 116 additions and 63 deletions

View File

@ -2651,66 +2651,50 @@ function OpenSeadragon( options ){
//@private, runs non-invasive update of all tiles given in the list //@private, runs non-invasive update of all tiles given in the list
invalidateTilesLater: function(tileList, tStamp, viewer, batch = $.DEFAULT_SETTINGS.maxTilesPerFrame) { invalidateTilesLater: function(tileList, tStamp, viewer) {
let i = 0; if (tileList.length < 1) {
let interval = setInterval(() => { return;
let tile = tileList[i]; }
while (tile && !tile.loaded) {
tile = tileList[i++]; function finish () {
const tile = tileList[0];
const tiledImage = tile.tiledImage;
tiledImage.invalidatedFinishAt = tiledImage.invalidatedAt;
for (let tile of tileList) {
tile.render();
}
viewer.forceRedraw();
}
$.Promise.all(tileList.map(tile => {
if (!tile.loaded) {
return undefined;
} }
if (i >= tileList.length) {
viewer.forceRedraw();
clearInterval(interval);
return;
}
const tiledImage = tile.tiledImage; const tiledImage = tile.tiledImage;
if (tiledImage.invalidatedAt > tStamp) { if (tiledImage.invalidatedAt > tStamp) {
viewer.forceRedraw(); return undefined;
clearInterval(interval);
return;
} }
let count = 1;
for (; i < tileList.length; i++) {
const tile = tileList[i];
if (!tile.loaded) {
continue;
}
const tileCache = tile.getCache(); const tileCache = tile.getCache();
if (tileCache._updateStamp >= tStamp) { if (tileCache._updateStamp >= tStamp) {
continue; return undefined;
}
// prevents other tiles sharing the cache (~the key) from event
//todo works unless the cache key CHANGES by plugins
// - either prevent
// - or ...?
tileCache._updateStamp = tStamp;
$.invalidateTile(tile, tile.tiledImage, tStamp, viewer, i);
if (++count > batch) {
break;
}
} }
}); tileCache._updateStamp = tStamp;
return viewer.raiseEventAwaiting('tile-needs-update', {
tile: tile,
tiledImage: tile.tiledImage,
}).then(() => {
// TODO: check that the user has finished tile update and if not, rename cache key or throw
const newCache = tile.getCache();
if (newCache) {
newCache._updateStamp = tStamp;
} else {
$.console.error("After an update, the tile %s has not cache data! Check handlers on 'tile-needs-update' event!", tile);
}
});
})).catch(finish).then(finish);
}, },
//@private, runs tile update event
invalidateTile: function(tile, image, tStamp, viewer, i = -1) {
//console.log(i, "tile: process", tile);
//todo consider also ability to cut execution of ongoing event if outdated by providing comparison timestamp
viewer.raiseEventAwaiting('tile-needs-update', {
tile: tile,
tiledImage: image,
}).then(() => {
const newCache = tile.getCache();
if (newCache) {
newCache._updateStamp = tStamp;
} else {
$.console.error("After an update, the tile %s has not cache data! Check handlers on 'tile-needs-update' event!", tile);
}
});
}
}); });
@ -2976,18 +2960,85 @@ function OpenSeadragon( options ){
* @type {PromiseConstructor} * @type {PromiseConstructor}
*/ */
$.Promise = (function () { $.Promise = (function () {
if (window.Promise) { return class {
return window.Promise; constructor(handler) {
} this._error = false;
const promise = function () {}; this.__value = undefined;
//TODO consider supplying promise API via callbacks/polyfill
promise.prototype.then = try {
promise.prototype.catch = handler(
promise.prototype.finally = (value) => {
promise.all = promise.race = function () { this._value = value;
throw "OpenSeadragon needs promises API. Your browser do not support promises. You can add polyfill.js to import promises."; },
(error) => {
this._value = error;
this._error = true;
}
);
} catch (e) {
this._value = e;
this._error = true;
}
}
then(handler) {
if (!this._error) {
try {
this._value = handler(this._value);
} catch (e) {
this._value = e;
this._error = true;
}
}
return this;
}
catch(handler) {
if (this._error) {
try {
this._value = handler(this._value);
this._error = false;
} catch (e) {
this._value = e;
this._error = true;
}
}
return this;
}
get _value() {
return this.__value;
}
set _value(val) {
if (val && val.constructor === this.constructor) {
val = val._value; //unwrap
}
this.__value = val;
}
static resolve(value) {
return new this((resolve) => resolve(value));
}
static reject(error) {
return new this((_, reject) => reject(error));
}
static all(functions) {
return functions.map(fn => new this(fn));
}
static race(functions) {
if (functions.length < 1) {
return undefined;
}
return new this(functions[0]);
}
}; };
return promise; // if (window.Promise) {
// return window.Promise;
// }
// todo let users chose sync/async
})(); })();
}(OpenSeadragon)); }(OpenSeadragon));

View File

@ -233,6 +233,8 @@ $.TiledImage = function( options ) {
this._ownAjaxHeaders = {}; this._ownAjaxHeaders = {};
this.setAjaxHeaders(ajaxHeaders, false); this.setAjaxHeaders(ajaxHeaders, false);
this._initialized = true; this._initialized = true;
this.invalidatedAt = 0;
this.invalidatedFinishAt = 0;
}; };
$.extend($.TiledImage.prototype, $.EventSource.prototype, /** @lends OpenSeadragon.TiledImage.prototype */{ $.extend($.TiledImage.prototype, $.EventSource.prototype, /** @lends OpenSeadragon.TiledImage.prototype */{