MOBILE-3814 core: Remove timeout from waitToBeInDOM

main
Pau Ferrer Ocaña 2022-03-17 11:08:10 +01:00
parent 9f61d559b1
commit e5d748f5e5
2 changed files with 13 additions and 18 deletions

View File

@ -107,6 +107,7 @@ export class CoreEditorRichTextEditorComponent implements OnInit, AfterContentIn
protected languageChangedSubscription?: Subscription;
protected resizeListener?: CoreEventObserver;
protected domPromise?: CoreCancellablePromise<void>;
protected buttonsDomPromise?: CoreCancellablePromise<void>;
rteEnabled = false;
isPhone = false;
@ -836,7 +837,10 @@ export class CoreEditorRichTextEditorComponent implements OnInit, AfterContentIn
const length = await this.toolbarSlides.length();
await CoreDomUtils.waitToBeInDOM(this.toolbar.nativeElement, 5000);
// Cancel previous one, if any.
this.buttonsDomPromise?.cancel();
this.buttonsDomPromise = CoreDomUtils.waitToBeInDOM(this.toolbar.nativeElement);
await this.buttonsDomPromise;
const width = this.toolbar.nativeElement.getBoundingClientRect().width;
@ -1097,6 +1101,7 @@ export class CoreEditorRichTextEditorComponent implements OnInit, AfterContentIn
this.labelObserver?.disconnect();
this.resizeListener?.off();
this.domPromise?.cancel();
this.buttonsDomPromise?.cancel();
}
}

View File

@ -97,10 +97,9 @@ export class CoreDomUtilsProvider {
* Wait an element to be in dom of another element.
*
* @param element Element to wait.
* @param timeout If defined, timeout to wait before rejecting the promise.
* @return Cancellable promise.
*/
waitToBeInDOM(element: HTMLElement, timeout?: number): CoreCancellablePromise<void> {
waitToBeInDOM(element: HTMLElement): CoreCancellablePromise<void> {
const root = element.getRootNode({ composed: true });
if (root === document) {
@ -109,33 +108,24 @@ export class CoreDomUtilsProvider {
}
let observer: MutationObserver;
let observerTimeout: number | undefined;
return new CoreCancellablePromise<void>(
(resolve, reject) => {
(resolve) => {
observer = new MutationObserver(() => {
const root = element.getRootNode({ composed: true });
if (root === document) {
if (root !== document) {
return;
}
observer?.disconnect();
observerTimeout && clearTimeout(observerTimeout);
resolve();
}
});
if (timeout) {
observerTimeout = window.setTimeout(() => {
observer?.disconnect();
reject(new Error('Waiting for DOM timeout reached'));
}, timeout);
}
observer.observe(document.body, { subtree: true, childList: true });
},
() => {
observer?.disconnect();
observerTimeout && clearTimeout(observerTimeout);
},
);
}