Skip to content

Commit 9d19d07

Browse files
committed
[fix] Setting the nodeIndex causing sideffects so created a new lookup nodeLinkIndex
1 parent 30bf15e commit 9d19d07

File tree

4 files changed

+20
-42
lines changed

4 files changed

+20
-42
lines changed

src/js/netjsongraph.core.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,19 @@ class NetJSONGraph {
111111
this.config.maxPointsFetched - 1,
112112
JSONData.nodes.length - this.config.maxPointsFetched,
113113
);
114-
const nodeSet = new Set(JSONData.nodes.map((node) => node.id));
114+
const nodeSet = new Set();
115+
// Build a lookup map (this.nodeLinkIndex) for quick access to node or link data.
116+
// Uses node IDs as keys for nodes and "source~target" as keys for links.
117+
// This avoids repeated traversal when restoring state from URL fragments.
118+
this.nodeLinkIndex = {};
119+
JSONData.nodes.forEach((node) => {
120+
nodeSet.add(node.id);
121+
this.nodeLinkIndex[node.id] = node;
122+
});
115123
JSONData.links = JSONData.links.filter((link) => {
116124
if (nodeSet.has(link.source) && nodeSet.has(link.target)) {
125+
const key = `${link.source}~${link.target}`;
126+
this.nodeLinkIndex[key] = link;
117127
return true;
118128
}
119129
if (!nodeSet.has(link.source)) {

src/js/netjsongraph.render.js

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -151,19 +151,10 @@ class NetJSONGraphRender {
151151
// Preserve original NetJSON node for sidebar use
152152
/* eslint-disable no-underscore-dangle */
153153
nodeResult._source = JSON.parse(JSON.stringify(node));
154-
// Store each node's index in `this.indexedNode` for quick lookup,
155-
// allowing direct access to the node via `this.data.nodes[index]` without traversing the array.
156-
self.nodeIndex = self.nodeIndex || {};
157-
self.nodeIndex[node.id] = index;
158154
return nodeResult;
159155
});
160156
const links = JSONData.links.map((link, index) => {
161157
const linkResult = JSON.parse(JSON.stringify(link));
162-
// Similarly, create a lookup for links using "source-target" as the key,
163-
// storing only the index for direct access via `this.data.links[index]`.
164-
self.nodeIndex = self.nodeIndex || {};
165-
const {source, target} = linkResult;
166-
self.nodeIndex[`${source}~${target}`] = index;
167158

168159
const {linkStyleConfig, linkEmphasisConfig} = self.utils.getLinkStyle(
169160
link,
@@ -300,10 +291,6 @@ class NetJSONGraphRender {
300291
});
301292
}
302293
}
303-
// Store each node's index in `this.indexedNode` for quick lookup,
304-
// allowing direct access to the node via `this.data.nodes[index]` without traversing the array.
305-
self.nodeIndex = self.nodeIndex || {};
306-
self.nodeIndex[node.id] = index;
307294
});
308295
links.forEach((link, index) => {
309296
if (!flatNodes[link.source]) {
@@ -332,11 +319,6 @@ class NetJSONGraphRender {
332319
link,
333320
});
334321
}
335-
// Similarly, create a lookup for links using "source-target" as the key,
336-
// storing only the index for direct access via `this.data.links[index]`.
337-
self.nodeIndex = self.nodeIndex || {};
338-
const {source, target} = link;
339-
self.nodeIndex[`${source}~${target}`] = index;
340322
});
341323

342324
nodesData = nodesData.concat(clusters);

