From 49fcbc8411cb7c13e0f9bdff3c69570156a72f06 Mon Sep 17 00:00:00 2001 From: Neil Fraser Date: Fri, 3 Sep 2021 11:40:55 -0700 Subject: [PATCH] Use Blockly.utils.arrayRemove (#5420) Recently added code doesn't use existing utility function. --- core/component_manager.js | 11 +++++------ core/theme_manager.js | 5 ++--- core/utils.js | 9 ++++----- core/variable_map.js | 8 +++----- core/workspace.js | 3 +-- 5 files changed, 15 insertions(+), 21 deletions(-) diff --git a/core/component_manager.js b/core/component_manager.js index d8b215504..62c306872 100644 --- a/core/component_manager.js +++ b/core/component_manager.js @@ -13,6 +13,8 @@ goog.provide('Blockly.ComponentManager'); +goog.require('Blockly.utils'); + goog.requireType('Blockly.IAutoHideable'); goog.requireType('Blockly.IComponent'); goog.requireType('Blockly.IDeleteArea'); @@ -93,8 +95,7 @@ Blockly.ComponentManager.prototype.removeComponent = function(id) { } for (var i = 0; i < componentInfo.capabilities.length; i++) { var capability = String(componentInfo.capabilities[i]).toLowerCase(); - this.capabilityToComponentIds_[capability].splice( - this.capabilityToComponentIds_[capability].indexOf(id), 1); + Blockly.utils.arrayRemove(this.capabilityToComponentIds_[capability], id); } delete this.componentData_[id]; }; @@ -139,10 +140,8 @@ Blockly.ComponentManager.prototype.removeCapability = function(id, capability) { return; } capability = String(capability).toLowerCase(); - this.componentData_[id].capabilities.splice( - this.componentData_[id].capabilities.indexOf(capability), 1); - this.capabilityToComponentIds_[capability].splice( - this.capabilityToComponentIds_[capability].indexOf(id), 1); + Blockly.utils.arrayRemove(this.componentData_[id].capabilities, capability); + Blockly.utils.arrayRemove(this.capabilityToComponentIds_[capability], id); }; /** diff --git a/core/theme_manager.js b/core/theme_manager.js index 6c461a2e7..5778bb201 100644 --- a/core/theme_manager.js +++ b/core/theme_manager.js @@ -15,6 +15,7 @@ goog.provide('Blockly.ThemeManager'); goog.require('Blockly.Theme'); +goog.require('Blockly.utils'); goog.requireType('Blockly.Workspace'); goog.requireType('Blockly.WorkspaceSvg'); @@ -129,11 +130,9 @@ Blockly.ThemeManager.prototype.subscribeWorkspace = function(workspace) { * @package */ Blockly.ThemeManager.prototype.unsubscribeWorkspace = function(workspace) { - var index = this.subscribedWorkspaces_.indexOf(workspace); - if (index < 0) { + if (!Blockly.utils.arrayRemove(this.subscribedWorkspaces_, workspace)) { throw Error('Cannot unsubscribe a workspace that hasn\'t been subscribed.'); } - this.subscribedWorkspaces_.splice(index, 1); }; /** diff --git a/core/utils.js b/core/utils.js index 171e83422..7dba787d1 100644 --- a/core/utils.js +++ b/core/utils.js @@ -519,14 +519,13 @@ Blockly.utils.getViewportBBox = function() { /** * Removes the first occurrence of a particular value from an array. - * @param {!Array} arr Array from which to remove - * value. - * @param {*} obj Object to remove. + * @param {!Array} arr Array from which to remove value. + * @param {*} value Value to remove. * @return {boolean} True if an element was removed. * @package */ -Blockly.utils.arrayRemove = function(arr, obj) { - var i = arr.indexOf(obj); +Blockly.utils.arrayRemove = function(arr, value) { + var i = arr.indexOf(value); if (i == -1) { return false; } diff --git a/core/variable_map.js b/core/variable_map.js index 34fa926ec..5c576cce8 100644 --- a/core/variable_map.js +++ b/core/variable_map.js @@ -149,10 +149,7 @@ Blockly.VariableMap.prototype.renameVariableWithConflict_ = function(variable, Blockly.Events.fire(new (Blockly.Events.get(Blockly.Events.VAR_DELETE))( variable)); // And remove it from the list. - var variableList = this.getVariablesOfType(type); - var variableIndex = variableList.indexOf(variable); - this.variableMap_[type].splice(variableIndex, 1); - + Blockly.utils.arrayRemove(this.variableMap_[type], variable); }; /* End functions for renaming variables. */ @@ -205,9 +202,10 @@ Blockly.VariableMap.prototype.createVariable = function(name, * @param {!Blockly.VariableModel} variable Variable to delete. */ Blockly.VariableMap.prototype.deleteVariable = function(variable) { + var variableId = variable.getId(); var variableList = this.variableMap_[variable.type]; for (var i = 0, tempVar; (tempVar = variableList[i]); i++) { - if (tempVar.getId() == variable.getId()) { + if (tempVar.getId() == variableId) { variableList.splice(i, 1); Blockly.Events.fire(new (Blockly.Events.get(Blockly.Events.VAR_DELETE))( variable)); diff --git a/core/workspace.js b/core/workspace.js index 9a1dd8cbb..e04007bbc 100644 --- a/core/workspace.js +++ b/core/workspace.js @@ -240,8 +240,7 @@ Blockly.Workspace.prototype.addTypedBlock = function(block) { * @param {!Blockly.Block} block Block to remove. */ Blockly.Workspace.prototype.removeTypedBlock = function(block) { - this.typedBlocksDB_[block.type].splice(this.typedBlocksDB_[block.type] - .indexOf(block), 1); + Blockly.utils.arrayRemove(this.typedBlocksDB_[block.type], block); if (!this.typedBlocksDB_[block.type].length) { delete this.typedBlocksDB_[block.type]; }