Skip to content

Commit 81cefcb

Browse files
fix: restore Music Blocks loading regression after RequireJS changes (sugarlabs#5632)
- Add robust load guard in index.html for createjs - Enhance activity.js initialize() to wait for critical globals (Tone, libgif, etc.) - Make Activity constructor defensive against missing GIFAnimator - Fix potential ReferenceError in index.html doSearch() call Co-authored-by: Walter Bender <walter@sugarlabs.org>
1 parent e22947d commit 81cefcb

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

index.html

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,13 @@
548548
<script>
549549
let canvas, stage;
550550
function init() {
551+
// Defensive check: ensure createjs is loaded before using it
552+
if (typeof createjs === "undefined") {
553+
// Retry after a short delay if createjs isn't ready yet
554+
setTimeout(init, 50);
555+
return;
556+
}
557+
551558
canvas = document.getElementById("canvas");
552559
stage = new createjs.Stage(canvas);
553560

@@ -608,7 +615,12 @@
608615
</script>
609616
<script>
610617
$(document).ready(function () {
611-
doSearch();
618+
// Defensive check for doSearch global (now an Activity member)
619+
if (typeof doSearch === "function") {
620+
doSearch();
621+
} else if (window.activity && typeof window.activity.doSearch === "function") {
622+
window.activity.doSearch();
623+
}
612624
});
613625
</script>
614626

js/activity.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,12 @@ class Activity {
306306
this.loadAnimationIntervalId = null;
307307

308308
// Initialize GIF animator
309-
// Check if GIFAnimator is available before using it
310309
if (typeof GIFAnimator !== "undefined") {
311310
this.gifAnimator = new GIFAnimator();
311+
} else {
312+
// eslint-disable-next-line no-console
313+
console.debug("GIFAnimator not yet available in constructor");
314+
this.gifAnimator = null;
312315
}
313316

314317
// Dirty flag for canvas rendering optimization
@@ -485,6 +488,11 @@ class Activity {
485488
this.helpfulWheelItems = [];
486489

487490
this.setHelpfulSearchDiv();
491+
492+
// Late initialization of GIF animator if it was missed in constructor
493+
if (!this.gifAnimator && typeof GIFAnimator !== "undefined") {
494+
this.gifAnimator = new GIFAnimator();
495+
}
488496
};
489497

490498
/*
@@ -7786,7 +7794,15 @@ const activity = new Activity();
77867794
// Execute initialization once all RequireJS modules are loaded AND DOM is ready
77877795
define(["domReady!"].concat(MYDEFINES), doc => {
77887796
const initialize = () => {
7789-
if (typeof createDefaultStack !== "undefined") {
7797+
// Defensive check for multiple critical globals that may be delayed
7798+
// due to 'defer' execution timing variances.
7799+
const globalsReady = typeof createDefaultStack !== "undefined" &&
7800+
typeof createjs !== "undefined" &&
7801+
typeof Tone !== "undefined" &&
7802+
typeof GIFAnimator !== "undefined" &&
7803+
typeof SuperGif !== "undefined";
7804+
7805+
if (globalsReady) {
77907806
activity.setupDependencies();
77917807
activity.domReady(doc);
77927808
activity.doContextMenus();
@@ -7810,7 +7826,7 @@ define(["domReady!"].concat(MYDEFINES), doc => {
78107826
}
78117827
);
78127828
} else {
7813-
setTimeout(initialize, 10);
7829+
setTimeout(initialize, 50); // Increased delay slightly
78147830
}
78157831
}
78167832
};

0 commit comments

Comments
 (0)