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

refact: Optimize Rect _onValueChanged callback. #2407

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Changes from 2 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
31 changes: 23 additions & 8 deletions packages/math/src/Rect.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { IClone } from "./IClone";
import { ICopy } from "./ICopy";

// Empty function for initialization of _onValueChangedCallback .
function emptyFunc(): void {}

// A 2d rectangle defined by x and y position, width and height.
export class Rect implements IClone<Rect>, ICopy<Rect, Rect> {
/** @internal */
Expand All @@ -12,8 +15,20 @@
/** @internal */
_height: number;
/** @internal */
_onValueChanged: () => void = null;

get _onValueChanged(): () => void {
if (this._onValueChangedCallback === emptyFunc) {
return null;

Check warning on line 20 in packages/math/src/Rect.ts

View check run for this annotation

Codecov / codecov/patch

packages/math/src/Rect.ts#L20

Added line #L20 was not covered by tests
}
return this._onValueChangedCallback;

Check warning on line 22 in packages/math/src/Rect.ts

View check run for this annotation

Codecov / codecov/patch

packages/math/src/Rect.ts#L22

Added line #L22 was not covered by tests
}
set _onValueChanged(callback: () => void | null) {
chenyunda218 marked this conversation as resolved.
Show resolved Hide resolved
if (callback) {
this._onValueChangedCallback = callback;
} else {
this._onValueChangedCallback = emptyFunc;
}
chenyunda218 marked this conversation as resolved.
Show resolved Hide resolved
}
private _onValueChangedCallback: () => void = emptyFunc;
/**
* The x coordinate of the rectangle.
*/
Expand All @@ -23,7 +38,7 @@

set x(value: number) {
this._x = value;
this._onValueChanged && this._onValueChanged();
this._onValueChangedCallback();

Check warning on line 41 in packages/math/src/Rect.ts

View check run for this annotation

Codecov / codecov/patch

packages/math/src/Rect.ts#L41

Added line #L41 was not covered by tests
}

/**
Expand All @@ -35,7 +50,7 @@

set y(value: number) {
this._y = value;
this._onValueChanged && this._onValueChanged();
this._onValueChangedCallback();

Check warning on line 53 in packages/math/src/Rect.ts

View check run for this annotation

Codecov / codecov/patch

packages/math/src/Rect.ts#L53

Added line #L53 was not covered by tests
}

/**
Expand All @@ -47,7 +62,7 @@

set width(value: number) {
this._width = value;
this._onValueChanged && this._onValueChanged();
this._onValueChangedCallback();

Check warning on line 65 in packages/math/src/Rect.ts

View check run for this annotation

Codecov / codecov/patch

packages/math/src/Rect.ts#L65

Added line #L65 was not covered by tests
}

/**
Expand All @@ -59,7 +74,7 @@

set height(value: number) {
this._height = value;
this._onValueChanged && this._onValueChanged();
this._onValueChangedCallback();

Check warning on line 77 in packages/math/src/Rect.ts

View check run for this annotation

Codecov / codecov/patch

packages/math/src/Rect.ts#L77

Added line #L77 was not covered by tests
}

/**
Expand Down Expand Up @@ -89,7 +104,7 @@
this._y = y;
this._width = width;
this._height = height;
this._onValueChanged && this._onValueChanged();
this._onValueChangedCallback();
return this;
}

Expand All @@ -111,7 +126,7 @@
this._y = source.y;
this._width = source.width;
this._height = source.height;
this._onValueChanged && this._onValueChanged();
this._onValueChangedCallback();
return this;
}
}
Loading