src/js/netjsongraph.util.js

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,35 +1253,28 @@ class NetJSONGraphUtil {
12531253
}
12541254
const fragments = this.parseUrlFragments();
12551255
const {id} = self.config.bookmarkableActions;
1256-
let nodeId, index, nodeData;
1256+
let nodeId;
12571257
if (self.config.render === self.utils.graphRender) {
12581258
if (params.dataType === "node") {
12591259
nodeId = params.data.id;
1260-
index = self.nodeIndex[nodeId];
1261-
nodeData = self.data.nodes[index];
12621260
} else if (params.dataType === "edge") {
12631261
const {source, target} = params.data;
12641262
nodeId = `${source}~${target}`;
1265-
index = self.nodeIndex[source];
1266-
nodeData = self.data.links[index];
12671263
}
12681264
} else if (self.config.render === self.utils.mapRender) {
12691265
if (params.seriesType === "scatter") {
12701266
nodeId = params.data.node.id;
1271-
index = self.nodeIndex[nodeId];
1272-
nodeData = self.data.nodes[index];
12731267
} else if (params.seriesType === "lines") {
12741268
const {source, target} = params.data.link;
12751269
nodeId = `${source}~${target}`;
1276-
index = self.nodeIndex[source];
1277-
nodeData = self.data.links[index];
12781270
}
12791271
}
12801272
if (!fragments[id]) {
12811273
fragments[id] = new URLSearchParams();
12821274
fragments[id].set("id", id);
12831275
}
12841276
fragments[id].set("nodeId", nodeId);
1277+
const nodeData = self.nodeLinkIndex[nodeId];
12851278
this.updateUrlFragments(fragments, nodeData);
12861279
}
12871280

@@ -1303,13 +1296,11 @@ class NetJSONGraphUtil {
13031296
const fragmentParams = fragments[id] && fragments[id].get ? fragments[id] : null;
13041297
const nodeId =
13051298
fragmentParams && fragmentParams.get ? fragmentParams.get("nodeId") : undefined;
1306-
if (!nodeId || !self.nodeIndex || !self.nodeIndex[nodeId] == null) {
1299+
if (!nodeId || !self.nodeLinkIndex || !self.nodeLinkIndex[nodeId] == null) {
13071300
return;
13081301
}
13091302
const [source, target] = nodeId.split("~");
1310-
const index = self.nodeIndex[nodeId];
1311-
// If source && target both exists then the node is a link
1312-
const node = source && target ? self.data.links[index] : self.data.nodes[index];
1303+
const node = self.nodeLinkIndex[nodeId];
13131304
const nodeType =
13141305
self.config.graphConfig.series.type || self.config.mapOptions.nodeConfig.type;
13151306
const {location, cluster} = node || {};

test/netjsongraph.util.test.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,8 @@ describe("Test URL fragment utilities", () => {
217217
render: "graph",
218218
bookmarkableActions: {enabled: true, id: "basicUsage"},
219219
},
220-
data: {nodes: [node]},
221220
utils: {...utils, graphRender: "graph", mapRender: "map"},
222-
nodeIndex: {node1: 0},
221+
nodeLinkIndex: {node1: node},
223222
};
224223
const params = {
225224
dataType: "node",
@@ -241,9 +240,8 @@ describe("Test URL fragment utilities", () => {
241240
render: "graph",
242241
bookmarkableActions: {enabled: true, id: "basicUsage"},
243242
},
244-
data: {links: [link]},
245243
utils: {...utils, graphRender: "graph", mapRender: "map"},
246-
nodeIndex: {"node1:node2": 0},
244+
nodeLinkIndex: {"node1:node2": link},
247245
};
248246

249247
const params = {
@@ -268,9 +266,8 @@ describe("Test URL fragment utilities", () => {
268266
render: "graph",
269267
bookmarkableActions: {enabled: true, id: "geo"},
270268
},
271-
data: {nodes: [node1, node2]},
272269
utils: {...utils, graphRender: "graph", mapRender: "map"},
273-
nodeIndex: {node1: 0, node2: 1},
270+
nodeLinkIndex: {node1: node1, node2: node2},
274271
};
275272
const params = {
276273
dataType: "node",
@@ -313,8 +310,7 @@ describe("Test URL fragment utilities", () => {
313310
mapOptions: {nodeConfig: {type: "scatter"}, center: [0, 0]},
314311
onClickElement: mockOnClick,
315312
},
316-
data: {nodes: [node]},
317-
nodeIndex: {n1: 0},
313+
nodeLinkIndex: {n1: node},
318314
leaflet: {setView: mockSetView, getZoom: () => 6},
319315
utils,
320316
};
@@ -343,8 +339,7 @@ describe("Test URL fragment utilities", () => {
343339
mapOptions: {nodeConfig: {type: "scatter"}, center: [10, 20]},
344340
onClickElement: mockOnClick,
345341
},
346-
data: {links: [link]},
347-
nodeIndex: {"n1~n2": 0},
342+
nodeLinkIndex: {"n1~n2": link},
348343
leaflet: {setView: mockSetView, getZoom: () => 6},
349344
utils,
350345
};

0 commit comments

Comments
 (0)