From f86273616316344b45da8644b8f14910de9d8475 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20Lyngs=C3=B8?= Date: Thu, 9 Jan 2025 20:23:28 +0100 Subject: [PATCH 1/2] make sure callback can be undefined --- .../src/libs/class-api/class.mixin.ts | 14 +++++++++----- .../src/libs/element-api/element.mixin.ts | 14 +++++++++----- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/libs/class-api/class.mixin.ts b/src/Umbraco.Web.UI.Client/src/libs/class-api/class.mixin.ts index d727346364..3cb7f91fc0 100644 --- a/src/Umbraco.Web.UI.Client/src/libs/class-api/class.mixin.ts +++ b/src/Umbraco.Web.UI.Client/src/libs/class-api/class.mixin.ts @@ -53,21 +53,25 @@ export const UmbClassMixin = >(superClas >( // This type dance checks if the Observable given could be undefined, if it potentially could be undefined it means that this potentially could return undefined and then call the callback with undefined. [NL] source: ObservableType, - callback: ObserverCallback, + callback?: ObserverCallback, controllerAlias?: UmbControllerAlias | null, ): SpecificR { - // Fallback to use a hash of the provided method, but only if the alias is undefined. - controllerAlias ??= controllerAlias === undefined ? simpleHashCode(callback.toString()) : undefined; + // Fallback to use a hash of the provided method, but only if the alias is undefined and there is a callback. + if (controllerAlias === undefined && callback) { + controllerAlias = simpleHashCode(callback.toString()); + } else { + controllerAlias = undefined; + } if (source) { return new UmbObserverController( this, source, - callback as unknown as ObserverCallback, + callback as unknown as ObserverCallback | undefined, controllerAlias, ) as unknown as SpecificR; } else { - callback(undefined as SpecificT); + callback?.(undefined as SpecificT); this.removeUmbControllerByAlias(controllerAlias); return undefined as SpecificR; } diff --git a/src/Umbraco.Web.UI.Client/src/libs/element-api/element.mixin.ts b/src/Umbraco.Web.UI.Client/src/libs/element-api/element.mixin.ts index 45f24e3351..dfb35472e9 100644 --- a/src/Umbraco.Web.UI.Client/src/libs/element-api/element.mixin.ts +++ b/src/Umbraco.Web.UI.Client/src/libs/element-api/element.mixin.ts @@ -29,21 +29,25 @@ export const UmbElementMixin = (superClass: T) >( // This type dance checks if the Observable given could be undefined, if it potentially could be undefined it means that this potentially could return undefined and then call the callback with undefined. [NL] source: ObservableType, - callback: ObserverCallback, + callback?: ObserverCallback, controllerAlias?: UmbControllerAlias | null, ): SpecificR { - // Fallback to use a hash of the provided method, but only if the alias is undefined. - controllerAlias ??= controllerAlias === undefined ? simpleHashCode(callback.toString()) : undefined; + // Fallback to use a hash of the provided method, but only if the alias is undefined and there is a callback. + if (controllerAlias === undefined && callback) { + controllerAlias = simpleHashCode(callback.toString()); + } else { + controllerAlias = undefined; + } if (source) { return new UmbObserverController( this, source, - callback as unknown as ObserverCallback, + callback as unknown as ObserverCallback | undefined, controllerAlias, ) as unknown as SpecificR; } else { - callback(undefined as SpecificT); + callback?.(undefined as SpecificT); this.removeUmbControllerByAlias(controllerAlias); return undefined as SpecificR; } From bd456a1dfaf6dcdd869f2adc059fd963740bf5e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20Lyngs=C3=B8?= Date: Thu, 9 Jan 2025 20:28:58 +0100 Subject: [PATCH 2/2] correct null case --- src/Umbraco.Web.UI.Client/src/libs/class-api/class.mixin.ts | 3 ++- .../src/libs/element-api/element.mixin.ts | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/libs/class-api/class.mixin.ts b/src/Umbraco.Web.UI.Client/src/libs/class-api/class.mixin.ts index 3cb7f91fc0..0b1c893bcd 100644 --- a/src/Umbraco.Web.UI.Client/src/libs/class-api/class.mixin.ts +++ b/src/Umbraco.Web.UI.Client/src/libs/class-api/class.mixin.ts @@ -59,7 +59,8 @@ export const UmbClassMixin = >(superClas // Fallback to use a hash of the provided method, but only if the alias is undefined and there is a callback. if (controllerAlias === undefined && callback) { controllerAlias = simpleHashCode(callback.toString()); - } else { + } else if (controllerAlias === null) { + // if value is null, then reset it to undefined. Null is used to explicitly tell that we do not want a controller alias. [NL] controllerAlias = undefined; } diff --git a/src/Umbraco.Web.UI.Client/src/libs/element-api/element.mixin.ts b/src/Umbraco.Web.UI.Client/src/libs/element-api/element.mixin.ts index dfb35472e9..9b46710c58 100644 --- a/src/Umbraco.Web.UI.Client/src/libs/element-api/element.mixin.ts +++ b/src/Umbraco.Web.UI.Client/src/libs/element-api/element.mixin.ts @@ -35,7 +35,8 @@ export const UmbElementMixin = (superClass: T) // Fallback to use a hash of the provided method, but only if the alias is undefined and there is a callback. if (controllerAlias === undefined && callback) { controllerAlias = simpleHashCode(callback.toString()); - } else { + } else if (controllerAlias === null) { + // if value is null, then reset it to undefined. Null is used to explicitly tell that we do not want a controller alias. [NL] controllerAlias = undefined; }