MOBILE-3817 core: Convert has changes func to return Promise

main
Dani Palou 2022-10-04 10:55:29 +02:00
parent a157b5503c
commit 70191ad5bf
2 changed files with 15 additions and 9 deletions

View File

@ -711,10 +711,10 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem
* @param newCourses New courses. * @param newCourses New courses.
* @return Whether it has meaningful changes. * @return Whether it has meaningful changes.
*/ */
protected coursesHaveMeaningfulChanges( protected async coursesHaveMeaningfulChanges(
previousCourses: CoreEnrolledCourseDataWithExtraInfoAndOptions[], previousCourses: CoreEnrolledCourseDataWithExtraInfoAndOptions[],
newCourses: CoreEnrolledCourseDataWithExtraInfoAndOptions[], newCourses: CoreEnrolledCourseDataWithExtraInfoAndOptions[],
): boolean { ): Promise<boolean> {
if (previousCourses.length !== newCourses.length) { if (previousCourses.length !== newCourses.length) {
return true; return true;
} }
@ -746,10 +746,10 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem
* @param newCourses New courses. * @param newCourses New courses.
* @return Whether it has meaningful changes. * @return Whether it has meaningful changes.
*/ */
protected customFilterCoursesHaveMeaningfulChanges( protected async customFilterCoursesHaveMeaningfulChanges(
previousCourses: CoreCourseSummaryData[], previousCourses: CoreCourseSummaryData[],
newCourses: CoreCourseSummaryData[], newCourses: CoreCourseSummaryData[],
): boolean { ): Promise<boolean> {
if (previousCourses.length !== newCourses.length) { if (previousCourses.length !== newCourses.length) {
return true; return true;
} }

View File

@ -29,6 +29,7 @@ export class PageLoadWatcher {
protected ongoingRequests = 0; protected ongoingRequests = 0;
protected components = new Set<AsyncComponent>(); protected components = new Set<AsyncComponent>();
protected loadedTimeout?: number; protected loadedTimeout?: number;
protected hasChangesPromises: Promise<boolean>[] = [];
constructor( constructor(
protected loadsManager: PageLoadsManager, protected loadsManager: PageLoadsManager,
@ -97,7 +98,7 @@ export class PageLoadWatcher {
*/ */
watchRequest<T>( watchRequest<T>(
observable: WSObservable<T>, observable: WSObservable<T>,
hasMeaningfulChanges?: (previousValue: T, newValue: T) => boolean, hasMeaningfulChanges?: (previousValue: T, newValue: T) => Promise<boolean>,
): Promise<T> { ): Promise<T> {
const promisedValue = new CorePromisedValue<T>(); const promisedValue = new CorePromisedValue<T>();
let subscription: Subscription | null = null; let subscription: Subscription | null = null;
@ -124,9 +125,11 @@ export class PageLoadWatcher {
} }
// Second value, it means data was updated in background. Compare data. // Second value, it means data was updated in background. Compare data.
if (hasMeaningfulChanges?.(firstValue, value)) { if (!hasMeaningfulChanges) {
this.hasChanges = true; return;
} }
this.hasChangesPromises.push(CoreUtils.ignoreErrors(hasMeaningfulChanges(firstValue, value), false));
}, },
error: (error) => { error: (error) => {
promisedValue.reject(error); promisedValue.reject(error);
@ -150,8 +153,11 @@ export class PageLoadWatcher {
// It seems load has finished. Wait to make sure no new component has been rendered and started loading. // It seems load has finished. Wait to make sure no new component has been rendered and started loading.
// If a new component or a new request starts the timeout will be cancelled, no need to double check it. // If a new component or a new request starts the timeout will be cancelled, no need to double check it.
clearTimeout(this.loadedTimeout); clearTimeout(this.loadedTimeout);
this.loadedTimeout = window.setTimeout(() => { this.loadedTimeout = window.setTimeout(async () => {
// Loading finished. // Loading finished. Calculate has changes.
const values = await Promise.all(this.hasChangesPromises);
this.hasChanges = this.hasChanges || values.includes(true);
this.loadsManager.onPageLoaded(this); this.loadsManager.onPageLoaded(this);
}, 100); }, 100);
} }