From de08acfb18803d1683c8ce855b8ca3290e87d69f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20Lyngs=C3=B8?= Date: Tue, 21 May 2024 13:49:40 +0200 Subject: [PATCH] out comment object state debounce --- .../observable-api/states/object-state.ts | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/Umbraco.Web.UI.Client/src/libs/observable-api/states/object-state.ts b/src/Umbraco.Web.UI.Client/src/libs/observable-api/states/object-state.ts index 815bee0865..fdcafa1bac 100644 --- a/src/Umbraco.Web.UI.Client/src/libs/observable-api/states/object-state.ts +++ b/src/Umbraco.Web.UI.Client/src/libs/observable-api/states/object-state.ts @@ -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 extends UmbDeepState { + #partialUpdateData?: Partial; + #partialUpdateDebounce?: boolean; + /** * @method update * @param {Partial} partialData - A object containing some of the data to update in this Subject. @@ -20,8 +23,39 @@ export class UmbObjectState extends UmbDeepState { * const myState = new UmbObjectState(data); * myState.update({value: 'myNewValue'}); */ + update(partialData: Partial) { this.setValue({ ...this._subject.getValue(), ...partialData }); return this; } + /* + update(partialData: Partial) { + 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); + } + } + */ }