out comment object state debounce
This commit is contained in:
@@ -10,6 +10,9 @@ import { UmbDeepState } from './deep-state.js';
|
||||
* The UmbObjectState provides methods to append data when the data is an Object.
|
||||
*/
|
||||
export class UmbObjectState<T> extends UmbDeepState<T> {
|
||||
#partialUpdateData?: Partial<T>;
|
||||
#partialUpdateDebounce?: boolean;
|
||||
|
||||
/**
|
||||
* @method update
|
||||
* @param {Partial<T>} partialData - A object containing some of the data to update in this Subject.
|
||||
@@ -20,8 +23,39 @@ export class UmbObjectState<T> extends UmbDeepState<T> {
|
||||
* const myState = new UmbObjectState(data);
|
||||
* myState.update({value: 'myNewValue'});
|
||||
*/
|
||||
|
||||
update(partialData: Partial<T>) {
|
||||
this.setValue({ ...this._subject.getValue(), ...partialData });
|
||||
return this;
|
||||
}
|
||||
/*
|
||||
update(partialData: Partial<T>) {
|
||||
this.#partialUpdateData = { ...this.#partialUpdateData, ...partialData };
|
||||
this.#performUpdate();
|
||||
return this;
|
||||
}
|
||||
|
||||
async #performUpdate() {
|
||||
if (this.#partialUpdateDebounce) return;
|
||||
this.#partialUpdateDebounce = true;
|
||||
await Promise.resolve();
|
||||
if (this.#partialUpdateData) {
|
||||
this.setValue({ ...this._subject.getValue(), ...this.#partialUpdateData });
|
||||
this.#partialUpdateData = undefined;
|
||||
}
|
||||
this.#partialUpdateDebounce = false;
|
||||
}
|
||||
|
||||
//getValue? — should this also include the partial update? but be aware that getValue is used for setValue comparison....!! [NL]
|
||||
|
||||
setValue(data: T): void {
|
||||
if (this.#partialUpdateData) {
|
||||
console.error('SetValue was called while in debouncing mode.');
|
||||
super.setValue({ ...data, ...this.#partialUpdateData });
|
||||
//this.#partialUpdateData = undefined; // maybe not, cause keeping this enables that to be merged in despite a another change coming from above.
|
||||
} else {
|
||||
super.setValue(data);
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user