Browse Source

Network: Prevent crash when dataChanged is triggered during initialization (#3568)

* Network: Prevent crash when dataChanged is triggered during initial setting of options

Fixes #3562.

Options `hidden` and `physics` can emit a `_dataChanged` event within `setOptions()`.
If this happens when setting options during the initialization of the `Network` instance, this leads
to an error thrown, because there is no DataSet instance  connected yet to the instance.

This bug was introduced in `v4.21.0`. Unit tests have been added for this case.

* Edited comment
mbroad/code-climate-coverage-develop
wimrijnders 7 years ago
committed by Yotam Berkowitz
parent
commit
f6445cdf07
2 changed files with 26 additions and 1 deletions
  1. +5
    -1
      lib/network/modules/EdgesHandler.js
  2. +21
    -0
      test/Network.test.js

+ 5
- 1
lib/network/modules/EdgesHandler.js View File

@ -461,8 +461,12 @@ class EdgesHandler {
* @private * @private
*/ */
_addMissingEdges() { _addMissingEdges() {
let edges = this.body.edges;
let edgesData = this.body.data.edges; let edgesData = this.body.data.edges;
if (edgesData === undefined || edgesData === null) {
return; // No edges DataSet yet; can happen on startup
}
let edges = this.body.edges;
let addIds = []; let addIds = [];
edgesData.forEach((edgeData, edgeId) => { edgesData.forEach((edgeData, edgeId) => {

+ 21
- 0
test/Network.test.js View File

@ -369,6 +369,27 @@ describe('Network', function () {
}); });
it('does not crash when dataChanged is triggered when setting options on first initialization ', function() {
// The init should succeed without an error thrown.
var options = {
nodes: {
physics: false // any value here triggered the error
}
};
createSampleNetwork(options);
// Do the other values as well that can cause this./
// 'any values' applies here as well, expecting no throw
options = {edges: {physics: false}};
createSampleNetwork(options);
options = {nodes: {hidden: false}};
createSampleNetwork(options);
options = {edges: {hidden: false}};
createSampleNetwork(options);
});
describe('Node', function () { describe('Node', function () {
it('has known font options', function () { it('has known font options', function () {

Loading…
Cancel
Save