Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix: prevent redundant texture preview updates and race conditions

esengine1443eec9da3a
diff --git a/editor/src/property/editors.ts b/editor/src/property/editors.ts
index 29d381580..9b6caa492 100644
--- a/editor/src/property/editors.ts
+++ b/editor/src/property/editors.ts
@@ -677,6 +677,8 @@ function createTextureEditor(
): PropertyEditorInstance {
const { value, onChange } = ctx;
let currentBlobUrl: string | null = null;
+ let currentRef = '';
+ let previewGeneration = 0;
const wrapper = document.createElement('div');
wrapper.className = 'es-texture-editor';
@@ -710,6 +712,8 @@ function createTextureEditor(
clearBtn.innerHTML = `<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><line x1="18" y1="6" x2="6" y2="18"></line><line x1="6" y1="6" x2="18" y2="18"></line></svg>`;
const updatePreview = async (textureRef: string) => {
+ const gen = ++previewGeneration;
+
if (currentBlobUrl) {
URL.revokeObjectURL(currentBlobUrl);
currentBlobUrl = null;
@@ -723,6 +727,7 @@ function createTextureEditor(
const fullPath = `${projectDir}/${texturePath}`;
try {
const data = await fs.readBinaryFile(fullPath);
+ if (gen !== previewGeneration) return;
if (data) {
const blob = new Blob([data.buffer as ArrayBuffer], { type: getMimeType(texturePath) });
currentBlobUrl = URL.createObjectURL(blob);
@@ -732,6 +737,7 @@ function createTextureEditor(
return;
}
} catch (err) {
+ if (gen !== previewGeneration) return;
console.warn('Failed to load texture preview:', err);
}
}
@@ -752,7 +758,8 @@ function createTextureEditor(
}
};
- updatePreview((value && typeof value === 'string') ? value : '');
+ currentRef = (value && typeof value === 'string') ? value : '';
+ updatePreview(currentRef);
input.addEventListener('click', navigateToAssetPath);
preview.addEventListener('click', navigateToAssetPath);
@@ -788,6 +795,7 @@ function createTextureEditor(
const uuid = getAssetLibrary().getUuid(relativePath);
const ref = uuid ?? relativePath;
input.value = relativePath;
+ currentRef = ref;
onChange(ref);
updatePreview(ref);
}
@@ -818,6 +826,7 @@ function createTextureEditor(
const uuid = getAssetLibrary().getUuid(relativePath);
const ref = uuid ?? relativePath;
input.value = relativePath;
+ currentRef = ref;
onChange(ref);
updatePreview(ref);
}
@@ -829,6 +838,7 @@ function createTextureEditor(
clearBtn.addEventListener('click', () => {
input.value = '';
+ currentRef = '';
onChange('');
updatePreview('');
});
@@ -844,7 +854,10 @@ function createTextureEditor(
update(v: unknown) {
const newValue = String(v ?? '');
input.value = resolveDisplayName(newValue);
- updatePreview(newValue);
+ if (newValue !== currentRef) {
+ currentRef = newValue;
+ updatePreview(newValue);
+ }
},
dispose() {
if (currentBlobUrl) {
diff --git a/editor/src/property/materialEditors.ts b/editor/src/property/materialEditors.ts
index 96f4ea8e2..d26027054 100644
--- a/editor/src/property/materialEditors.ts
+++ b/editor/src/property/materialEditors.ts
@@ -206,6 +206,8 @@ function createMaterialTextureEditor(
): PropertyEditorInstance {
const { value, onChange } = ctx;
let currentBlobUrl: string | null = null;
+ let currentRef = '';
+ let previewGeneration = 0;
const wrapper = document.createElement('div');
wrapper.className = 'es-texture-editor es-material-texture-editor';
@@ -233,6 +235,8 @@ function createMaterialTextureEditor(
browseBtn.innerHTML = `<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M3 7v10a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V9a2 2 0 0 0-2-2h-6l-2-2H5a2 2 0 0 0-2 2z"></path></svg>`;
const updatePreview = async (texturePath: string) => {
+ const gen = ++previewGeneration;
+
if (currentBlobUrl) {
URL.revokeObjectURL(currentBlobUrl);
currentBlobUrl = null;
@@ -245,6 +249,7 @@ function createMaterialTextureEditor(
const fullPath = `${projectDir}/${texturePath}`;
try {
const data = await fs.readBinaryFile(fullPath);
+ if (gen !== previewGeneration) return;
if (data) {
const blob = new Blob([data.buffer as ArrayBuffer], { type: getMimeType(texturePath) });
currentBlobUrl = URL.createObjectURL(blob);
@@ -254,6 +259,7 @@ function createMaterialTextureEditor(
return;
}
} catch (err) {
+ if (gen !== previewGeneration) return;
console.warn('Failed to load texture preview:', err);
}
}
@@ -263,7 +269,8 @@ function createMaterialTextureEditor(
preview.classList.remove('es-has-preview');
};
- updatePreview((value && typeof value === 'string') ? value : '');
+ currentRef = (value && typeof value === 'string') ? value : '';
+ updatePreview(currentRef);
input.addEventListener('click', () => {
if (input.value) {
@@ -300,6 +307,7 @@ function createMaterialTextureEditor(
if (projectDir && assetData.path.startsWith(projectDir)) {
const relativePath = assetData.path.substring(projectDir.length + 1);
input.value = relativePath;
+ currentRef = relativePath;
onChange(relativePath);
updatePreview(relativePath);
}
@@ -328,6 +336,7 @@ function createMaterialTextureEditor(
if (assetsIndex !== -1) {
const relativePath = normalizedPath.substring(assetsIndex + 1);
input.value = relativePath;
+ currentRef = relativePath;
onChange(relativePath);
updatePreview(relativePath);
}
@@ -347,7 +356,10 @@ function createMaterialTextureEditor(
update(v: unknown) {
const newValue = String(v ?? '');
input.value = newValue;
- updatePreview(newValue);
+ if (newValue !== currentRef) {
+ currentRef = newValue;
+ updatePreview(newValue);
+ }
},
dispose() {
if (currentBlobUrl) {