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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
feat(asset): invalidate(ref) for hot-reload
esengine351f18274899
diff --git a/sdk/src/asset/Assets.ts b/sdk/src/asset/Assets.tsindex 094302bee..3e1d68835 100644--- a/sdk/src/asset/Assets.ts+++ b/sdk/src/asset/Assets.ts@@ -473,6 +473,38 @@ export class Assets {}}+ /**+ * Drop every internal cache entry for `ref` so the next `loadTexture` /+ * `loadMaterial` / ... fetches fresh bytes. Call this when the source+ * file changed on disk (hot reload).+ *+ * Any GPU handle that was already handed out stays valid and keeps+ * rendering — that's the caller's concern to release. The next load+ * produces a brand-new handle from the updated bytes; the old handle+ * is evicted from the cache but not freed, so currently-rendering+ * entities don't flicker.+ *+ * Returns true if any cache held `ref`.+ */+ invalidate(ref: string): boolean {+ const path = this.resolveRef(ref) ?? ref;+ let hit = false;++ // Textures: cache_key has a flip flag suffix, so check both.+ for (const flip of [true, false]) {+ const key = this.textureCacheKey_(path, flip);+ if (this.textureCache_.invalidate(key)) hit = true;+ if (this.textureRefCounts_.delete(key)) hit = true;+ }++ // Generic caches: material / font / anim-clip / tilemap / timeline / audio / prefab.+ for (const cache of this.genericCache_.values()) {+ if (cache.invalidate(path)) hit = true;+ }++ return hit;+ }+releaseAll(): void {const rm = requireResourceManager();for (const info of this.textureCache_.values()) {diff --git a/sdk/src/asset/AsyncCache.ts b/sdk/src/asset/AsyncCache.tsindex d167c3f14..d6523e4c3 100644--- a/sdk/src/asset/AsyncCache.ts+++ b/sdk/src/asset/AsyncCache.ts@@ -84,6 +84,26 @@ export class AsyncCache<T> {return this.cache_.delete(key);}+ /**+ * Drop every record of `key` — resolved value, failure cooldown, and+ * any in-flight loader. Used by hot-reload when the underlying bytes+ * changed on disk and the next `getOrLoad` must fetch fresh.+ *+ * Returns true if any record was removed. Doesn't release whatever+ * resource the cached value points to — that's the caller's concern+ * (see `Assets.invalidate` for the resource-aware variant).+ */+ invalidate(key: string): boolean {+ const hadCached = this.cache_.delete(key);+ const hadFailed = this.failed_.delete(key);+ const pending = this.pending_.get(key);+ if (pending) {+ pending.aborted = true;+ this.pending_.delete(key);+ }+ return hadCached || hadFailed || pending !== undefined;+ }+clear(): void {this.cache_.clear();}diff --git a/sdk/tests/AsyncCache.test.ts b/sdk/tests/AsyncCache.test.tsindex 9d9845261..7e8f5a3de 100644--- a/sdk/tests/AsyncCache.test.ts+++ b/sdk/tests/AsyncCache.test.ts@@ -281,4 +281,54 @@ describe('AsyncCache', () => {expect(cache.has('key')).toBe(true);});});++ describe('invalidate', () => {+ it('drops the cached value so next load fetches fresh', async () => {+ const loader = vi.fn()+ .mockResolvedValueOnce('v1')+ .mockResolvedValueOnce('v2');++ expect(await cache.getOrLoad('k', loader)).toBe('v1');+ expect(cache.invalidate('k')).toBe(true);+ expect(await cache.getOrLoad('k', loader)).toBe('v2');+ expect(loader).toHaveBeenCalledTimes(2);+ });++ it('returns false for a key that was never cached', () => {+ expect(cache.invalidate('missing')).toBe(false);+ });++ it('also clears the failure cooldown', async () => {+ const loader = vi.fn()+ .mockRejectedValueOnce(new Error('fail'))+ .mockResolvedValueOnce('ok');++ await expect(cache.getOrLoad('k', loader)).rejects.toThrow('fail');+ // Without invalidate, the cooldown would throw again.+ expect(cache.invalidate('k')).toBe(true);+ expect(await cache.getOrLoad('k', loader)).toBe('ok');+ });++ it('aborts an in-flight loader so the next getOrLoad starts a new one', async () => {+ let resolveFirst: (v: string) => void = () => {};+ const first = new Promise<string>(r => { resolveFirst = r; });+ const loader = vi.fn()+ .mockImplementationOnce(() => first)+ .mockResolvedValueOnce('v2');++ const p1 = cache.getOrLoad('k', loader);+ expect(cache.invalidate('k')).toBe(true);++ // Second call must not dedupe against the aborted first loader.+ const p2 = cache.getOrLoad('k', loader);+ resolveFirst('v1');++ // The aborted first loader still resolves but must not land in+ // the cache; the fresh load wins.+ await p1;+ expect(await p2).toBe('v2');+ expect(cache.get('k')).toBe('v2');+ expect(loader).toHaveBeenCalledTimes(2);+ });+ });});