Skip to content

Commit 2f6f7eb

Browse files
Yomguitherealjacomyal
authored andcommitted
Cleaning graph update events
Dropping the keyToIndex registers, not used anymore Fixing hover items not being cleared on item drop Fix jacomyal#1214
1 parent ee83f2d commit 2f6f7eb

File tree

1 file changed

+34
-54
lines changed

1 file changed

+34
-54
lines changed

src/sigma.ts

Lines changed: 34 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,6 @@ export default class Sigma extends TypedEventEmitter<SigmaEvents> {
166166
private edgeDataCache: Record<string, EdgeDisplayData> = {};
167167
private nodesWithForcedLabels: string[] = [];
168168
private edgesWithForcedLabels: string[] = [];
169-
private nodeKeyToIndex: Record<string, number> = {};
170-
private edgeKeyToIndex: Record<string, number> = {};
171169
private nodeExtent: { x: Extent; y: Extent } = { x: [0, 1], y: [0, 1] };
172170

173171
private matrix: Float32Array = identity();
@@ -218,8 +216,6 @@ export default class Sigma extends TypedEventEmitter<SigmaEvents> {
218216
this.graph = graph;
219217
this.container = container;
220218

221-
this.initializeCache();
222-
223219
// Initializing contexts
224220
this.createWebGLContext("edges", { preserveDrawingBuffer: true });
225221
this.createCanvasContext("edgeLabels");
@@ -355,31 +351,6 @@ export default class Sigma extends TypedEventEmitter<SigmaEvents> {
355351
return this;
356352
}
357353

358-
/**
359-
* Method used to initialize display data cache.
360-
*
361-
* @return {Sigma}
362-
*/
363-
private initializeCache(): void {
364-
const graph = this.graph;
365-
366-
// NOTE: the data caches are never reset to avoid paying a GC cost
367-
// But this could prove to be a bad decision. In which case just "reset"
368-
// them here.
369-
370-
let i = 0;
371-
372-
graph.forEachNode((key) => {
373-
this.nodeKeyToIndex[key] = i++;
374-
});
375-
376-
i = 0;
377-
378-
graph.forEachEdge((key) => {
379-
this.edgeKeyToIndex[key] = i++;
380-
});
381-
}
382-
383354
/**
384355
* Method binding camera handlers.
385356
*
@@ -566,33 +537,46 @@ export default class Sigma extends TypedEventEmitter<SigmaEvents> {
566537
this._scheduleRefresh();
567538
};
568539

569-
this.activeListeners.addNodeGraphUpdate = (e: { key: string }): void => {
570-
// Adding entry to cache
571-
this.nodeKeyToIndex[e.key] = graph.order - 1;
540+
this.activeListeners.dropNodeGraphUpdate = (e: { key: string }): void => {
541+
delete this.nodeDataCache[e.key];
542+
543+
if (this.hoveredNode === e.key) this.hoveredNode = null;
544+
572545
this.activeListeners.graphUpdate();
573546
};
574547

575-
this.activeListeners.addEdgeGraphUpdate = (e: { key: string }): void => {
576-
// Adding entry to cache
577-
this.nodeKeyToIndex[e.key] = graph.order - 1;
548+
this.activeListeners.dropEdgeGraphUpdate = (e: { key: string }): void => {
549+
delete this.edgeDataCache[e.key];
550+
551+
if (this.hoveredEdge === e.key) this.hoveredEdge = null;
552+
578553
this.activeListeners.graphUpdate();
579554
};
580555

581-
// TODO: clean cache on drop!
556+
this.activeListeners.clearEdgesGraphUpdate = (): void => {
557+
this.edgeDataCache = {};
558+
this.hoveredEdge = null;
582559

583-
// TODO: bind this on composed state events
584-
// TODO: it could be possible to update only specific node etc. by holding
585-
// a fixed-size pool of updated items
586-
graph.on("nodeAdded", this.activeListeners.addNodeGraphUpdate);
587-
graph.on("nodeDropped", this.activeListeners.graphUpdate);
560+
this.activeListeners.graphUpdate();
561+
};
562+
563+
this.activeListeners.clearGraphUpdate = (): void => {
564+
this.nodeDataCache = {};
565+
this.hoveredNode = null;
566+
567+
this.activeListeners.clearEdgesGraphUpdate();
568+
};
569+
570+
graph.on("nodeAdded", this.activeListeners.graphUpdate);
571+
graph.on("nodeDropped", this.activeListeners.dropNodeGraphUpdate);
588572
graph.on("nodeAttributesUpdated", this.activeListeners.softGraphUpdate);
589573
graph.on("eachNodeAttributesUpdated", this.activeListeners.graphUpdate);
590-
graph.on("edgeAdded", this.activeListeners.addEdgeGraphUpdate);
591-
graph.on("edgeDropped", this.activeListeners.graphUpdate);
574+
graph.on("edgeAdded", this.activeListeners.graphUpdate);
575+
graph.on("edgeDropped", this.activeListeners.dropEdgeGraphUpdate);
592576
graph.on("edgeAttributesUpdated", this.activeListeners.softGraphUpdate);
593577
graph.on("eachEdgeAttributesUpdated", this.activeListeners.graphUpdate);
594-
graph.on("edgesCleared", this.activeListeners.graphUpdate);
595-
graph.on("cleared", this.activeListeners.graphUpdate);
578+
graph.on("edgesCleared", this.activeListeners.clearEdgesGraphUpdate);
579+
graph.on("cleared", this.activeListeners.clearGraphUpdate);
596580

597581
return this;
598582
}
@@ -792,8 +776,6 @@ export default class Sigma extends TypedEventEmitter<SigmaEvents> {
792776

793777
// Save the node in the highlighted set if needed
794778
if (data.highlighted && !data.hidden) this.highlightedNodes.add(node);
795-
796-
this.nodeKeyToIndex[node] = i;
797779
}
798780

799781
this.labelGrid.organize();
@@ -853,8 +835,6 @@ export default class Sigma extends TypedEventEmitter<SigmaEvents> {
853835

854836
const hidden = data.hidden || sourceData.hidden || targetData.hidden;
855837
this.edgePrograms[data.type].process(sourceData, targetData, data, hidden, edgesPerPrograms[data.type]++);
856-
857-
this.nodeKeyToIndex[edge] = i;
858838
}
859839

860840
for (const type in this.edgePrograms) {
@@ -1706,16 +1686,16 @@ export default class Sigma extends TypedEventEmitter<SigmaEvents> {
17061686
this.touchCaptor.kill();
17071687

17081688
// Releasing graph handlers
1709-
graph.removeListener("nodeAdded", this.activeListeners.addNodeGraphUpdate);
1689+
graph.removeListener("nodeAdded", this.activeListeners.dropNodeGraphUpdate);
17101690
graph.removeListener("nodeDropped", this.activeListeners.graphUpdate);
17111691
graph.removeListener("nodeAttributesUpdated", this.activeListeners.softGraphUpdate);
17121692
graph.removeListener("eachNodeAttributesUpdated", this.activeListeners.graphUpdate);
1713-
graph.removeListener("edgeAdded", this.activeListeners.addEdgeGraphUpdate);
1714-
graph.removeListener("edgeDropped", this.activeListeners.graphUpdate);
1693+
graph.removeListener("edgeAdded", this.activeListeners.graphUpdate);
1694+
graph.removeListener("edgeDropped", this.activeListeners.dropEdgeGraphUpdate);
17151695
graph.removeListener("edgeAttributesUpdated", this.activeListeners.softGraphUpdate);
17161696
graph.removeListener("eachEdgeAttributesUpdated", this.activeListeners.graphUpdate);
1717-
graph.removeListener("edgesCleared", this.activeListeners.graphUpdate);
1718-
graph.removeListener("cleared", this.activeListeners.graphUpdate);
1697+
graph.removeListener("edgesCleared", this.activeListeners.clearEdgesGraphUpdate);
1698+
graph.removeListener("cleared", this.activeListeners.clearGraphUpdate);
17191699

17201700
// Releasing cache & state
17211701
this.quadtree = new QuadTree();

0 commit comments

Comments
 (0)