Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add timeouts to delay expensive mutation operations #6149

Merged
merged 2 commits into from
May 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions core/mutator.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ class Mutator extends Icon {
* @private
*/
this.sourceListener_ = null;

/**
* The PID associated with the updateWorkpace_ timeout, or 0 if no timeout
* is currently running.
* @type {number}
*/
this.updateWorkspacePid_ = 0;
}

/**
Expand Down Expand Up @@ -402,14 +409,26 @@ class Mutator extends Icon {
* @private
*/
workspaceChanged_(e) {
if (!(e.isUiEvent ||
(e.type === eventUtils.CHANGE &&
/** @type {!BlockChange} */ (e).element === 'disabled') ||
e.type === eventUtils.CREATE)) {
this.updateWorkspace_();
if (!this.shouldIgnoreMutatorEvent_(e) && !this.updateWorkspacePid_) {
this.updateWorkspacePid_ = setTimeout(() => {
this.updateWorkspacePid_ = 0;
this.updateWorkspace_();
}, 0);
}
}

/**
* Returns whether the given event in the mutator workspace should be ignored
* when deciding whether to update the workspace and compose the block or not.
* @param {!Abstract} e The event.
* @return {boolean} Whether to ignore the event or not.
*/
shouldIgnoreMutatorEvent_(e) {
return e.isUiEvent || e.type === eventUtils.CREATE ||
e.type === eventUtils.CHANGE &&
/** @type {!BlockChange} */ (e).element === 'disabled';
}

/**
* Updates the source block when the mutator's blocks are changed.
* Bump down any block that's too high.
Expand Down Expand Up @@ -485,7 +504,7 @@ class Mutator extends Icon {
// Don't update the bubble until the drag has ended, to avoid moving
// blocks under the cursor.
if (!this.workspace_.isDragging()) {
this.resizeBubble_();
setTimeout(() => this.resizeBubble_(), 0);
}
eventUtils.setGroup(existingGroup);
}
Expand Down