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

refactor(graph): optimize graph api, improve docs #6792

Merged
merged 1 commit into from
Feb 27, 2025
Merged
Changes from all commits
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
30 changes: 27 additions & 3 deletions packages/g6/src/runtime/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,10 @@ export class Graph extends EventEmitter {
* const node1 = graph.getNodeData('node-1');
* ```
* @apiCategory data
* @remarks
* <zh/> 节点 id 必须存在,否则会抛出异常
*
* <en/> Node id must exist, otherwise an exception will be thrown
*/
public getNodeData(id: ID): NodeData;
/**
Expand All @@ -567,12 +571,16 @@ export class Graph extends EventEmitter {
* const [node1, node2] = graph.getNodeData(['node-1', 'node-2']);
* ```
* @apiCategory data
* @remarks
* <zh/> 数组中的每个节点 id 必须存在,否则将抛出异常
*
* <en/> Each node id in the array must exist, otherwise an exception will be thrown
*/
public getNodeData(ids: ID[]): NodeData[];
public getNodeData(id?: ID | ID[]): NodeData | NodeData[] {
if (id === undefined) return this.context.model.getNodeData();
if (Array.isArray(id)) return this.context.model.getNodeData(id);
return this.context.model.getNodeData([id])?.[0];
return this.context.model.getNodeLikeDatum(id);

Choose a reason for hiding this comment

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

The change from getNodeData([id])?.[0] to getNodeLikeDatum(id) improves performance by directly accessing the node datum without wrapping it in an array. Ensure that getNodeLikeDatum handles non-existent IDs appropriately to avoid runtime errors.

}

/**
Expand All @@ -594,6 +602,10 @@ export class Graph extends EventEmitter {
* const edge1 = graph.getEdgeData('edge-1');
* ```
* @apiCategory data
* @remarks
* <zh/> 边 id 必须存在,否则会抛出异常
*
* <en/> Edge id must exist, otherwise an exception will be thrown
*/
public getEdgeData(id: ID): EdgeData;
/**
Expand All @@ -607,12 +619,16 @@ export class Graph extends EventEmitter {
* const [edge1, edge2] = graph.getEdgeData(['edge-1', 'edge-2']);
* ```
* @apiCategory data
* @remarks
* <zh/> 数组中的每个边 id 必须存在,否则将抛出异常
*
* <en/> Each edge id in the array must exist, otherwise an exception will be thrown
*/
public getEdgeData(ids: ID[]): EdgeData[];
public getEdgeData(id?: ID | ID[]): EdgeData | EdgeData[] {
if (id === undefined) return this.context.model.getEdgeData();
if (Array.isArray(id)) return this.context.model.getEdgeData(id);
return this.context.model.getEdgeData([id])?.[0];
return this.context.model.getEdgeDatum(id);

Choose a reason for hiding this comment

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

The change from getEdgeData([id])?.[0] to getEdgeDatum(id) enhances performance by directly accessing the edge datum. Verify that getEdgeDatum properly manages cases where the ID does not exist to prevent potential exceptions.

}

/**
Expand All @@ -634,6 +650,10 @@ export class Graph extends EventEmitter {
* const combo1 = graph.getComboData('combo-1');
* ```
* @apiCategory data
* @remarks
* <zh/> 组合 id 必须存在,否则会抛出异常
*
* <en/> Combo id must exist, otherwise an exception will be thrown
*/
public getComboData(id: ID): ComboData;
/**
Expand All @@ -647,12 +667,16 @@ export class Graph extends EventEmitter {
* const [combo1, combo2] = graph.getComboData(['combo-1', 'combo-2']);
* ```
* @apiCategory data
* @remarks
* <zh/> 数组中的每个组合 id 必须存在,否则将抛出异常
*
* <en/> Each combo id in the array must exist, otherwise an exception will be thrown
*/
public getComboData(ids: ID[]): ComboData[];
public getComboData(id?: ID | ID[]): ComboData | ComboData[] {
if (id === undefined) return this.context.model.getComboData();
if (Array.isArray(id)) return this.context.model.getComboData(id);
return this.context.model.getComboData([id])?.[0];
return this.context.model.getNodeLikeDatum(id);

Choose a reason for hiding this comment

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

The update from getComboData([id])?.[0] to getNodeLikeDatum(id) optimizes the retrieval process by directly accessing the combo datum. Confirm that getNodeLikeDatum correctly handles scenarios where the combo ID is missing to avoid errors.

}

/**
Expand Down
Loading