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
fix(sdk): concurrent audio preloads of one url decode once
esengine93e555882510
diff --git a/sdk/src/audio/Audio.ts b/sdk/src/audio/Audio.tsindex cfafae6fc..0a44f9236 100644--- a/sdk/src/audio/Audio.ts+++ b/sdk/src/audio/Audio.ts@@ -43,6 +43,7 @@ export class AudioAPI {private readonly backend_: PlatformAudioBackend;private readonly mixer_: AudioMixer | null;private readonly bufferCache_ = new Map<string, BufferEntry>();+ private readonly loadingBuffers_ = new Map<string, Promise<void>>();/** refCount==0 urls in eviction order (oldest first); access re-appends. */private readonly evictOrder_ = new Set<string>();private residentBytes_ = 0;@@ -225,8 +226,21 @@ export class AudioAPI {* double `has()` check absorbs a concurrent load racing to insert first. */private async ensureBuffer_(url: string, load: () => Promise<AudioBufferHandle>): Promise<void> {if (this.bufferCache_.has(url)) return;- const buffer = await load();- if (!this.bufferCache_.has(url)) this.insertEntry_(url, buffer);+ // De-dupe concurrent loads of the same url — without this, two preloads+ // (esp. the loadBufferFromData path, which has no backend-side dedup)+ // both decode and orphan one AudioBuffer.+ const inFlight = this.loadingBuffers_.get(url);+ if (inFlight) return inFlight;+ const promise = (async () => {+ const buffer = await load();+ if (!this.bufferCache_.has(url)) this.insertEntry_(url, buffer);+ })();+ this.loadingBuffers_.set(url, promise);+ try {+ await promise;+ } finally {+ this.loadingBuffers_.delete(url);+ }}async preload(url: string): Promise<void> {diff --git a/sdk/tests/audio-api.test.ts b/sdk/tests/audio-api.test.tsindex 0e4c8ec9d..7f7d326af 100644--- a/sdk/tests/audio-api.test.ts+++ b/sdk/tests/audio-api.test.ts@@ -40,6 +40,7 @@ function createMockBackend(): PlatformAudioBackend {initialize: vi.fn().mockResolvedValue(undefined),ensureResumed: vi.fn().mockResolvedValue(undefined),loadBuffer: vi.fn().mockResolvedValue({ id: 1, duration: 2.0 }),+ loadBufferFromData: vi.fn().mockResolvedValue({ id: 2, duration: 1.0 }),unloadBuffer: vi.fn(),play: vi.fn().mockReturnValue(createMockHandle()),suspend: vi.fn(),@@ -92,6 +93,16 @@ describe('AudioAPI', () => {expect(backend.loadBuffer).toHaveBeenCalledTimes(1);});+ it('de-dupes CONCURRENT loads of the same url (one decode, no orphan)', async () => {+ const data = new ArrayBuffer(8);+ // Both start before either resolves — without in-flight dedup both decode.+ await Promise.all([+ audio.preloadFromData('wind.mp3', data),+ audio.preloadFromData('wind.mp3', data),+ ]);+ expect(backend.loadBufferFromData).toHaveBeenCalledTimes(1);+ });+it('prefixes relative urls with baseUrl (legacy prefix path)', async () => {audio.baseUrl = 'estella://project';await audio.preload('assets/sfx.mp3');