From 7befdb5448207f3633dd8be37524e6cdcc43e524 Mon Sep 17 00:00:00 2001 From: bfs2021 <101915083+bfs2021@users.noreply.github.com> Date: Thu, 29 Oct 2026 05:25:21 +0800 Subject: [PATCH] fix(audio): release the loading flag so a failed effect can retry loadEffects set loading[key] and no path cleared it, so one failed XHR or decode left the effect permanently skipped. Every exit now settles the flag through a shared done callback, while it stays set for the duration of the request so parallel calls still cannot duplicate one. --- audio.js | 9 +++-- tests/audio.test.cjs | 90 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 tests/audio.test.cjs diff --git a/audio.js b/audio.js index 0d981f7..9dfec86 100644 --- a/audio.js +++ b/audio.js @@ -39,13 +39,16 @@ names.slice(1).forEach(function (key) { if (!assets[key] || buffers[key] || loading[key]) return; loading[key] = true; + // Every exit clears the flag, so a failed load can be tried again; while + // it is set, parallel calls skip the effect instead of racing it. + var settle = function () { delete loading[key]; }; var xhr = new XMLHttpRequest(); xhr.open('GET', assets[key], true); xhr.responseType = 'arraybuffer'; xhr.timeout = 8000; xhr.onload = function () { - if ((xhr.status === 0 || xhr.status === 200) && xhr.response) decode(key, xhr.response); - else errors[key] = 'Sound file unavailable.'; + if ((xhr.status === 0 || xhr.status === 200) && xhr.response) decode(key, xhr.response, settle); + else { errors[key] = 'Sound file unavailable.'; settle(); } }; - xhr.onerror = xhr.ontimeout = function () { errors[key] = 'Unable to load sound effect.'; }; + xhr.onerror = xhr.ontimeout = function () { errors[key] = 'Unable to load sound effect.'; settle(); }; xhr.send(); }); } diff --git a/tests/audio.test.cjs b/tests/audio.test.cjs new file mode 100644 index 0000000..fd2da11 --- /dev/null +++ b/tests/audio.test.cjs @@ -0,0 +1,90 @@ +'use strict'; +const test = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); + +const ASSETS = { + menu: 'assets/audio/menu.mp3', hover: 'assets/audio/hover.mp3', + select: 'assets/audio/select.mp3', back: 'assets/audio/back.mp3', + page: 'assets/audio/page.mp3', start: 'assets/audio/start.mp3' +}; + +/** + * A sandbox whose XHRs record themselves: each instance the page creates is + * pushed onto `requests`, and `xhr.respond()` drives its callbacks. + * `decode` is the AudioContext.decodeAudioData the run ends in: 'ok' decodes a + * 1s buffer, anything else reports an unsupported format. + */ +function environment(decode) { + const requests = []; + function XMLHttpRequest() { + requests.push(this); + this.onload = this.onerror = this.ontimeout = null; + } + XMLHttpRequest.prototype.open = function () {}; + XMLHttpRequest.prototype.send = function () {}; + function Audio() { + return {loop: false, preload: '', volume: 1, play() { return {catch() {}}; }, pause() {}}; + } + function AudioContext() { + this.state = 'running'; + this.resume = () => ({catch() {}}); + this.createGain = () => ({connect() {}, gain: {value: 1}}); + this.decodeAudioData = (data, ok, fail) => { + if (decode === 'ok') ok({duration: 1}); + else fail(); + }; + } + const window = { + localStorage: {getItem: () => null, setItem() {}}, + WII_AUDIO_ASSETS: ASSETS, + AudioContext, + addEventListener() {}, + XMLHttpRequest, + URL: {createObjectURL: () => 'blob:x', revokeObjectURL() {}} + }; + vm.runInNewContext(fs.readFileSync(path.join(__dirname, '..', 'audio.js'), 'utf8'), + {window, document: {addEventListener() {}, hidden: false}, Audio, XMLHttpRequest, localStorage: window.localStorage, + URL: window.URL, JSON, Object, Array, String, Math, RegExp, Number}); + return {window, requests}; +} + +test('a failed load can be retried', () => { + const {window, requests} = environment('fail'); + window.WiiAudio.unlock(); + assert.equal(requests.length, 5); + requests.forEach(xhr => xhr.onerror()); + window.WiiAudio.unlock(); + assert.equal(requests.length, 10); +}); + +test('a decoded effect is cached and not requested again', () => { + const {window, requests} = environment('ok'); + window.WiiAudio.unlock(); + assert.equal(requests.length, 5); + requests.forEach(xhr => { + xhr.status = 200; xhr.response = 'audio bytes'; xhr.onload(); + }); + window.WiiAudio.unlock(); + assert.equal(requests.length, 5); +}); + +test('a decode failure leaves the effect retryable', () => { + const {window, requests} = environment('fail'); + window.WiiAudio.unlock(); + requests.forEach(xhr => { + xhr.status = 200; xhr.response = 'audio bytes'; xhr.onload(); + }); + window.WiiAudio.unlock(); + assert.equal(requests.length, 10); +}); + +test('parallel unlocks never duplicate an in-flight request', () => { + const {window, requests} = environment('ok'); + window.WiiAudio.unlock(); + window.WiiAudio.unlock(); + window.WiiAudio.unlock(); + assert.equal(requests.length, 5); +});