p
This commit is contained in:
parent
d14550dad6
commit
63e10af865
86 changed files with 5400 additions and 1209 deletions
21
js/assets.js
21
js/assets.js
|
|
@ -70,7 +70,9 @@ const scaledSpriteCache = new Map();
|
|||
const stainOverlayCache = new Map();
|
||||
const SPRITE_CACHE_LIMIT = 128;
|
||||
const STAIN_CACHE_LIMIT = 64;
|
||||
const IMAGE_LOAD_TIMEOUT_MS = 9000;
|
||||
const IMAGE_KEEP_IDS = new Set(["smile"]);
|
||||
let imageLoadTimeoutsTotal = 0;
|
||||
|
||||
const SUNBATH_IMAGE_IDS = new Set(SPRITES
|
||||
.map(asset => asset?.id || "")
|
||||
|
|
@ -228,9 +230,22 @@ function loadImageAsset(asset, priority = "auto") {
|
|||
img.alphaBounds = ALPHA_BOUNDS[asset.id] || null;
|
||||
images.set(asset.id, img);
|
||||
let promise;
|
||||
let settled = false;
|
||||
promise = new Promise((resolve) => {
|
||||
img.onload = () => { markImageLoaded(asset, img); touchImage(asset.id); resolve(img); };
|
||||
img.onerror = () => { resolve(null); };
|
||||
const finish = (value, timedOut = false) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
if (timer) clearTimeout(timer);
|
||||
if (timedOut) imageLoadTimeoutsTotal += 1;
|
||||
resolve(value);
|
||||
};
|
||||
const timer = window.setTimeout ? window.setTimeout(() => finish(null, true), IMAGE_LOAD_TIMEOUT_MS) : null;
|
||||
img.onload = () => {
|
||||
markImageLoaded(asset, img);
|
||||
touchImage(asset.id);
|
||||
finish(img);
|
||||
};
|
||||
img.onerror = () => { finish(null); };
|
||||
}).finally(() => {
|
||||
if (imagePromises.get(asset.id) === promise) imagePromises.delete(asset.id);
|
||||
});
|
||||
|
|
@ -369,7 +384,7 @@ if (typeof window !== "undefined") {
|
|||
trim: trimImageMemory,
|
||||
unload: unloadImageAsset,
|
||||
stats() {
|
||||
return { loaded: images.size, pending: imagePromises.size, spriteCache: scaledSpriteCache.size, stainCache: stainOverlayCache.size };
|
||||
return { loaded: images.size, pending: imagePromises.size, spriteCache: scaledSpriteCache.size, stainCache: stainOverlayCache.size, timeouts: imageLoadTimeoutsTotal };
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue