From 1022aa943fd9bc7f8c8caa864e721009cbb17186 Mon Sep 17 00:00:00 2001 From: AlexIchenskiy Date: Thu, 14 Mar 2024 09:16:47 +0100 Subject: [PATCH] Chore: Refactor state types --- src/models/edge.ts | 20 +++++++++++--------- src/models/node.ts | 20 +++++++++++--------- src/models/state.ts | 8 ++++---- 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/src/models/edge.ts b/src/models/edge.ts index 9d150ce..4b36145 100644 --- a/src/models/edge.ts +++ b/src/models/edge.ts @@ -422,25 +422,27 @@ abstract class Edge 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): number { + if (options?.isToggle && this._state === state) { return GraphObjectState.NONE; } else { return state; diff --git a/src/models/node.ts b/src/models/node.ts index b99a15f..1715c26 100644 --- a/src/models/node.ts +++ b/src/models/node.ts @@ -548,18 +548,20 @@ export class Node 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(); @@ -569,8 +571,8 @@ export class Node 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): number { + if (options?.isToggle && this._state === state) { return GraphObjectState.NONE; } else { return state; diff --git a/src/models/state.ts b/src/models/state.ts index 785f171..6a06839 100644 --- a/src/models/state.ts +++ b/src/models/state.ts @@ -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; } export interface ISetStateDataPayload { id: any; type: GraphObject; - options: IGraphObjectStateOptions; + options: Partial; }