Fall back to standard load handling when decode fails

This commit is contained in:
Andreas Hocevar
2021-12-02 08:26:53 +01:00
parent 38bff05e43
commit 2d92756f84
+19 -17
View File
@@ -160,14 +160,23 @@ class ImageWrapper extends ImageBase {
*/ */
export function listenImage(image, loadHandler, errorHandler) { export function listenImage(image, loadHandler, errorHandler) {
const img = /** @type {HTMLImageElement} */ (image); const img = /** @type {HTMLImageElement} */ (image);
let listening = true;
let decoding = false;
let loaded = false;
const listenerKeys = [
listenOnce(img, EventType.LOAD, function () {
loaded = true;
if (!decoding) {
loadHandler();
}
}),
];
if (img.src && IMAGE_DECODE) { if (img.src && IMAGE_DECODE) {
const promise = img.decode(); decoding = true;
let listening = true; img
const unlisten = function () { .decode()
listening = false;
};
promise
.then(function () { .then(function () {
if (listening) { if (listening) {
loadHandler(); loadHandler();
@@ -175,26 +184,19 @@ export function listenImage(image, loadHandler, errorHandler) {
}) })
.catch(function (error) { .catch(function (error) {
if (listening) { if (listening) {
// FIXME: Unconditionally call errorHandler() when this bug is fixed upstream: if (loaded) {
// https://bugs.webkit.org/show_bug.cgi?id=198527
if (
error.name === 'EncodingError' &&
error.message === 'Invalid image type.'
) {
loadHandler(); loadHandler();
} else { } else {
errorHandler(); errorHandler();
} }
} }
}); });
return unlisten; } else {
listenerKeys.push(listenOnce(img, EventType.ERROR, errorHandler));
} }
const listenerKeys = [
listenOnce(img, EventType.LOAD, loadHandler),
listenOnce(img, EventType.ERROR, errorHandler),
];
return function unlisten() { return function unlisten() {
listening = false;
listenerKeys.forEach(unlistenByKey); listenerKeys.forEach(unlistenByKey);
}; };
} }