Skip to content

Commit

Permalink
Chore: Refactor remove return values
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexIchenskiy committed Mar 14, 2024
1 parent 53c615d commit 5432587
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/models/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ import { IGraphStyle } from './style';
import { ImageHandler } from '../services/images';
import { getEdgeOffsets } from './topology';
import { IEntityState, EntityState } from '../utils/entity.utils';
import { dedupArrays } from '../utils/array.utils';

export interface IGraphData<N extends INodeBase, E extends IEdgeBase> {
nodes: N[];
edges: E[];
}

export interface IGraphObjectsIds {
nodeIds: any[];
edgeIds: any[];
}

export type IEdgeFilter<N extends INodeBase, E extends IEdgeBase> = (edge: IEdge<N, E>) => boolean;

export type INodeFilter<N extends INodeBase, E extends IEdgeBase> = (node: INode<N, E>) => boolean;
Expand All @@ -33,7 +39,10 @@ export interface IGraph<N extends INodeBase, E extends IEdgeBase> {
setup(data: Partial<IGraphData<N, E>>): void;
clearPositions(): void;
merge(data: Partial<IGraphData<N, E>>): void;
remove(data: Partial<{ nodeIds: number[]; edgeIds: number[] }>): void;
remove(data: Partial<IGraphObjectsIds>): void;
removeAll(): void;
removeAllNodes(): void;
removeAllEdges(): void;
isEqual<T extends INodeBase, K extends IEdgeBase>(graph: Graph<T, K>): boolean;
getBoundingBox(): IRectangle;
getNearestNode(point: IPosition): INode<N, E> | undefined;
Expand All @@ -47,7 +56,7 @@ export interface IGraphSettings<N extends INodeBase, E extends IEdgeBase> {
onLoadedImages?: () => void;
onSetupData?: (data: Partial<IGraphData<N, E>>) => void;
onMergeData?: (data: Partial<IGraphData<N, E>>) => void;
onRemoveData?: (data: Partial<{ nodeIds: any[]; edgeIds: any[] }>) => void;
onRemoveData?: (data: Partial<IGraphObjectsIds>) => void;
}

export class Graph<N extends INodeBase, E extends IEdgeBase> implements IGraph<N, E> {
Expand Down Expand Up @@ -263,17 +272,17 @@ export class Graph<N extends INodeBase, E extends IEdgeBase> implements IGraph<N
this._settings?.onMergeData?.(data);
}

remove(data: Partial<{ nodeIds: number[]; edgeIds: number[] }>) {
remove(data: Partial<IGraphObjectsIds>) {
const nodeIds = data.nodeIds ?? [];
const edgeIds = data.edgeIds ?? [];

const removedData = this._removeNodes(nodeIds);
const removedEdgeIds = this._removeEdges(edgeIds);
const removedNodesData = this._removeNodes(nodeIds);
const removedEdgesData = this._removeEdges(edgeIds);

// Merge edges removed by removing nodes and by removing edges, ensuring there are no duplicate edge IDs.
removedData.edgeIds = removedData.edgeIds.concat(
removedEdgeIds.filter((edgeId) => removedData.edgeIds.indexOf(edgeId) < 0),
);
const removedData = {
nodeIds: dedupArrays(removedNodesData.nodeIds, removedEdgesData.nodeIds),
edgeIds: dedupArrays(removedNodesData.edgeIds, removedEdgesData.edgeIds),
};

this._applyEdgeOffsets();
this._applyStyle();
Expand Down Expand Up @@ -497,7 +506,7 @@ export class Graph<N extends INodeBase, E extends IEdgeBase> implements IGraph<N
this._edges.removeMany(removedEdgeIds);
}

private _removeNodes(nodeIds: any[]): { nodeIds: any[]; edgeIds: any[] } {
private _removeNodes(nodeIds: any[]): IGraphObjectsIds {
const removedNodeIds: any[] = [];
const removedEdgeIds: any[] = [];

Expand All @@ -523,7 +532,7 @@ export class Graph<N extends INodeBase, E extends IEdgeBase> implements IGraph<N
return { nodeIds: removedNodeIds, edgeIds: removedEdgeIds };
}

private _removeEdges(edgeIds: any[]): any[] {
private _removeEdges(edgeIds: any[]): IGraphObjectsIds {
const removedEdgeIds: any[] = [];

for (let i = 0; i < edgeIds.length; i++) {
Expand All @@ -538,7 +547,7 @@ export class Graph<N extends INodeBase, E extends IEdgeBase> implements IGraph<N
}
this._edges.removeMany(removedEdgeIds);

return removedEdgeIds;
return { nodeIds: [], edgeIds: removedEdgeIds };
}

private _applyEdgeOffsets() {
Expand Down
5 changes: 5 additions & 0 deletions src/utils/array.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ export const copyArray = <T>(array: Array<T>): Array<T> => {
}
return newArray;
};

export const dedupArrays = <T>(...arrays: T[][]): T[] => {
const combinedArray = arrays.reduce((acc, curr) => acc.concat(curr), []);
return Array.from(new Set(combinedArray));
};

0 comments on commit 5432587

Please sign in to comment.