From 9f6c1c316a2d4f5da9e8f304dd3e6a6128108702 Mon Sep 17 00:00:00 2001 From: Neil Fraser Date: Wed, 12 Jun 2019 12:15:22 -0700 Subject: [PATCH] Refactor workspace.connectionDBList MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It seems wrong that Blockly.ConnectionDB.init reaches in to create a property on workspace. It seems wrong that this database isn’t disposed of when the workspace is. It seems unnecessary that disposed connections need to drop their reference *to* the workspace’s database since the connection object is going away anyway, and the databases aren’t. --- core/connection.js | 2 -- core/connection_db.js | 8 ++++---- core/workspace.js | 6 ++++++ core/workspace_svg.js | 4 +++- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/core/connection.js b/core/connection.js index 320039fd2..082cfc441 100644 --- a/core/connection.js +++ b/core/connection.js @@ -242,8 +242,6 @@ Blockly.Connection.prototype.dispose = function() { if (this.inDB_) { this.db_.removeConnection_(this); } - this.db_ = null; - this.dbOpposite_ = null; }; /** diff --git a/core/connection_db.js b/core/connection_db.js index 192e88f6e..ce7e9487b 100644 --- a/core/connection_db.js +++ b/core/connection_db.js @@ -287,15 +287,15 @@ Blockly.ConnectionDB.prototype.searchForClosest = function(conn, maxRadius, }; /** - * Initialize a set of connection DBs for a specified workspace. - * @param {!Blockly.Workspace} workspace The workspace this DB is for. + * Initialize a set of connection DBs for a workspace. + * @return {!Array.} Array of databases. */ -Blockly.ConnectionDB.init = function(workspace) { +Blockly.ConnectionDB.init = function() { // Create four databases, one for each connection type. var dbList = []; dbList[Blockly.INPUT_VALUE] = new Blockly.ConnectionDB(); dbList[Blockly.OUTPUT_VALUE] = new Blockly.ConnectionDB(); dbList[Blockly.NEXT_STATEMENT] = new Blockly.ConnectionDB(); dbList[Blockly.PREVIOUS_STATEMENT] = new Blockly.ConnectionDB(); - workspace.connectionDBList = dbList; + return dbList; }; diff --git a/core/workspace.js b/core/workspace.js index a630a7a0b..6b662fc16 100644 --- a/core/workspace.js +++ b/core/workspace.js @@ -135,6 +135,12 @@ Blockly.Workspace.prototype.isClearing = false; */ Blockly.Workspace.prototype.MAX_UNDO = 1024; +/** + * Set of databases for rapid lookup of connection locations. + * @type {Array.} + */ +Blockly.Workspace.prototype.connectionDBList = null; + /** * Dispose of this workspace. * Unlink from all DOM elements to prevent memory leaks. diff --git a/core/workspace_svg.js b/core/workspace_svg.js index 5fd0c8899..7ce834341 100644 --- a/core/workspace_svg.js +++ b/core/workspace_svg.js @@ -74,7 +74,7 @@ Blockly.WorkspaceSvg = function(options, this.setMetrics = options.setMetrics || Blockly.WorkspaceSvg.setTopLevelWorkspaceMetrics_; - Blockly.ConnectionDB.init(this); + this.connectionDBList = Blockly.ConnectionDB.init(); if (opt_blockDragSurface) { this.blockDragSurface_ = opt_blockDragSurface; @@ -607,6 +607,8 @@ Blockly.WorkspaceSvg.prototype.dispose = function() { this.grid_ = null; } + this.connectionDBList = null; + this.toolboxCategoryCallbacks_ = null; this.flyoutButtonCallbacks_ = null;