Add a bulkUpdate flag to avoid expensive operations (such as toolbox refresh) during workspace loading.

This commit is contained in:
Rachel Fenichel 2017-10-09 12:04:59 -07:00
parent fdd7c2d4f8
commit c838ca3a87
3 changed files with 40 additions and 9 deletions

View file

@ -453,7 +453,7 @@ Blockly.Flyout.prototype.hide = function() {
* Variables and procedures have a custom set of blocks.
*/
Blockly.Flyout.prototype.show = function(xmlList) {
this.workspace_.setResizesEnabled(false);
this.workspace_.setBulkUpdate(true);
this.hide();
this.clearOldBlocks_();
@ -529,7 +529,7 @@ Blockly.Flyout.prototype.show = function(xmlList) {
this.listeners_.push(Blockly.bindEvent_(this.svgBackground_, 'mouseover',
this, deselectAll));
this.workspace_.setResizesEnabled(true);
this.workspace_.setBulkUpdate(false);
this.reflow();
// Correctly position the flyout's scrollbar when it opens.

View file

@ -150,6 +150,14 @@ Blockly.WorkspaceSvg.prototype.isMutator = false;
*/
Blockly.WorkspaceSvg.prototype.resizesEnabled_ = true;
/**
* Whether this workspace is in the middle of a bulk update.
* Turn on during batch operations for a performance improvement.
* @type {boolean}
* @private
*/
Blockly.WorkspaceSvg.prototype.isBulkUpdating_ = false;
/**
* Current horizontal scrolling offset in pixel units.
* @type {number}
@ -1016,7 +1024,8 @@ Blockly.WorkspaceSvg.prototype.paste = function(xmlBlock) {
* @private
*/
Blockly.WorkspaceSvg.prototype.refreshToolboxSelection_ = function() {
if (this.toolbox_ && this.toolbox_.flyout_ && !this.currentGesture_) {
if (this.toolbox_ && this.toolbox_.flyout_ && !this.currentGesture_ &&
!this.isBulkUpdating_) {
this.toolbox_.refreshSelection();
}
};
@ -1796,13 +1805,35 @@ Blockly.WorkspaceSvg.prototype.setResizesEnabled = function(enabled) {
}
};
/**
* Set whether this workspace is currently doing a bulk update.
* A bulk update pauses workspace resizing but also pauses other expensive
* operations, such as refreshing the toolbox as variables are added and
* removed.
* @param {boolean} enabled True if a bulk update is starting, false if a bulk
* update is ending.
* @package
*/
Blockly.WorkspaceSvg.prototype.setBulkUpdate = function(enabled) {
// This will trigger a resize if necessary.
this.setResizesEnabled(enabled);
var stoppedUpdating = (this.isBulkUpdating_ && !enabled);
this.isBulkUpdating_ = stoppedUpdating;
if (stoppedUpdating) {
// Refresh the toolbox.
if (this.toolbox_) {
this.toolbox_.refreshSelection();
}
}
};
/**
* Dispose of all blocks in workspace, with an optimization to prevent resizes.
*/
Blockly.WorkspaceSvg.prototype.clear = function() {
this.setResizesEnabled(false);
this.setBulkUpdate(true);
Blockly.WorkspaceSvg.superClass_.clear.call(this);
this.setResizesEnabled(true);
this.setBulkUpdate(false);
};
/**

View file

@ -331,8 +331,8 @@ Blockly.Xml.domToWorkspace = function(xml, workspace) {
}
// Disable workspace resizes as an optimization.
if (workspace.setResizesEnabled) {
workspace.setResizesEnabled(false);
if (workspace.setBulkUpdate) {
workspace.setBulkUpdate(true);
}
var variablesFirst = true;
try {
@ -376,8 +376,8 @@ Blockly.Xml.domToWorkspace = function(xml, workspace) {
}
workspace.updateVariableStore(false);
// Re-enable workspace resizing.
if (workspace.setResizesEnabled) {
workspace.setResizesEnabled(true);
if (workspace.setBulkUpdate) {
workspace.setBulkUpdate(false);
}
return newBlockIds;
};