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

🥸 : Optimise updating state #3

Merged
merged 5 commits into from
Jul 30, 2022
Merged
Changes from all 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
19 changes: 16 additions & 3 deletions lib/State.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,30 @@ class State<T> implements StateObject<T> {
this.onChange = onChange;
}

/**
* Checks if the value has been updated or not to optimise performance.
*
* @type {T} Generic type for the value
* @param {T} newValue
* @returns {boolean}
*/
verifyValue(newValue: T): boolean {
return this.value !== newValue;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, I compared if it is equal to the new value and resulted in a bug, that will prevent from setting the state value. Now, we will verify if the value is not same as the old one.

}

/**
* Sets the value to the `newValue` argument.
*
* @type {T} Generic type for the value
* @param newValue {T}
*/
set(newValue: T) {
this.value = newValue;
if (this.verifyValue(newValue)) {
this.value = newValue;

if (this.onChange) {
this.onChange(this.value);
if (this.onChange) {
this.onChange(this.value);
}
}
}

Expand Down