Commit 7db8ce94 authored by Neil Fraser's avatar Neil Fraser

Minor simplifications to connections.

parent 34914674
...@@ -42,8 +42,10 @@ Blockly.Connection = function(source, type) { ...@@ -42,8 +42,10 @@ Blockly.Connection = function(source, type) {
/** @type {number} */ /** @type {number} */
this.type = type; this.type = type;
// Shortcut for the databases for this connection's workspace. // Shortcut for the databases for this connection's workspace.
this.dbList_ = source.workspace.connectionDBList; this.db_ = source.workspace.connectionDBList[type];
this.hidden_ = !this.dbList_; this.dbOpposite_ =
source.workspace.connectionDBList[Blockly.OPPOSITE_TYPE[type]];
this.hidden_ = !this.db_;
}; };
/** /**
...@@ -88,7 +90,7 @@ Blockly.Connection.prototype.dispose = function() { ...@@ -88,7 +90,7 @@ Blockly.Connection.prototype.dispose = function() {
throw 'Disconnect connection before disposing of it.'; throw 'Disconnect connection before disposing of it.';
} }
if (this.inDB_) { if (this.inDB_) {
this.dbList_[this.type].removeConnection_(this); this.db_.removeConnection_(this);
} }
if (Blockly.highlightedConnection_ == this) { if (Blockly.highlightedConnection_ == this) {
Blockly.highlightedConnection_ = null; Blockly.highlightedConnection_ = null;
...@@ -96,6 +98,8 @@ Blockly.Connection.prototype.dispose = function() { ...@@ -96,6 +98,8 @@ Blockly.Connection.prototype.dispose = function() {
if (Blockly.localConnection_ == this) { if (Blockly.localConnection_ == this) {
Blockly.localConnection_ = null; Blockly.localConnection_ = null;
} }
this.db_ = null;
this.dbOpposite_ = null;
}; };
/** /**
...@@ -242,8 +246,8 @@ Blockly.Connection.prototype.connect = function(otherConnection) { ...@@ -242,8 +246,8 @@ Blockly.Connection.prototype.connect = function(otherConnection) {
*/ */
Blockly.Connection.singleConnection_ = function(block, orphanBlock) { Blockly.Connection.singleConnection_ = function(block, orphanBlock) {
var connection = false; var connection = false;
for (var x = 0; x < block.inputList.length; x++) { for (var i = 0; i < block.inputList.length; i++) {
var thisConnection = block.inputList[x].connection; var thisConnection = block.inputList[i].connection;
if (thisConnection && thisConnection.type == Blockly.INPUT_VALUE && if (thisConnection && thisConnection.type == Blockly.INPUT_VALUE &&
orphanBlock.outputConnection.checkType_(thisConnection)) { orphanBlock.outputConnection.checkType_(thisConnection)) {
if (connection) { if (connection) {
...@@ -351,13 +355,13 @@ Blockly.Connection.prototype.bumpAwayFrom_ = function(staticConnection) { ...@@ -351,13 +355,13 @@ Blockly.Connection.prototype.bumpAwayFrom_ = function(staticConnection) {
Blockly.Connection.prototype.moveTo = function(x, y) { Blockly.Connection.prototype.moveTo = function(x, y) {
// Remove it from its old location in the database (if already present) // Remove it from its old location in the database (if already present)
if (this.inDB_) { if (this.inDB_) {
this.dbList_[this.type].removeConnection_(this); this.db_.removeConnection_(this);
} }
this.x_ = x; this.x_ = x;
this.y_ = y; this.y_ = y;
// Insert it into its new location in the database. // Insert it into its new location in the database.
if (!this.hidden_) { if (!this.hidden_) {
this.dbList_[this.type].addConnection_(this); this.db_.addConnection_(this);
} }
}; };
...@@ -441,8 +445,7 @@ Blockly.Connection.prototype.closest = function(maxLimit, dx, dy) { ...@@ -441,8 +445,7 @@ Blockly.Connection.prototype.closest = function(maxLimit, dx, dy) {
return {connection: null, radius: maxLimit}; return {connection: null, radius: maxLimit};
} }
// Determine the opposite type of connection. // Determine the opposite type of connection.
var oppositeType = Blockly.OPPOSITE_TYPE[this.type]; var db = this.dbOpposite_;
var db = this.dbList_[oppositeType];
// Since this connection is probably being dragged, add the delta. // Since this connection is probably being dragged, add the delta.
var currentX = this.x_ + dx; var currentX = this.x_ + dx;
...@@ -560,8 +563,8 @@ Blockly.Connection.prototype.checkType_ = function(otherConnection) { ...@@ -560,8 +563,8 @@ Blockly.Connection.prototype.checkType_ = function(otherConnection) {
return true; return true;
} }
// Find any intersection in the check lists. // Find any intersection in the check lists.
for (var x = 0; x < this.check_.length; x++) { for (var i = 0; i < this.check_.length; i++) {
if (otherConnection.check_.indexOf(this.check_[x]) != -1) { if (otherConnection.check_.indexOf(this.check_[i]) != -1) {
return true; return true;
} }
} }
...@@ -608,8 +611,7 @@ Blockly.Connection.prototype.setCheck = function(check) { ...@@ -608,8 +611,7 @@ Blockly.Connection.prototype.setCheck = function(check) {
*/ */
Blockly.Connection.prototype.neighbours_ = function(maxLimit) { Blockly.Connection.prototype.neighbours_ = function(maxLimit) {
// Determine the opposite type of connection. // Determine the opposite type of connection.
var oppositeType = Blockly.OPPOSITE_TYPE[this.type]; var db = this.dbOpposite_;
var db = this.dbList_[oppositeType];
var currentX = this.x_; var currentX = this.x_;
var currentY = this.y_; var currentY = this.y_;
...@@ -668,9 +670,9 @@ Blockly.Connection.prototype.neighbours_ = function(maxLimit) { ...@@ -668,9 +670,9 @@ Blockly.Connection.prototype.neighbours_ = function(maxLimit) {
Blockly.Connection.prototype.setHidden = function(hidden) { Blockly.Connection.prototype.setHidden = function(hidden) {
this.hidden_ = hidden; this.hidden_ = hidden;
if (hidden && this.inDB_) { if (hidden && this.inDB_) {
this.dbList_[this.type].removeConnection_(this); this.db_.removeConnection_(this);
} else if (!hidden && !this.inDB_) { } else if (!hidden && !this.inDB_) {
this.dbList_[this.type].addConnection_(this); this.db_.addConnection_(this);
} }
}; };
...@@ -692,8 +694,8 @@ Blockly.Connection.prototype.hideAll = function() { ...@@ -692,8 +694,8 @@ Blockly.Connection.prototype.hideAll = function() {
} }
// Close all bubbles of all children. // Close all bubbles of all children.
var icons = block.getIcons(); var icons = block.getIcons();
for (var x = 0; x < icons.length; x++) { for (var i = 0; i < icons.length; i++) {
icons[x].setVisible(false); icons[i].setVisible(false);
} }
} }
} }
...@@ -799,6 +801,7 @@ Blockly.ConnectionDB.prototype.removeConnection_ = function(connection) { ...@@ -799,6 +801,7 @@ Blockly.ConnectionDB.prototype.removeConnection_ = function(connection) {
} }
connection.inDB_ = false; connection.inDB_ = false;
// Find the connection using a binary search. // Find the connection using a binary search.
// About 10% faster than a linear search using indexOf.
var pointerMin = 0; var pointerMin = 0;
var pointerMax = this.length - 2; var pointerMax = this.length - 2;
var pointerMid = pointerMax; var pointerMid = pointerMax;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment