Music Lab: hot reload fix - #74871
Open
breville wants to merge 2 commits into
Open
Conversation
MusicView is a class component, which React Fast Refresh remounts rather than preserves, so every development rebuild that reaches it ran componentDidMount again with the sources the page loaded with. loadCode saves what it loads, so the remount wrote those stale sources over whatever had been done since the page opened. The mount now asks the project manager for the sources it holds — pending save, else last saved or loaded — and falls back to initialSources only when there is no manager. ProjectManager gains getCurrentSources for that; in production the two are the same object, since load stores what it returns. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
[written by Claude]
Music Lab could lose work in development: a hot update remounts
MusicView, which loaded the page's original sources again and saved them over the project. The mount now loads the project's current sources instead.What happened
MusicViewis a class component, which React Fast Refresh remounts rather than preserves. Every development rebuild that reached it rancomponentDidMountagain, and the mount calledonLevelLoadwithinitialSources— the sources the page loaded with.loadCodesaves whatever it loads, so the remount immediately wrote those stale sources over everything done since the page opened. It looked like "my last few changes vanished after a rebuild". Production never hot-reloads, so students were never exposed; this is a developer-experience fix.The fix
ProjectManagergainsgetCurrentSources(): the sources waiting to be saved, else the last saved or loaded ones.MusicView's mount loads those, falling back toinitialSourcesonly when there is no project manager. In production they are the same sources, sinceloadstores what it returns, so nothing changes there.Sprite Lab in Lab2 had a sibling of this bug the same afternoon (a seed effect re-run by Fast Refresh, then autosaved); its fix is in #74845.
Testing
ProjectManagerunit test: the current sources are the loaded ones before any save, the saved ones after, and the queued ones while a save waits for its interval. The existing 37 pass. Typecheck and lint clean.MusicView.jsxto trigger a hot update. With the fix the component remounted and the block stayed in the workspace and in the stored sources. With staging'sMusicView.jsxswapped in, the same hot update reverted the workspace to the when-run block alone and the stored sources lost the block within the save interval.