mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-05 13:02:54 +00:00
fix: address late init from ionic components
This commit is contained in:
@@ -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},
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user