diff --git a/src/Umbraco.Web.UI.Client/src/libs/observable-api/states/string-state.ts b/src/Umbraco.Web.UI.Client/src/libs/observable-api/states/string-state.ts index f404e762ef..7208de66fc 100644 --- a/src/Umbraco.Web.UI.Client/src/libs/observable-api/states/string-state.ts +++ b/src/Umbraco.Web.UI.Client/src/libs/observable-api/states/string-state.ts @@ -1,5 +1,5 @@ import type { MappingFunction, MemoizationFunction } from '../types/index.js'; -import { createObservablePart } from '../utils/index.js'; +import { createObservablePart, strictEqualityMemoization } from '../utils/index.js'; import { UmbBasicState } from './basic-state.js'; /** @@ -17,6 +17,6 @@ export class UmbStringState extends UmbBasicState { mappingFunction: MappingFunction, memoizationFunction?: MemoizationFunction, ) { - return createObservablePart(this._subject, mappingFunction, memoizationFunction); + return createObservablePart(this._subject, mappingFunction, memoizationFunction ?? strictEqualityMemoization); } } diff --git a/src/Umbraco.Web.UI.Client/src/libs/observable-api/utils/deep-freeze.function.ts b/src/Umbraco.Web.UI.Client/src/libs/observable-api/utils/deep-freeze.function.ts index 6dc099932d..6c188be72a 100644 --- a/src/Umbraco.Web.UI.Client/src/libs/observable-api/utils/deep-freeze.function.ts +++ b/src/Umbraco.Web.UI.Client/src/libs/observable-api/utils/deep-freeze.function.ts @@ -1,3 +1,14 @@ +/** + * Deep freeze an object and all its properties. + * @param {T} inObj - The object to deep freeze. + * @returns {T} - The deep frozen object. + * @description - Deep freezes an object and all its properties. + * @example Example of deep freezing an object. + * const myObject = {a: 1, b: {c: 2}}; + * const frozenObject = deepFreeze(myObject); + * frozenObject.a = 3; // Throws an error. + * frozenObject.b.c = 4; // Throws an error. + */ export const deepFreeze = Object.freeze(function deepFreezeImpl(inObj: T): T { if (inObj != null && typeof inObj === 'object') { Object.freeze(inObj); diff --git a/src/Umbraco.Web.UI.Client/src/libs/observable-api/utils/index.ts b/src/Umbraco.Web.UI.Client/src/libs/observable-api/utils/index.ts index 11f244f56d..0c77207194 100644 --- a/src/Umbraco.Web.UI.Client/src/libs/observable-api/utils/index.ts +++ b/src/Umbraco.Web.UI.Client/src/libs/observable-api/utils/index.ts @@ -3,9 +3,10 @@ export * from './create-observable-part.function.js'; export * from './deep-freeze.function.js'; export * from './default-memoization.function.js'; export * from './filter-frozen-array.function.js'; -export * from './merge-observables.function.js'; export * from './json-string-comparison.function.js'; +export * from './merge-observables.function.js'; export * from './observe-multiple.function.js'; export * from './partial-update-frozen-array.function.js'; export * from './push-to-unique-array.function.js'; export * from './simple-hash-code.function.js'; +export * from './strict-equality-memoization.function.js'; diff --git a/src/Umbraco.Web.UI.Client/src/libs/observable-api/utils/merge-observables.function.ts b/src/Umbraco.Web.UI.Client/src/libs/observable-api/utils/merge-observables.function.ts index 8038b21baa..4824eaa926 100644 --- a/src/Umbraco.Web.UI.Client/src/libs/observable-api/utils/merge-observables.function.ts +++ b/src/Umbraco.Web.UI.Client/src/libs/observable-api/utils/merge-observables.function.ts @@ -37,7 +37,7 @@ export function mergeObservables< ): Observable { return combineLatest(sources).pipe( map(mergeFunction as MappingFunction), - distinctUntilChanged(memoizationFunction || defaultMemoization), + distinctUntilChanged(memoizationFunction ?? defaultMemoization), shareReplay(1), ); } diff --git a/src/Umbraco.Web.UI.Client/src/libs/observable-api/utils/strict-equality-memoization.function.ts b/src/Umbraco.Web.UI.Client/src/libs/observable-api/utils/strict-equality-memoization.function.ts new file mode 100644 index 0000000000..ca2dc61675 --- /dev/null +++ b/src/Umbraco.Web.UI.Client/src/libs/observable-api/utils/strict-equality-memoization.function.ts @@ -0,0 +1,11 @@ +/** + * @export + * @method strictEqualityMemoization + * @param {unknown} previousValue - The previous value to compare. + * @param {unknown} currentValue - The current value to compare. + * @returns {boolean} - Returns true if the values are identical. + * @description - Compares the two values using strict equality. + */ +export function strictEqualityMemoization(previousValue: unknown, currentValue: unknown): boolean { + return previousValue === currentValue; +}