Files
feedBack/static/v3/live-guitar-tone-source.js
T
4b4c156fce refactor(ui): defer every classic script, keep boot() on DOMContentLoaded (R3a) (#872)
Puts every external `<script>` in the v3 shell into the deferred queue, and
keeps each script's boot() firing at DOMContentLoaded exactly as it does today.
Behaviourally a no-op; it is what makes the ES-module flips safe.

WHY. `type="module"` defers execution to after HTML parse. Classic-`defer` and
module scripts share ONE "execute after parsing" list and run in DOCUMENT ORDER,
but a plain classic script runs DURING parse — ahead of all of them. So the
moment capabilities.js becomes a module while app.js is still plain, app.js runs
FIRST, and its 11 top-level `window.feedBack.on(...)` calls (app.js:6245-6722)
hit a bare `{}` — `_ensureFeedBackEventBus()` (capabilities.js:33), which
attaches .on/.emit/.off, would not have run yet. TypeError, app.js dies
mid-parse. Deferring everything now keeps document order == execution order
through the rest of the migration.

THE CATCH (Codex preflight caught this — a real ordering change). 22 scripts
guard their boot with `if (document.readyState === 'loading')`. A deferred
script runs at readyState 'interactive', so that test is FALSE and the else-branch
fires boot() immediately, at the script's position in document order — instead of
at DOMContentLoaded, after every script has evaluated.

That matters far more than one call site: a scan of the shell's scripts found
**43 forward references** where a script's boot() reads a global that a LATER
script defines (shell.js -> profile.js's window.v3Onboarding, songs.js ->
settings.js's window._confirmDialog, badges.js -> songs.js's
window.displayTuningName, ...). Every one of them resolves today only because
all boots happen at DOMContentLoaded. So the guards now treat 'interactive' as
not-ready (`!== 'complete'`), restoring that exactly.

Codex's specific finding (first-run onboarding silently skipped) did NOT
reproduce — shell.js's boot() awaits /api/profile, and that yield lets the
remaining deferred scripts run first. But the race it described is real, the
guard is silent when it fails (`&& window.v3Onboarding`), and the other 42
forward refs have no such await protecting them. Fixed at the root rather than
at the one site.

VERIFIED. A/B against origin/main on a fresh profile, 13 probes (onboarding
overlay, v3Onboarding/v3Songs/v3Profile/fbNotify/v3Badges/uiPrompt/showScreen,
bus, capabilities.version, createHighway, plugin scripts, mounted screens):
IDENTICAL, zero console/page errors on both. pytest 2396, node 1028/1028,
ESLint 0 errors, Codex 0.

New guard: test_every_external_script_defers_so_document_order_is_execution_order
fails if any external tag is plain classic — verified to fail on a single
reverted tag, so it actually bites.

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-11 16:57:04 +02:00

106 lines
3.5 KiB
JavaScript

/*
* fee[dB]ack v0.3.0 — live guitar tone source preference.
*
* Lets users who get guitar tone from external hardware (Spark LIVE, pedalboards,
* etc.) opt out of the desktop "no internal amp loaded" monitor-mute hint.
* Persisted in localStorage; desktop renderer reads the same key directly.
*/
(function (root) {
'use strict';
const KEY = 'feedBack-live-guitar-tone-source';
const SOURCES = Object.freeze({
INTERNAL: 'internal',
EXTERNAL_HARDWARE: 'external_hardware',
SPARK_CONTROL_X: 'spark_control_x',
});
const DEFAULT = SOURCES.INTERNAL;
const LABELS = Object.freeze({
[SOURCES.INTERNAL]: 'fee[dB]ack internal tone',
[SOURCES.EXTERNAL_HARDWARE]: 'External amp / hardware pedalboard',
[SOURCES.SPARK_CONTROL_X]: 'Spark LIVE + Spark Control X',
});
const HELP_TEXT =
'Choose External/Spark if your guitar tone comes from hardware like Spark LIVE. '
+ 'fee[dB]ack will still score your playing but won\u2019t warn that no internal amp tone is loaded.';
function normalize(value) {
if (value === SOURCES.EXTERNAL_HARDWARE || value === SOURCES.SPARK_CONTROL_X) return value;
return DEFAULT;
}
function get() {
try { return normalize(localStorage.getItem(KEY)); } catch (_) { return DEFAULT; }
}
function set(value) {
const next = normalize(value);
try { localStorage.setItem(KEY, next); } catch (_) { /* private mode / quota */ }
syncSelects(next);
return next;
}
function shouldSuppressMonitorMuteHint(source) {
const s = normalize(source == null ? get() : source);
return s === SOURCES.EXTERNAL_HARDWARE || s === SOURCES.SPARK_CONTROL_X;
}
function syncSelects(value) {
// Exported + required in tests; guard DOM access so set()/init() can be
// called in a non-browser environment without throwing.
if (typeof document === 'undefined') return;
const v = normalize(value == null ? get() : value);
document.querySelectorAll('[data-live-guitar-tone-source]').forEach((el) => {
if (el && el.value !== v) el.value = v;
});
}
function bindSelect(el) {
if (!el || el.dataset.liveGuitarToneBound === '1') return;
el.dataset.liveGuitarToneBound = '1';
el.setAttribute('data-live-guitar-tone-source', '1');
el.value = get();
el.addEventListener('change', () => { set(el.value); });
}
function init() {
if (typeof document === 'undefined') return;
bindSelect(document.getElementById('setting-live-guitar-tone-source'));
bindSelect(document.getElementById('player-live-guitar-tone-source'));
syncSelects();
}
const api = {
KEY,
SOURCES,
DEFAULT,
LABELS,
HELP_TEXT,
get,
set,
normalize,
shouldSuppressMonitorMuteHint,
init,
};
if (root) root.v3LiveGuitarToneSource = api;
if (typeof module !== 'undefined' && module.exports) {
module.exports = api;
}
if (typeof document !== 'undefined') {
// `defer` runs this at readyState 'interactive' — later scripts have not
// evaluated yet, so wait for DOMContentLoaded (see static/v3/index.html).
if (document.readyState !== 'complete') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
}
}(typeof window !== 'undefined' ? window : null));