@@ -145,7 +145,8 @@ class Blocks extends React.Component {
145145 "PROCEDURE" ,
146146 this . ScratchBlocks . ScratchProcedures . getProceduresCategory
147147 ) ;
148- this . workspace . addChangeListener ( ( event ) => {
148+
149+ this . toolboxUpdateChangeListener = ( event ) => {
149150 if (
150151 event . type === this . ScratchBlocks . Events . VAR_CREATE ||
151152 event . type === this . ScratchBlocks . Events . VAR_RENAME ||
@@ -161,7 +162,8 @@ class Blocks extends React.Component {
161162 ) {
162163 this . requestToolboxUpdate ( ) ;
163164 }
164- } ) ;
165+ } ;
166+ this . workspace . addChangeListener ( this . toolboxUpdateChangeListener ) ;
165167
166168 // Register buttons under new callback keys for creating variables,
167169 // lists, and procedures from extensions.
@@ -515,6 +517,7 @@ class Blocks extends React.Component {
515517
516518 // Remove and reattach the workspace listener (but allow flyout events)
517519 this . workspace . removeChangeListener ( this . props . vm . blockListener ) ;
520+ this . workspace . removeChangeListener ( this . toolboxUpdateChangeListener ) ;
518521 const dom = this . ScratchBlocks . utils . xml . textToDom ( data . xml ) ;
519522 try {
520523 this . ScratchBlocks . Xml . clearWorkspaceAndLoadFromXml (
@@ -556,6 +559,15 @@ class Blocks extends React.Component {
556559 // fresh workspace and we don't want any changes made to another sprites
557560 // workspace to be 'undone' here.
558561 this . workspace . clearUndo ( ) ;
562+ // Let events get flushed before readding the toolbox-updater listener
563+ // to avoid unneeded refreshes.
564+ requestAnimationFrame ( ( ) => {
565+ setTimeout ( ( ) => {
566+ this . workspace . addChangeListener (
567+ this . toolboxUpdateChangeListener
568+ ) ;
569+ } ) ;
570+ } ) ;
559571 }
560572 handleMonitorsUpdate ( monitors ) {
561573 // Update the checkboxes of the relevant monitors.
0 commit comments