Merge pull request #3173 from crazyserver/MOBILE-3814

MOBILE-3814 chore: Refactor waitToDom and rename it to waitToBeInDOM
main
Dani Palou 2022-03-14 12:33:17 +01:00 committed by GitHub
commit cd323719db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 25 deletions

View File

@ -89,7 +89,7 @@ export class CoreCollapsibleItemDirective implements OnInit {
* @return Promise resolved when loadings are done. * @return Promise resolved when loadings are done.
*/ */
protected async waitLoadingsDone(): Promise<void> { protected async waitLoadingsDone(): Promise<void> {
await CoreDomUtils.waitToDom(this.element); await CoreDomUtils.waitToBeInDOM(this.element);
const page = this.element.closest('.ion-page'); const page = this.element.closest('.ion-page');

View File

@ -53,7 +53,6 @@ import { NavigationStart } from '@angular/router';
import { filter } from 'rxjs/operators'; import { filter } from 'rxjs/operators';
import { Subscription } from 'rxjs'; import { Subscription } from 'rxjs';
import { CoreComponentsRegistry } from '@singletons/components-registry'; import { CoreComponentsRegistry } from '@singletons/components-registry';
import { CorePromisedValue } from '@classes/promised-value';
/* /*
* "Utils" service with helper functions for UI, DOM elements and HTML code. * "Utils" service with helper functions for UI, DOM elements and HTML code.
@ -98,22 +97,20 @@ export class CoreDomUtilsProvider {
* @param element Element to wait. * @param element Element to wait.
* @return Promise resolved when added. It will be rejected after a timeout of 5s. * @return Promise resolved when added. It will be rejected after a timeout of 5s.
*/ */
waitToDom( async waitToBeInDOM(
element: Element, element: Element,
): CorePromisedValue<void> { ): Promise<void> {
let root = element.getRootNode({ composed: true }); let root = element.getRootNode({ composed: true });
const inDomPromise = new CorePromisedValue<void>();
if (root === document) { if (root === document) {
// Already in DOM. // Already in DOM.
inDomPromise.resolve(); return;
return inDomPromise;
} }
return new Promise((resolve, reject) => {
// Disconnect observer for performance reasons. // Disconnect observer for performance reasons.
const timeout = window.setTimeout(() => { const timeout = window.setTimeout(() => {
inDomPromise.reject(new Error('Waiting for DOM timeout reached')); reject(new Error('Waiting for DOM timeout reached'));
observer.disconnect(); observer.disconnect();
}, 5000); }, 5000);
@ -122,13 +119,12 @@ export class CoreDomUtilsProvider {
if (root === document) { if (root === document) {
observer.disconnect(); observer.disconnect();
clearTimeout(timeout); clearTimeout(timeout);
inDomPromise.resolve(); 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 element DOM Element.
* @param selector Selector to search. * @param selector Selector to search.
* @return Closest ancestor. * @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 { closest(element: Element | undefined | null, selector: string): Element | null {
return element?.closest(selector) ?? null; return element?.closest(selector) ?? null;