Skip to content

Commit f862736

Browse files
committed
make sure callback can be undefined
1 parent 75e7bb4 commit f862736

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

src/Umbraco.Web.UI.Client/src/libs/class-api/class.mixin.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,25 @@ export const UmbClassMixin = <T extends ClassConstructor<EventTarget>>(superClas
5353
>(
5454
// 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]
5555
source: ObservableType,
56-
callback: ObserverCallback<SpecificT>,
56+
callback?: ObserverCallback<SpecificT>,
5757
controllerAlias?: UmbControllerAlias | null,
5858
): SpecificR {
59-
// Fallback to use a hash of the provided method, but only if the alias is undefined.
60-
controllerAlias ??= controllerAlias === undefined ? simpleHashCode(callback.toString()) : undefined;
59+
// Fallback to use a hash of the provided method, but only if the alias is undefined and there is a callback.
60+
if (controllerAlias === undefined && callback) {
61+
controllerAlias = simpleHashCode(callback.toString());
62+
} else {
63+
controllerAlias = undefined;
64+
}
6165

6266
if (source) {
6367
return new UmbObserverController<T>(
6468
this,
6569
source,
66-
callback as unknown as ObserverCallback<T>,
70+
callback as unknown as ObserverCallback<T> | undefined,
6771
controllerAlias,
6872
) as unknown as SpecificR;
6973
} else {
70-
callback(undefined as SpecificT);
74+
callback?.(undefined as SpecificT);
7175
this.removeUmbControllerByAlias(controllerAlias);
7276
return undefined as SpecificR;
7377
}

src/Umbraco.Web.UI.Client/src/libs/element-api/element.mixin.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,25 @@ export const UmbElementMixin = <T extends HTMLElementConstructor>(superClass: T)
2929
>(
3030
// 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]
3131
source: ObservableType,
32-
callback: ObserverCallback<SpecificT>,
32+
callback?: ObserverCallback<SpecificT>,
3333
controllerAlias?: UmbControllerAlias | null,
3434
): SpecificR {
35-
// Fallback to use a hash of the provided method, but only if the alias is undefined.
36-
controllerAlias ??= controllerAlias === undefined ? simpleHashCode(callback.toString()) : undefined;
35+
// Fallback to use a hash of the provided method, but only if the alias is undefined and there is a callback.
36+
if (controllerAlias === undefined && callback) {
37+
controllerAlias = simpleHashCode(callback.toString());
38+
} else {
39+
controllerAlias = undefined;
40+
}
3741

3842
if (source) {
3943
return new UmbObserverController<T>(
4044
this,
4145
source,
42-
callback as unknown as ObserverCallback<T>,
46+
callback as unknown as ObserverCallback<T> | undefined,
4347
controllerAlias,
4448
) as unknown as SpecificR;
4549
} else {
46-
callback(undefined as SpecificT);
50+
callback?.(undefined as SpecificT);
4751
this.removeUmbControllerByAlias(controllerAlias);
4852
return undefined as SpecificR;
4953
}

0 commit comments

Comments
 (0)