Skip to content

Commit 40e2058

Browse files
committed
fix(lib): rework loadingSuccess and loadingError outputs to inputs (pass in handler)
- the outputs don't work on * based structural directives - desugared ng-template based syntax doesn't work for this use case (retrieve tag, ...)
1 parent 1283dae commit 40e2058

4 files changed

Lines changed: 24 additions & 13 deletions

File tree

projects/elements-demo/src/app/features/examples/basic/basic.component.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ <h2 id="error-template">
9292
'https://unpkg.com/wrong-url.js?module';
9393
loadingTemplate: loading;
9494
errorTemplate: error;
95-
module: true
95+
module: true;
96+
loadingError: customLoadingErrorHandler
9697
"
9798
raised
9899
>

projects/elements-demo/src/app/features/examples/basic/basic.component.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ export class BasicComponent {
5252
onSliderChange(value: number) {
5353
this.xAxis = [-value, value];
5454
}
55+
56+
customLoadingErrorHandler(error: ErrorEvent) {
57+
console.log(
58+
'[Optional custom loading error handler] Loading failed:',
59+
error,
60+
);
61+
}
5562
}
5663

5764
const CODE_EXAMPLE_1 = `<!-- url = 'https://unpkg.com/@material/mwc-icon@0.27.0/mwc-icon.js?module'; -->

projects/elements/src/lib/lazy-elements/lazy-element-dynamic/lazy-element-dynamic.directive.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ export class LazyElementDynamicDirective implements OnInit {
3434
errorTemplateRef: TemplateRef<any> | null = null;
3535
@Input('axLazyElementDynamicModule') isModule = false; // eslint-disable-line @angular-eslint/no-input-rename
3636
@Input('axLazyElementDynamicImportMap') importMap = false; // eslint-disable-line @angular-eslint/no-input-rename
37-
38-
loadingSuccess = output<void>();
39-
loadingError = output<ErrorEvent>();
37+
@Input('axLazyElementLoadingSuccess') loadingSuccess?: () => void;
38+
@Input('axLazyElementLoadingError') loadingError?: (
39+
error: ErrorEvent,
40+
) => void;
4041

4142
#viewRef: EmbeddedViewRef<any> | null = null;
4243

@@ -98,7 +99,7 @@ export class LazyElementDynamicDirective implements OnInit {
9899
)
99100
.subscribe({
100101
next: () => {
101-
this.loadingSuccess.emit();
102+
this.loadingSuccess?.();
102103
this.#vcr.clear();
103104
const originalCreateElement = this.#renderer.createElement;
104105
this.#renderer.createElement = (name: string, namespace: string) => {
@@ -112,7 +113,7 @@ export class LazyElementDynamicDirective implements OnInit {
112113
this.#cdr.markForCheck();
113114
},
114115
error: (error) => {
115-
this.loadingError.emit(error);
116+
this.loadingError?.(error);
116117
const errorComponent =
117118
elementConfig.errorComponent || options.errorComponent;
118119
this.#vcr.clear();
@@ -122,7 +123,7 @@ export class LazyElementDynamicDirective implements OnInit {
122123
} else if (errorComponent) {
123124
this.#vcr.createComponent(errorComponent);
124125
this.#cdr.markForCheck();
125-
} else if (ngDevMode) {
126+
} else if (ngDevMode && !this.loadingError) {
126127
console.error(
127128
`${LOG_PREFIX} - Loading of element <${this.tag}> failed, please provide <ng-template #error>Loading failed...</ng-template> and reference it in *axLazyElementDynamic="errorTemplate: error" to display customized error message in place of element\n\n`,
128129
error,

projects/elements/src/lib/lazy-elements/lazy-element/lazy-element.directive.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ export class LazyElementDirective implements OnInit, OnChanges {
5151
errorTemplateRef: TemplateRef<any> | null = null;
5252
@Input('axLazyElementModule') isModule?: boolean; // eslint-disable-line @angular-eslint/no-input-rename
5353
@Input('axLazyElementImportMap') importMap = false; // eslint-disable-line @angular-eslint/no-input-rename
54-
55-
loadingSuccess = output<void>();
56-
loadingError = output<ErrorEvent>();
54+
@Input('axLazyElementLoadingSuccess') loadingSuccess?: () => void;
55+
@Input('axLazyElementLoadingError') loadingError?: (
56+
error: ErrorEvent,
57+
) => void;
5758

5859
#viewRef: EmbeddedViewRef<any> | null = null;
5960
#url$ = new BehaviorSubject<string | null>(null);
@@ -86,6 +87,7 @@ export class LazyElementDirective implements OnInit, OnChanges {
8687

8788
#setupUrlListener(): void {
8889
const tpl = this.#template as any;
90+
console.log(tpl);
8991
const elementTag = tpl._declarationTContainer
9092
? tpl._declarationTContainer.tagName || tpl._declarationTContainer.value
9193
: tpl._def.element.#template.nodes[0].element.name;
@@ -120,7 +122,7 @@ export class LazyElementDirective implements OnInit, OnChanges {
120122
),
121123
).pipe(
122124
catchError((error) => {
123-
this.loadingError.emit(error);
125+
this.loadingError?.(error);
124126
this.#vcr.clear();
125127
const errorComponent =
126128
elementConfig.errorComponent || options.errorComponent;
@@ -130,7 +132,7 @@ export class LazyElementDirective implements OnInit, OnChanges {
130132
} else if (errorComponent) {
131133
this.#vcr.createComponent(errorComponent);
132134
this.#cdr.markForCheck();
133-
} else if (ngDevMode) {
135+
} else if (ngDevMode && !this.loadingError) {
134136
console.error(
135137
`${LOG_PREFIX} - Loading of element <${elementTag}> failed, please provide <ng-template #error>Loading failed...</ng-template> and reference it in *axLazyElement="errorTemplate: error" to display customized error message in place of element`,
136138
);
@@ -139,7 +141,7 @@ export class LazyElementDirective implements OnInit, OnChanges {
139141
}),
140142
);
141143
}),
142-
tap(() => this.loadingSuccess.emit()),
144+
tap(() => this.loadingSuccess?.()),
143145
mergeMap(() => customElements.whenDefined(elementTag)),
144146
takeUntilDestroyed(this.#destroyRef),
145147
)

0 commit comments

Comments
 (0)