Skip to content

Commit

Permalink
Fix: Skip unnecessary listener notify on set style
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexIchenskiy committed May 15, 2024
1 parent 7cd077b commit 88c3006
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 27 deletions.
32 changes: 20 additions & 12 deletions src/models/edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export interface IEdgePosition {
target: any;
}

export interface IEdgeSetStyleOptions {
isNotifySkipped: boolean;
}

export enum EdgeLineStyleType {
SOLID = 'solid',
DASHED = 'dashed',
Expand Down Expand Up @@ -115,10 +119,10 @@ export interface IEdge<N extends INodeBase, E extends IEdgeBase> extends ISubjec
setData(callback: (edge: IEdge<N, E>) => E): void;
patchData(data: Partial<E>): void;
patchData(callback: (edge: IEdge<N, E>) => Partial<E>): void;
setStyle(style: IEdgeStyle): void;
setStyle(callback: (edge: IEdge<N, E>) => IEdgeStyle): void;
patchStyle(style: IEdgeStyle): void;
patchStyle(callback: (edge: IEdge<N, E>) => IEdgeStyle): void;
setStyle(style: IEdgeStyle, options?: IEdgeSetStyleOptions): void;
setStyle(callback: (edge: IEdge<N, E>) => IEdgeStyle, options?: IEdgeSetStyleOptions): void;
patchStyle(style: IEdgeStyle, options?: IEdgeSetStyleOptions): void;
patchStyle(callback: (edge: IEdge<N, E>) => IEdgeStyle, options?: IEdgeSetStyleOptions): void;
setState(state: number): void;
setState(state: IGraphObjectStateParameters): void;
setState(callback: (edge: IEdge<N, E>) => number): void;
Expand Down Expand Up @@ -374,20 +378,22 @@ abstract class Edge<N extends INodeBase, E extends IEdgeBase> extends Subject im
this.notifyListeners();
}

setStyle(style: IEdgeStyle): void;
setStyle(callback: (edge: IEdge<N, E>) => IEdgeStyle): void;
setStyle(arg: IEdgeStyle | ((edge: IEdge<N, E>) => IEdgeStyle)): void {
setStyle(style: IEdgeStyle, options?: IEdgeSetStyleOptions): void;
setStyle(callback: (edge: IEdge<N, E>) => IEdgeStyle, options?: IEdgeSetStyleOptions): void;
setStyle(arg: IEdgeStyle | ((edge: IEdge<N, E>) => IEdgeStyle), options?: IEdgeSetStyleOptions): void {
if (isFunction(arg)) {
this._style = (arg as (edge: IEdge<N, E>) => IEdgeStyle)(this);
} else {
this._style = arg as IEdgeStyle;
}
this.notifyListeners();
if (!options?.isNotifySkipped) {
this.notifyListeners();
}
}

patchStyle(style: IEdgeStyle): void;
patchStyle(callback: (edge: IEdge<N, E>) => IEdgeStyle): void;
patchStyle(arg: IEdgeStyle | ((edge: IEdge<N, E>) => IEdgeStyle)) {
patchStyle(style: IEdgeStyle, options?: IEdgeSetStyleOptions): void;
patchStyle(callback: (edge: IEdge<N, E>) => IEdgeStyle, options?: IEdgeSetStyleOptions): void;
patchStyle(arg: IEdgeStyle | ((edge: IEdge<N, E>) => IEdgeStyle), options?: IEdgeSetStyleOptions) {
let style: IEdgeStyle;

if (isFunction(arg)) {
Expand All @@ -398,7 +404,9 @@ abstract class Edge<N extends INodeBase, E extends IEdgeBase> extends Subject im

patchProperties(this._style, style);

this.notifyListeners();
if (!options?.isNotifySkipped) {
this.notifyListeners();
}
}

setState(state: number): void;
Expand Down
6 changes: 3 additions & 3 deletions src/models/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ export class Graph<N extends INodeBase, E extends IEdgeBase> extends Subject imp
},
);
edge.setState(existingEdge.getState());
edge.setStyle(existingEdge.getStyle());
edge.setStyle(existingEdge.getStyle(), { isNotifySkipped: true });
newEdges.push(edge);
}

