fix: address late init from ionic components

This commit is contained in:
2022-08-19 14:51:22 +02:00
committed by Rainer Killinger
parent 2130d84920
commit 0bce9e5452
2 changed files with 34 additions and 7 deletions

View File

@@ -30,6 +30,8 @@ import {HebisDataProvider} from '../hebis-data.provider';
import {HebisDetailComponent} from './hebis-detail.component'; import {HebisDetailComponent} from './hebis-detail.component';
import {Observable, of} from 'rxjs'; import {Observable, of} from 'rxjs';
import {StorageProvider} from '../../storage/storage.provider'; import {StorageProvider} from '../../storage/storage.provider';
import {IonicModule} from '@ionic/angular';
import {IonIconModule} from '../../../util/ion-icon/ion-icon.module';
const translations: any = {data: {detail: {TITLE: 'Foo'}}}; const translations: any = {data: {detail: {TITLE: 'Foo'}}};
@@ -75,6 +77,8 @@ describe('HebisDetailComponent', () => {
RouterModule.forRoot([], {relativeLinkResolution: 'legacy'}), RouterModule.forRoot([], {relativeLinkResolution: 'legacy'}),
HebisRoutingModule, HebisRoutingModule,
HebisModule, HebisModule,
IonicModule,
IonIconModule,
TranslateModule.forRoot({ TranslateModule.forRoot({
loader: {provide: TranslateLoader, useClass: TranslateFakeLoader}, loader: {provide: TranslateLoader, useClass: TranslateFakeLoader},
}), }),

View File

@@ -44,13 +44,21 @@ export abstract class IconReplacer implements OnInit, OnDestroy {
protected slotName = 'sc-icon'; protected slotName = 'sc-icon';
protected maxAttempts = 10;
protected retryAfterMs = 10;
/** /**
* The host element * The host element
* *
* This will be either element.nativeElement.shadowRoot or element.nativeElement * This will be either element.nativeElement.shadowRoot or element.nativeElement
* depending on the iconDomLocation * depending on the iconDomLocation
*/ */
protected host: HTMLElement; protected get host() {
return this.iconDomLocation === 'shadow'
? this.element.nativeElement.shadowRoot
: this.element.nativeElement;
}
/** /**
* @param element The host element * @param element The host element
@@ -85,13 +93,28 @@ export abstract class IconReplacer implements OnInit, OnDestroy {
} }
ngOnInit() { ngOnInit() {
this.host =
this.iconDomLocation === 'shadow'
? this.element.nativeElement.shadowRoot
: this.element.nativeElement;
this.init(); this.init();
if (!this.host) {
let tries = 0;
console.warn('IconReplacer: host not found, trying again');
const interval = setInterval(() => {
if (tries > this.maxAttempts) {
clearInterval(interval);
throw new Error('IconReplacer: host not found');
}
if (this.host) {
clearInterval(interval);
this.replace();
}
tries++;
}, this.retryAfterMs);
} else {
this.attachObserver();
}
}
private attachObserver() {
this.mutationObserver = new MutationObserver(() => this.replace()); this.mutationObserver = new MutationObserver(() => this.replace());
this.mutationObserver.observe(this.host, { this.mutationObserver.observe(this.host, {
childList: true, childList: true,
@@ -139,7 +162,7 @@ export abstract class IconReplacer implements OnInit, OnDestroy {
} }
ngOnDestroy() { ngOnDestroy() {
this.mutationObserver.disconnect(); this.mutationObserver?.disconnect();
this.destroy(); this.destroy();
} }
} }