diff --git a/lib/State.ts b/lib/State.ts index 8318dab..59042f1 100644 --- a/lib/State.ts +++ b/lib/State.ts @@ -24,6 +24,17 @@ class State implements StateObject { 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; + } + /** * Sets the value to the `newValue` argument. * @@ -31,10 +42,12 @@ class State implements StateObject { * @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); + } } }