Expand Down Expand Up @@ -628,7 +628,7 @@ export class Graph<N extends INodeBase, E extends IEdgeBase> extends Subject imp

const style = this._defaultStyle.getNodeStyle(newNodes[i]);
if (style) {
newNodes[i].setStyle(style);
newNodes[i].setStyle(style, { isNotifySkipped: true });
// TODO Add these checks to node property setup
if (style.imageUrl) {
styleImageUrls.add(style.imageUrl);
Expand All @@ -649,7 +649,7 @@ export class Graph<N extends INodeBase, E extends IEdgeBase> extends Subject imp

const style = this._defaultStyle.getEdgeStyle(newEdges[i]);
if (style) {
newEdges[i].setStyle(style);
newEdges[i].setStyle(style, { isNotifySkipped: true });
}
}
}
Expand Down
32 changes: 20 additions & 12 deletions src/models/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export interface INodeSetPositionOptions {
isNotifySkipped: boolean;
}

export interface INodeSetStyleOptions {
isNotifySkipped: boolean;
}

export enum NodeShapeType {
CIRCLE = 'circle',
DOT = 'dot',
Expand Down Expand Up @@ -117,10 +121,10 @@ export interface INode<N extends INodeBase, E extends IEdgeBase> extends ISubjec
callback: (node: INode<N, E>) => INodeCoordinates | INodePosition,
options?: INodeSetPositionOptions,
): void;
setStyle(style: INodeStyle): void;
setStyle(callback: (node: INode<N, E>) => INodeStyle): void;
patchStyle(style: INodeStyle): void;
patchStyle(callback: (node: INode<N, E>) => INodeStyle): void;
setStyle(style: INodeStyle, options?: INodeSetPositionOptions): void;
setStyle(callback: (node: INode<N, E>) => INodeStyle, options?: INodeSetPositionOptions): void;
patchStyle(style: INodeStyle, options?: INodeSetPositionOptions): void;
patchStyle(callback: (node: INode<N, E>) => INodeStyle, options?: INodeSetPositionOptions): void;
setState(state: number): void;
setState(state: IGraphObjectStateParameters): void;
setState(callback: (node: INode<N, E>) => number): void;
Expand Down Expand Up @@ -478,20 +482,22 @@ export class Node<N extends INodeBase, E extends IEdgeBase> extends Subject impl
}
}

setStyle(style: INodeStyle): void;
setStyle(callback: (node: INode<N, E>) => INodeStyle): void;
setStyle(arg: INodeStyle | ((node: INode<N, E>) => INodeStyle)): void {
setStyle(style: INodeStyle, options?: INodeSetPositionOptions): void;
setStyle(callback: (node: INode<N, E>) => INodeStyle, options?: INodeSetPositionOptions): void;
setStyle(arg: INodeStyle | ((node: INode<N, E>) => INodeStyle), options?: INodeSetPositionOptions): void {
if (isFunction(arg)) {
this._style = (arg as (node: INode<N, E>) => INodeStyle)(this);
} else {
this._style = arg as INodeStyle;
}
this.notifyListeners();
if (!options?.isNotifySkipped) {
this.notifyListeners();
}
}

patchStyle(style: INodeStyle): void;
patchStyle(callback: (node: INode<N, E>) => INodeStyle): void;
patchStyle(arg: INodeStyle | ((node: INode<N, E>) => INodeStyle)) {
patchStyle(style: INodeStyle, options?: INodeSetPositionOptions): void;
patchStyle(callback: (node: INode<N, E>) => INodeStyle, options?: INodeSetPositionOptions): void;
patchStyle(arg: INodeStyle | ((node: INode<N, E>) => INodeStyle), options?: INodeSetPositionOptions) {
let style: INodeStyle;

if (isFunction(arg)) {
Expand All @@ -502,7 +508,9 @@ export class Node<N extends INodeBase, E extends IEdgeBase> extends Subject impl

patchProperties(this._style, style);

this.notifyListeners();
if (!options?.isNotifySkipped) {
this.notifyListeners();
}
}

setState(state: number): void;
Expand Down

0 comments on commit 88c3006

Please sign in to comment.