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.
*/
protected async waitLoadingsDone(): Promise<void> {
await CoreDomUtils.waitToDom(this.element);
await CoreDomUtils.waitToBeInDOM(this.element);
const page = this.element.closest('.ion-page');

View File

@ -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<void> {
): Promise<void> {
let root = element.getRootNode({ composed: true });
const inDomPromise = new CorePromisedValue<void>();
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;