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,58 +2651,41 @@ 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++];
} }
if (i >= tileList.length) { function finish () {
viewer.forceRedraw(); const tile = tileList[0];
clearInterval(interval); const tiledImage = tile.tiledImage;
return; tiledImage.invalidatedFinishAt = tiledImage.invalidatedAt;
for (let tile of tileList) {
tile.render();
} }
viewer.forceRedraw();
}
$.Promise.all(tileList.map(tile => {
if (!tile.loaded) {
return undefined;
}
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; tileCache._updateStamp = tStamp;
$.invalidateTile(tile, tile.tiledImage, tStamp, viewer, i); return viewer.raiseEventAwaiting('tile-needs-update', {
if (++count > batch) {
break;
}
}
});
},
//@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, tile: tile,
tiledImage: image, tiledImage: tile.tiledImage,
}).then(() => { }).then(() => {
// TODO: check that the user has finished tile update and if not, rename cache key or throw
const newCache = tile.getCache(); const newCache = tile.getCache();
if (newCache) { if (newCache) {
newCache._updateStamp = tStamp; newCache._updateStamp = tStamp;
@ -2710,7 +2693,8 @@ function OpenSeadragon( options ){
$.console.error("After an update, the tile %s has not cache data! Check handlers on 'tile-needs-update' event!", tile); $.console.error("After an update, the tile %s has not cache data! Check handlers on 'tile-needs-update' event!", tile);
} }
}); });
} })).catch(finish).then(finish);
},
}); });
@ -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;
this.__value = undefined;
try {
handler(
(value) => {
this._value = value;
},
(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]);
} }
const promise = function () {};
//TODO consider supplying promise API via callbacks/polyfill
promise.prototype.then =
promise.prototype.catch =
promise.prototype.finally =
promise.all = promise.race = function () {
throw "OpenSeadragon needs promises API. Your browser do not support promises. You can add polyfill.js to import promises.";
}; };
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 */{