diff --git a/src/core/directives/collapsible-item.ts b/src/core/directives/collapsible-item.ts index ba4489c4a..08cc6da18 100644 --- a/src/core/directives/collapsible-item.ts +++ b/src/core/directives/collapsible-item.ts @@ -89,7 +89,7 @@ export class CoreCollapsibleItemDirective implements OnInit { * @return Promise resolved when loadings are done. */ protected async waitLoadingsDone(): Promise { - await CoreDomUtils.waitToDom(this.element); + await CoreDomUtils.waitToBeInDOM(this.element); const page = this.element.closest('.ion-page'); diff --git a/src/core/services/utils/dom.ts b/src/core/services/utils/dom.ts index b3f31119c..a91e89caf 100644 --- a/src/core/services/utils/dom.ts +++ b/src/core/services/utils/dom.ts @@ -53,7 +53,6 @@ import { NavigationStart } from '@angular/router'; import { filter } from 'rxjs/operators'; import { Subscription } from 'rxjs'; import { CoreComponentsRegistry } from '@singletons/components-registry'; -import { CorePromisedValue } from '@classes/promised-value'; /* * "Utils" service with helper functions for UI, DOM elements and HTML code. @@ -98,37 +97,34 @@ export class CoreDomUtilsProvider { * @param element Element to wait. * @return Promise resolved when added. It will be rejected after a timeout of 5s. */ - waitToDom( + async waitToBeInDOM( element: Element, - ): CorePromisedValue { + ): Promise { let root = element.getRootNode({ composed: true }); - const inDomPromise = new CorePromisedValue(); if (root === document) { // Already in DOM. - inDomPromise.resolve(); - - return inDomPromise; + return; } - // Disconnect observer for performance reasons. - const timeout = window.setTimeout(() => { - inDomPromise.reject(new Error('Waiting for DOM timeout reached')); - observer.disconnect(); - }, 5000); - - const observer = new MutationObserver(() => { - root = element.getRootNode({ composed: true }); - if (root === document) { + return new Promise((resolve, reject) => { + // Disconnect observer for performance reasons. + const timeout = window.setTimeout(() => { + reject(new Error('Waiting for DOM timeout reached')); observer.disconnect(); - clearTimeout(timeout); - inDomPromise.resolve(); - } + }, 5000); + + const observer = new MutationObserver(() => { + root = element.getRootNode({ composed: true }); + if (root === document) { + observer.disconnect(); + clearTimeout(timeout); + resolve(); + } + }); + + observer.observe(document.body, { subtree: true, childList: true }); }); - - observer.observe(document.body, { subtree: true, childList: true }); - - return inDomPromise; } /** @@ -139,7 +135,7 @@ export class CoreDomUtilsProvider { * @param element DOM Element. * @param selector Selector to search. * @return Closest ancestor. - * @deprecated Not needed anymore since it's supported on both Android and iOS. Use closest instead. + * @deprecated since app 4.0 Not needed anymore since it's supported on both Android and iOS. Use closest instead. */ closest(element: Element | undefined | null, selector: string): Element | null { return element?.closest(selector) ?? null;