Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New: Add removal functions #96

Merged
merged 4 commits into from
Mar 18, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions src/models/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ export class Graph<N extends INodeBase, E extends IEdgeBase> implements IGraph<N
this._settings?.onMergeData?.(data);
}

// TODO(dlozic): Add delete all mechanic.
remove(data: Partial<{ nodeIds: number[]; edgeIds: number[] }>) {
const nodeIds = data.nodeIds ?? [];
const edgeIds = data.edgeIds ?? [];
Expand All @@ -274,7 +273,35 @@ export class Graph<N extends INodeBase, E extends IEdgeBase> implements IGraph<N
this._applyEdgeOffsets();
this._applyStyle();

this._settings?.onRemoveData?.(data);
if (this._settings && this._settings.onRemoveData) {
this._settings.onRemoveData(data);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a user (internal or external) I would expect to receive in the callback all nodes and edges that have been removed. You are forwarding the data that you've received which doesn't necessarily mean that those have been removed, e.g.

In my graph I have these nodes and edges

  • Nodes: 1, 2, 3
  • Edges A (1-2), B (2-1), C(1-3)

I call remove with nodes = 1, 3, 5 and edges = C, D. But actually nodes 1, 3 will be removed and all edges will be removed (A, B, C). So instead of receiving [1, 3, 5], [C, D] in the callback, I would expect to receive [1, 3], [A, B, C].

}
}

removeAll() {
const nodeIds = this._nodes.getAll().map((node) => node.id);
const edgeIds = this._edges.getAll().map((edge) => edge.id);

this._removeNodes(nodeIds);
this._removeEdges(edgeIds);

if (this._settings && this._settings.onRemoveData) {
this._settings.onRemoveData({ nodeIds, edgeIds });
}
}

removeAllEdges() {
const edgeIds = this._edges.getAll().map((edge) => edge.id);

this._removeEdges(edgeIds);

if (this._settings && this._settings.onRemoveData) {
this._settings.onRemoveData({ edgeIds });
}
}

removeAllNodes() {
this.removeAll();
}

isEqual<T extends INodeBase, K extends IEdgeBase>(graph: Graph<T, K>): boolean {
Expand Down
Loading