mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-20 16:42:56 +00:00
fix: address late init from ionic components
This commit is contained in:
@@ -44,13 +44,21 @@ export abstract class IconReplacer implements OnInit, OnDestroy {
|
||||
|
||||
protected slotName = 'sc-icon';
|
||||
|
||||
protected maxAttempts = 10;
|
||||
|
||||
protected retryAfterMs = 10;
|
||||
|
||||
/**
|
||||
* The host element
|
||||
*
|
||||
* This will be either element.nativeElement.shadowRoot or element.nativeElement
|
||||
* 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
|
||||
@@ -85,13 +93,28 @@ export abstract class IconReplacer implements OnInit, OnDestroy {
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
this.host =
|
||||
this.iconDomLocation === 'shadow'
|
||||
? this.element.nativeElement.shadowRoot
|
||||
: this.element.nativeElement;
|
||||
|
||||
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.observe(this.host, {
|
||||
childList: true,
|
||||
@@ -139,7 +162,7 @@ export abstract class IconReplacer implements OnInit, OnDestroy {
|
||||
}
|
||||
|
||||
ngOnDestroy() {
|
||||
this.mutationObserver.disconnect();
|
||||
this.mutationObserver?.disconnect();
|
||||
this.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user