Skip to content

Commit

Permalink
Chore: Refactor state types
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexIchenskiy committed Mar 14, 2024
1 parent f94769a commit 1022aa9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
20 changes: 11 additions & 9 deletions src/models/edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,25 +422,27 @@ abstract class Edge<N extends INodeBase, E extends IEdgeBase> extends Subject im

if (isNumber(result)) {
this._state = result;
} else if (isPlainObject(result) && result.options) {
} else if (isPlainObject(result)) {
const options = result.options;

this._state = this._handleState(result.state, options);

this.notifyListeners({
id: this.id,
type: 'edge',
options: options,
});
if (options) {
this.notifyListeners({
id: this.id,
type: 'edge',
options: options,
});

return;
return;
}
}

this.notifyListeners();
}

private _handleState(state: number, options: IGraphObjectStateOptions): number {
if (options.isToggle && this._state === state) {
private _handleState(state: number, options?: Partial<IGraphObjectStateOptions>): number {
if (options?.isToggle && this._state === state) {
return GraphObjectState.NONE;
} else {
return state;
Expand Down
20 changes: 11 additions & 9 deletions src/models/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,18 +548,20 @@ export class Node<N extends INodeBase, E extends IEdgeBase> extends Subject impl

if (isNumber(result)) {
this._state = result;
} else if (isPlainObject(result) && result.options) {
} else if (isPlainObject(result)) {
const options = result.options;

this._state = this._handleState(result.state, options);

this.notifyListeners({
id: this.id,
type: 'node',
options: options,
});
if (options) {
this.notifyListeners({
id: this.id,
type: 'node',
options: options,
});

return;
return;
}
}

this.notifyListeners();
Expand All @@ -569,8 +571,8 @@ export class Node<N extends INodeBase, E extends IEdgeBase> extends Subject impl
return isPointInRectangle(this.getBoundingBox(), point);
}

private _handleState(state: number, options: IGraphObjectStateOptions): number {
if (options.isToggle && this._state === state) {
private _handleState(state: number, options?: Partial<IGraphObjectStateOptions>): number {
if (options?.isToggle && this._state === state) {
return GraphObjectState.NONE;
} else {
return state;
Expand Down
8 changes: 4 additions & 4 deletions src/models/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ export const GraphObjectState = {
};

export interface IGraphObjectStateOptions {
isToggle?: boolean;
isSingle?: boolean;
isToggle: boolean;
isSingle: boolean;
}

export interface IGraphObjectStateParameters {
state: number;
options: IGraphObjectStateOptions;
options?: Partial<IGraphObjectStateOptions>;
}

export interface ISetStateDataPayload {
id: any;
type: GraphObject;
options: IGraphObjectStateOptions;
options: Partial<IGraphObjectStateOptions>;
}

0 comments on commit 1022aa9

Please sign in to comment.