From 56596ad30e8d6e4b68bc301d5b1a75227556e83c Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Thu, 11 Nov 2021 10:02:53 +0100 Subject: [PATCH] MOBILE-3909 calendar: Fix non-null assertion eslint warnings --- .../calendar/components/calendar/calendar.ts | 26 +++++++-------- .../upcoming-events/upcoming-events.ts | 6 ++-- src/addons/calendar/pages/day/day.page.ts | 4 +-- .../pages/edit-event/edit-event.page.ts | 19 +++++++---- src/addons/calendar/pages/event/event.page.ts | 9 ++--- .../calendar/services/calendar-helper.ts | 24 +++++++------- .../calendar/services/calendar-offline.ts | 2 +- src/addons/calendar/services/calendar-sync.ts | 9 ++--- src/addons/calendar/services/calendar.ts | 33 +++++++++++-------- .../services/database/calendar-offline.ts | 2 +- .../calendar/services/database/calendar.ts | 2 +- src/addons/messages/services/messages-sync.ts | 5 +-- src/addons/mod/assign/services/assign-sync.ts | 5 +-- src/addons/mod/choice/services/choice-sync.ts | 5 +-- src/addons/mod/data/services/data-sync.ts | 5 +-- .../mod/feedback/services/feedback-sync.ts | 5 +-- src/addons/mod/forum/services/forum-sync.ts | 10 +++--- .../mod/glossary/services/glossary-sync.ts | 5 +-- .../h5pactivity/services/h5pactivity-sync.ts | 5 +-- src/addons/mod/quiz/services/quiz-sync.ts | 5 +-- src/addons/mod/scorm/services/scorm-sync.ts | 5 +-- src/addons/mod/survey/services/survey-sync.ts | 5 +-- src/addons/mod/wiki/services/wiki-sync.ts | 5 +-- .../mod/workshop/services/workshop-sync.ts | 5 +-- src/addons/notes/services/notes-sync.ts | 5 +-- src/core/classes/base-sync.ts | 22 ++++--------- .../comments/services/comments-sync.ts | 5 +-- src/core/features/course/services/sync.ts | 5 +-- .../features/rating/services/rating-sync.ts | 5 +-- src/core/features/user/services/user-sync.ts | 5 +-- 30 files changed, 139 insertions(+), 114 deletions(-) diff --git a/src/addons/calendar/components/calendar/calendar.ts b/src/addons/calendar/components/calendar/calendar.ts index 5575e917c..a4b2178bc 100644 --- a/src/addons/calendar/components/calendar/calendar.ts +++ b/src/addons/calendar/components/calendar/calendar.ts @@ -94,7 +94,7 @@ export class AddonCalendarCalendarComponent implements OnInit, DoCheck, OnDestro this.obsDefaultTimeChange = CoreEvents.on(AddonCalendarProvider.DEFAULT_NOTIFICATION_TIME_CHANGED, () => { this.weeks.forEach((week) => { week.days.forEach((day) => { - AddonCalendar.scheduleEventsNotifications(day.eventsFormated!); + AddonCalendar.scheduleEventsNotifications(day.eventsFormated || []); }); }); }, this.currentSiteId); @@ -150,7 +150,7 @@ export class AddonCalendarCalendarComponent implements OnInit, DoCheck, OnDestro if (this.weeks) { // Check if there's any change in the filter object. - const changes = this.differ.diff(this.filter!); + const changes = this.differ.diff(this.filter || {}); if (changes) { this.filterEvents(); } @@ -173,8 +173,8 @@ export class AddonCalendarCalendarComponent implements OnInit, DoCheck, OnDestro this.offlineEvents = AddonCalendarHelper.classifyIntoMonths(events); // Get the IDs of events edited in offline. - const filtered = events.filter((event) => event.id! > 0); - this.offlineEditedEventsIds = filtered.map((event) => event.id!); + const filtered = events.filter((event) => event.id > 0); + this.offlineEditedEventsIds = filtered.map((event) => event.id); return; })); @@ -261,7 +261,7 @@ export class AddonCalendarCalendarComponent implements OnInit, DoCheck, OnDestro isPast = day.ispast; if (day.istoday) { - day.eventsFormated!.forEach((event) => { + day.eventsFormated?.forEach((event) => { event.ispast = this.isEventPast(event); }); } @@ -306,8 +306,8 @@ export class AddonCalendarCalendarComponent implements OnInit, DoCheck, OnDestro this.weeks.forEach((week) => { week.days.forEach((day) => { day.filteredEvents = AddonCalendarHelper.getFilteredEvents( - day.eventsFormated!, - this.filter!, + day.eventsFormated || [], + this.filter, this.categories, ); @@ -466,14 +466,14 @@ export class AddonCalendarCalendarComponent implements OnInit, DoCheck, OnDestro week.days.forEach((day) => { // Schedule notifications for the events retrieved (only future events will be scheduled). - AddonCalendar.scheduleEventsNotifications(day.eventsFormated!); + AddonCalendar.scheduleEventsNotifications(day.eventsFormated || []); if (monthOfflineEvents || this.deletedEvents.length) { // There is offline data, merge it. if (this.deletedEvents.length) { // Mark as deleted the events that were deleted in offline. - day.eventsFormated!.forEach((event) => { + day.eventsFormated?.forEach((event) => { event.deleted = this.deletedEvents.indexOf(event.id) != -1; }); } @@ -483,10 +483,10 @@ export class AddonCalendarCalendarComponent implements OnInit, DoCheck, OnDestro day.events = day.events.filter((event) => this.offlineEditedEventsIds.indexOf(event.id) == -1); } - if (monthOfflineEvents && monthOfflineEvents[day.mday]) { + if (monthOfflineEvents && monthOfflineEvents[day.mday] && day.eventsFormated) { // Add the offline events (either new or edited). day.eventsFormated = - AddonCalendarHelper.sortEvents(day.eventsFormated!.concat(monthOfflineEvents[day.mday])); + AddonCalendarHelper.sortEvents(day.eventsFormated.concat(monthOfflineEvents[day.mday])); } } }); @@ -505,7 +505,7 @@ export class AddonCalendarCalendarComponent implements OnInit, DoCheck, OnDestro this.weeks.forEach((week) => { week.days.forEach((day) => { - const event = day.eventsFormated!.find((event) => event.id == eventId); + const event = day.eventsFormated?.find((event) => event.id == eventId); if (event) { event.deleted = false; @@ -521,7 +521,7 @@ export class AddonCalendarCalendarComponent implements OnInit, DoCheck, OnDestro * @return True if it's in the past. */ protected isEventPast(event: { timestart: number; timeduration: number}): boolean { - return (event.timestart + event.timeduration) < this.currentTime!; + return (event.timestart + event.timeduration) < (this.currentTime || CoreTimeUtils.timestamp()); } /** diff --git a/src/addons/calendar/components/upcoming-events/upcoming-events.ts b/src/addons/calendar/components/upcoming-events/upcoming-events.ts index 7566c6d14..f59363845 100644 --- a/src/addons/calendar/components/upcoming-events/upcoming-events.ts +++ b/src/addons/calendar/components/upcoming-events/upcoming-events.ts @@ -106,7 +106,7 @@ export class AddonCalendarUpcomingEventsComponent implements OnInit, DoCheck, On */ ngDoCheck(): void { // Check if there's any change in the filter object. - const changes = this.differ.diff(this.filter!); + const changes = this.differ.diff(this.filter || {}); if (changes) { this.filterEvents(); } @@ -183,7 +183,7 @@ export class AddonCalendarUpcomingEventsComponent implements OnInit, DoCheck, On // Re-calculate the formatted time so it uses the device date. const promises = this.events.map((event) => - AddonCalendar.formatEventTime(event, this.timeFormat!).then((time) => { + AddonCalendar.formatEventTime(event, this.timeFormat).then((time) => { event.formattedtime = time; return; @@ -221,7 +221,7 @@ export class AddonCalendarUpcomingEventsComponent implements OnInit, DoCheck, On * Filter events based on the filter popover. */ protected filterEvents(): void { - this.filteredEvents = AddonCalendarHelper.getFilteredEvents(this.events, this.filter!, this.categories); + this.filteredEvents = AddonCalendarHelper.getFilteredEvents(this.events, this.filter, this.categories); } /** diff --git a/src/addons/calendar/pages/day/day.page.ts b/src/addons/calendar/pages/day/day.page.ts index f02bc54d1..8e6d707eb 100644 --- a/src/addons/calendar/pages/day/day.page.ts +++ b/src/addons/calendar/pages/day/day.page.ts @@ -286,7 +286,7 @@ export class AddonCalendarDayPage implements OnInit, OnDestroy { this.offlineEvents = AddonCalendarHelper.classifyIntoMonths(offlineEvents); // Get the IDs of events edited in offline. - this.offlineEditedEventsIds = offlineEvents.filter((event) => event.id! > 0).map((event) => event.id!); + this.offlineEditedEventsIds = offlineEvents.filter((event) => event.id > 0).map((event) => event.id); return; })); @@ -361,7 +361,7 @@ export class AddonCalendarDayPage implements OnInit, OnDestroy { const promises = this.events.map((event) => { event.ispast = this.isPastDay || (this.isCurrentDay && this.isEventPast(event)); - return AddonCalendar.formatEventTime(event, this.timeFormat!, true, dayTime).then((time) => { + return AddonCalendar.formatEventTime(event, this.timeFormat, true, dayTime).then((time) => { event.formattedtime = time; return; diff --git a/src/addons/calendar/pages/edit-event/edit-event.page.ts b/src/addons/calendar/pages/edit-event/edit-event.page.ts index 366e07fa9..e16c88b09 100644 --- a/src/addons/calendar/pages/edit-event/edit-event.page.ts +++ b/src/addons/calendar/pages/edit-event/edit-event.page.ts @@ -171,17 +171,19 @@ export class AddonCalendarEditEventPage implements OnInit, OnDestroy, CanLeave { if (this.eventId && !this.gotEventData) { // Editing an event, get the event data. Wait for sync first. + const eventId = this.eventId; + promises.push(AddonCalendarSync.waitForSync(AddonCalendarSyncProvider.SYNC_ID).then(async () => { // Do not block if the scope is already destroyed. if (!this.isDestroyed && this.eventId) { - CoreSync.blockOperation(AddonCalendarProvider.COMPONENT, this.eventId); + CoreSync.blockOperation(AddonCalendarProvider.COMPONENT, eventId); } let eventForm: AddonCalendarEvent | AddonCalendarOfflineEventDBRecord | undefined; // Get the event offline data if there's any. try { - eventForm = await AddonCalendarOffline.getEvent(this.eventId!); + eventForm = await AddonCalendarOffline.getEvent(eventId); this.hasOffline = true; } catch { @@ -189,9 +191,9 @@ export class AddonCalendarEditEventPage implements OnInit, OnDestroy, CanLeave { this.hasOffline = false; } - if (this.eventId! > 0) { + if (eventId > 0) { // It's an online event. get its data from server. - const event = await AddonCalendar.getEventById(this.eventId!); + const event = await AddonCalendar.getEventById(eventId); if (!eventForm) { eventForm = event; // Use offline data first. @@ -562,7 +564,7 @@ export class AddonCalendarEditEventPage implements OnInit, OnDestroy, CanLeave { if (event) { CoreEvents.trigger( AddonCalendarProvider.NEW_EVENT_EVENT, - { eventId: event.id! }, + { eventId: event.id }, this.currentSite.getId(), ); } else { @@ -578,10 +580,15 @@ export class AddonCalendarEditEventPage implements OnInit, OnDestroy, CanLeave { * Discard an offline saved discussion. */ async discard(): Promise { + if (!this.eventId) { + return; + } + try { await CoreDomUtils.showConfirm(Translate.instant('core.areyousure')); + try { - await AddonCalendarOffline.deleteEvent(this.eventId!); + await AddonCalendarOffline.deleteEvent(this.eventId); CoreForms.triggerFormCancelledEvent(this.formElement, this.currentSite.getId()); diff --git a/src/addons/calendar/pages/event/event.page.ts b/src/addons/calendar/pages/event/event.page.ts index bb21b880d..1d320f9ff 100644 --- a/src/addons/calendar/pages/event/event.page.ts +++ b/src/addons/calendar/pages/event/event.page.ts @@ -214,7 +214,8 @@ export class AddonCalendarEventPage implements OnInit, OnDestroy { try { // Get the event data. const event = await AddonCalendar.getEventById(this.eventId); - this.event = await AddonCalendarHelper.formatEventData(event); + const formattedEvent = await AddonCalendarHelper.formatEventData(event); + this.event = formattedEvent; // Load reminders, and re-schedule them if needed (maybe the event time has changed). this.loadReminders(); @@ -266,7 +267,7 @@ export class AddonCalendarEventPage implements OnInit, OnDestroy { // If it's a group event, get the name of the group. if (courseId && this.event.groupid) { promises.push(CoreGroups.getUserGroupsInCourse(courseId).then((groups) => { - const group = groups.find((group) => group.id == this.event!.groupid); + const group = groups.find((group) => group.id == formattedEvent.groupid); this.groupName = group ? group.name : ''; @@ -289,14 +290,14 @@ export class AddonCalendarEventPage implements OnInit, OnDestroy { // Check if event was deleted in offine. promises.push(AddonCalendarOffline.isEventDeleted(this.eventId).then((deleted) => { - this.event!.deleted = deleted; + formattedEvent.deleted = deleted; return; })); // Re-calculate the formatted time so it uses the device date. promises.push(AddonCalendar.getCalendarTimeFormat().then(async (timeFormat) => { - this.event!.formattedtime = await AddonCalendar.formatEventTime(this.event!, timeFormat); + formattedEvent.formattedtime = await AddonCalendar.formatEventTime(formattedEvent, timeFormat); return; })); diff --git a/src/addons/calendar/services/calendar-helper.ts b/src/addons/calendar/services/calendar-helper.ts index 1cf856436..b1a2081ec 100644 --- a/src/addons/calendar/services/calendar-helper.ts +++ b/src/addons/calendar/services/calendar-helper.ts @@ -222,7 +222,7 @@ export class AddonCalendarHelperProvider { formatOfflineEventData(event: AddonCalendarOfflineEventDBRecord): AddonCalendarEventToDisplay { const eventFormatted: AddonCalendarEventToDisplay = { - id: event.id!, + id: event.id, name: event.name, timestart: event.timestart, eventtype: event.eventtype, @@ -386,7 +386,7 @@ export class AddonCalendarHelperProvider { year: number, month: number, siteId?: string, - ): Promise<{ daynames: Partial[]; weeks: Partial[] }> { + ): Promise<{ daynames: Partial[]; weeks: AddonCalendarWeek[] }> { const site = await CoreSites.getSite(siteId); // Get starting week day user preference, fallback to site configuration. let startWeekDayStr = site.getStoredConfig('calendar_startwday'); @@ -395,7 +395,7 @@ export class AddonCalendarHelperProvider { const today = moment(); const isCurrentMonth = today.year() == year && today.month() == month - 1; - const weeks: Partial[] = []; + const weeks: AddonCalendarWeek[] = []; let date = moment({ year, month: month - 1, date: 1 }); for (let mday = 1; mday <= date.daysInMonth(); mday++) { @@ -422,7 +422,7 @@ export class AddonCalendarHelperProvider { } // Add day to current week. - weeks[weeks.length - 1].days!.push({ + weeks[weeks.length - 1].days.push({ events: [], hasevents: false, mday: date.date(), @@ -501,11 +501,11 @@ export class AddonCalendarHelperProvider { */ getFilteredEvents( events: AddonCalendarEventToDisplay[], - filter: AddonCalendarFilter, + filter: AddonCalendarFilter | undefined, categories: { [id: number]: CoreCategoryData }, ): AddonCalendarEventToDisplay[] { // Do not filter. - if (!filter.filtered) { + if (!filter || !filter.filtered) { return events; } @@ -526,9 +526,9 @@ export class AddonCalendarHelperProvider { * Check if an event should be displayed based on the filter. * * @param event Event object. + * @param categories Categories indexed by ID. * @param courseId Course ID to filter. * @param categoryId Category ID the course belongs to. - * @param categories Categories indexed by ID. * @return Whether it should be displayed. */ protected shouldDisplayEvent( @@ -543,7 +543,7 @@ export class AddonCalendarHelperProvider { } if (event.eventtype == 'category' && categories) { - if (!event.categoryid || !Object.keys(categories).length) { + if (!event.categoryid || !Object.keys(categories).length || !categoryId) { // We can't tell if the course belongs to the category, display them all. return true; } @@ -554,7 +554,7 @@ export class AddonCalendarHelperProvider { } // Check parent categories. - let category = categories[categoryId!]; + let category = categories[categoryId]; while (category) { if (!category.parent) { // Category doesn't have parent, stop. @@ -618,7 +618,7 @@ export class AddonCalendarHelperProvider { await AddonCalendar.getLocalEventsByRepeatIdFromLocalDb(eventData.repeatid, site.id); await CoreUtils.allPromises(repeatedEvents.map((event) => - AddonCalendar.invalidateEvent(event.id!))); + AddonCalendar.invalidateEvent(event.id))); return; } @@ -721,7 +721,7 @@ export class AddonCalendarHelperProvider { */ refreshAfterChangeEvent( event: { - id?: number; + id: number; repeatid?: number; timestart: number; }, @@ -730,7 +730,7 @@ export class AddonCalendarHelperProvider { ): Promise { return this.refreshAfterChangeEvents( [{ - id: event.id!, + id: event.id, repeatid: event.repeatid, timestart: event.timestart, repeated: repeated, diff --git a/src/addons/calendar/services/calendar-offline.ts b/src/addons/calendar/services/calendar-offline.ts index d34586f91..e34ea7eb7 100644 --- a/src/addons/calendar/services/calendar-offline.ts +++ b/src/addons/calendar/services/calendar-offline.ts @@ -110,7 +110,7 @@ export class AddonCalendarOfflineProvider { async getAllEditedEventsIds(siteId?: string): Promise { const events = await this.getAllEditedEvents(siteId); - return events.map((event) => event.id!); + return events.map((event) => event.id); } /** diff --git a/src/addons/calendar/services/calendar-sync.ts b/src/addons/calendar/services/calendar-sync.ts index 5d6fcad20..b6d4d0cf6 100644 --- a/src/addons/calendar/services/calendar-sync.ts +++ b/src/addons/calendar/services/calendar-sync.ts @@ -100,9 +100,10 @@ export class AddonCalendarSyncProvider extends CoreSyncBaseProvider { siteId = siteId || CoreSites.getCurrentSiteId(); - if (this.isSyncing(AddonCalendarSyncProvider.SYNC_ID, siteId)) { + const currentSyncPromise = this.getOngoingSync(AddonCalendarSyncProvider.SYNC_ID, siteId); + if (currentSyncPromise) { // There's already a sync ongoing for this site, return the promise. - return this.getOngoingSync(AddonCalendarSyncProvider.SYNC_ID, siteId)!; + return currentSyncPromise; } this.logger.debug('Try to sync calendar events for site ' + siteId); @@ -272,7 +273,7 @@ export class AddonCalendarSyncProvider extends CoreSyncBaseProvider [], + groupids: [], + }; const params: AddonCalendarGetCalendarEventsWSParams = { options: { userevents: true, @@ -925,23 +929,20 @@ export class AddonCalendarProvider { timestart: start, timeend: end, }, - events: { - courseids: [], - groupids: [], - }, + events: events, }; const promises: Promise[] = []; promises.push(CoreCourses.getUserCourses(false, siteId).then((courses) => { - params.events!.courseids = courses.map((course) => course.id); - params.events!.courseids.push(site.getSiteHomeId()); // Add front page. + events.courseids = courses.map((course) => course.id); + events.courseids.push(site.getSiteHomeId()); // Add front page. return; })); promises.push(CoreGroups.getAllUserGroups(siteId).then((groups) => { - params.events!.groupids = groups.map((group) => group.id); + events.groupids = groups.map((group) => group.id); return; })); @@ -1697,6 +1698,7 @@ export class AddonCalendarProvider { siteId?: string, ): Promise { const site = await CoreSites.getSite(siteId); + // Add data that is "hidden" in web. formData.id = eventId; formData.userid = site.getUserId(); @@ -1707,12 +1709,14 @@ export class AddonCalendarProvider { } else { formData['_qf__core_calendar_local_event_forms_create'] = 1; } + const params: AddonCalendarSubmitCreateUpdateFormWSParams = { formdata: CoreUtils.objectToGetParams(formData), }; const result = await site.write('core_calendar_submit_create_update_form', params); - if (result.validationerror) { + + if (result.validationerror || !result.event) { // Simulate a WS error. throw new CoreWSError({ message: Translate.instant('core.invalidformdata'), @@ -1720,7 +1724,7 @@ export class AddonCalendarProvider { }); } - return result.event!; + return result.event; } } @@ -2180,7 +2184,8 @@ type AddonCalendarSubmitCreateUpdateFormWSParams = { /** * Form data on AddonCalendarSubmitCreateUpdateFormWSParams. */ -export type AddonCalendarSubmitCreateUpdateFormDataWSParams = Omit & { +export type AddonCalendarSubmitCreateUpdateFormDataWSParams = Omit & { + id?: number; description?: { text: string; format: number; diff --git a/src/addons/calendar/services/database/calendar-offline.ts b/src/addons/calendar/services/database/calendar-offline.ts index 046dc59f5..5e08f8378 100644 --- a/src/addons/calendar/services/database/calendar-offline.ts +++ b/src/addons/calendar/services/database/calendar-offline.ts @@ -135,7 +135,7 @@ export const CALENDAR_OFFLINE_SITE_SCHEMA: CoreSiteSchema = { }; export type AddonCalendarOfflineEventDBRecord = { - id?: number; // Negative for offline entries. + id: number; // Negative for offline entries. name: string; timestart: number; eventtype: AddonCalendarEventType; diff --git a/src/addons/calendar/services/database/calendar.ts b/src/addons/calendar/services/database/calendar.ts index 643a5ae98..9a890b79f 100644 --- a/src/addons/calendar/services/database/calendar.ts +++ b/src/addons/calendar/services/database/calendar.ts @@ -258,7 +258,7 @@ export const CALENDAR_SITE_SCHEMA: CoreSiteSchema = { }; export type AddonCalendarEventDBRecord = { - id?: number; + id: number; name: string; description: string; eventtype: AddonCalendarEventType; diff --git a/src/addons/messages/services/messages-sync.ts b/src/addons/messages/services/messages-sync.ts index 9a29b9f7d..cdcc4dd6c 100644 --- a/src/addons/messages/services/messages-sync.ts +++ b/src/addons/messages/services/messages-sync.ts @@ -147,9 +147,10 @@ export class AddonMessagesSyncProvider extends CoreSyncBaseProvider { siteId = siteId || CoreSites.getCurrentSiteId(); - if (this.isSyncing(assignId, siteId)) { + const currentSyncPromise = this.getOngoingSync(assignId, siteId); + if (currentSyncPromise) { // There's already a sync ongoing for this assign, return the promise. - return this.getOngoingSync(assignId, siteId)!; + return currentSyncPromise; } // Verify that assign isn't blocked. diff --git a/src/addons/mod/choice/services/choice-sync.ts b/src/addons/mod/choice/services/choice-sync.ts index a27a01f05..ed2537467 100644 --- a/src/addons/mod/choice/services/choice-sync.ts +++ b/src/addons/mod/choice/services/choice-sync.ts @@ -122,9 +122,10 @@ export class AddonModChoiceSyncProvider extends CoreCourseActivitySyncBaseProvid siteId = site.getId(); const syncId = this.getSyncId(choiceId, userId); - if (this.isSyncing(syncId, siteId)) { + const currentSyncPromise = this.getOngoingSync(syncId, siteId); + if (currentSyncPromise) { // There's already a sync ongoing for this discussion, return the promise. - return this.getOngoingSync(syncId, siteId)!; + return currentSyncPromise; } this.logger.debug(`Try to sync choice '${choiceId}' for user '${userId}'`); diff --git a/src/addons/mod/data/services/data-sync.ts b/src/addons/mod/data/services/data-sync.ts index a010247c4..9a947ffcb 100644 --- a/src/addons/mod/data/services/data-sync.ts +++ b/src/addons/mod/data/services/data-sync.ts @@ -135,9 +135,10 @@ export class AddonModDataSyncProvider extends CoreCourseActivitySyncBaseProvider syncDatabase(dataId: number, siteId?: string): Promise { siteId = siteId || CoreSites.getCurrentSiteId(); - if (this.isSyncing(dataId, siteId)) { + const currentSyncPromise = this.getOngoingSync(dataId, siteId); + if (currentSyncPromise) { // There's already a sync ongoing for this database, return the promise. - return this.getOngoingSync(dataId, siteId)!; + return currentSyncPromise; } // Verify that database isn't blocked. diff --git a/src/addons/mod/feedback/services/feedback-sync.ts b/src/addons/mod/feedback/services/feedback-sync.ts index d404ed6f2..8b6702b8a 100644 --- a/src/addons/mod/feedback/services/feedback-sync.ts +++ b/src/addons/mod/feedback/services/feedback-sync.ts @@ -130,9 +130,10 @@ export class AddonModFeedbackSyncProvider extends CoreCourseActivitySyncBaseProv syncFeedback(feedbackId: number, siteId?: string): Promise { siteId = siteId || CoreSites.getCurrentSiteId(); - if (this.isSyncing(feedbackId, siteId)) { + const currentSyncPromise = this.getOngoingSync(feedbackId, siteId); + if (currentSyncPromise) { // There's already a sync ongoing for this feedback, return the promise. - return this.getOngoingSync(feedbackId, siteId)!; + return currentSyncPromise; } // Verify that feedback isn't blocked. diff --git a/src/addons/mod/forum/services/forum-sync.ts b/src/addons/mod/forum/services/forum-sync.ts index fae896b0c..65925b3d7 100644 --- a/src/addons/mod/forum/services/forum-sync.ts +++ b/src/addons/mod/forum/services/forum-sync.ts @@ -198,10 +198,11 @@ export class AddonModForumSyncProvider extends CoreCourseActivitySyncBaseProvide siteId = siteId || CoreSites.getCurrentSiteId(); const syncId = this.getForumSyncId(forumId, userId); + const currentSyncPromise = this.getOngoingSync(syncId, siteId); - if (this.isSyncing(syncId, siteId)) { + if (currentSyncPromise) { // There's already a sync ongoing for this discussion, return the promise. - return this.getOngoingSync(syncId, siteId)!; + return currentSyncPromise; } // Verify that forum isn't blocked. @@ -429,10 +430,11 @@ export class AddonModForumSyncProvider extends CoreCourseActivitySyncBaseProvide siteId = siteId || CoreSites.getCurrentSiteId(); const syncId = this.getDiscussionSyncId(discussionId, userId); + const currentSyncPromise = this.getOngoingSync(syncId, siteId); - if (this.isSyncing(syncId, siteId)) { + if (currentSyncPromise) { // There's already a sync ongoing for this discussion, return the promise. - return this.getOngoingSync(syncId, siteId)!; + return currentSyncPromise; } // Verify that forum isn't blocked. diff --git a/src/addons/mod/glossary/services/glossary-sync.ts b/src/addons/mod/glossary/services/glossary-sync.ts index 80a2fbe0d..8d0844cb4 100644 --- a/src/addons/mod/glossary/services/glossary-sync.ts +++ b/src/addons/mod/glossary/services/glossary-sync.ts @@ -144,9 +144,10 @@ export class AddonModGlossarySyncProvider extends CoreCourseActivitySyncBaseProv siteId = siteId || CoreSites.getCurrentSiteId(); const syncId = this.getGlossarySyncId(glossaryId, userId); - if (this.isSyncing(syncId, siteId)) { + const currentSyncPromise = this.getOngoingSync(syncId, siteId); + if (currentSyncPromise) { // There's already a sync ongoing for this glossary, return the promise. - return this.getOngoingSync(syncId, siteId)!; + return currentSyncPromise; } // Verify that glossary isn't blocked. diff --git a/src/addons/mod/h5pactivity/services/h5pactivity-sync.ts b/src/addons/mod/h5pactivity/services/h5pactivity-sync.ts index 10aeb200d..95b5748ca 100644 --- a/src/addons/mod/h5pactivity/services/h5pactivity-sync.ts +++ b/src/addons/mod/h5pactivity/services/h5pactivity-sync.ts @@ -108,9 +108,10 @@ export class AddonModH5PActivitySyncProvider extends CoreCourseActivitySyncBaseP throw new CoreNetworkError(); } - if (this.isSyncing(contextId, siteId)) { + const currentSyncPromise = this.getOngoingSync(contextId, siteId); + if (currentSyncPromise) { // There's already a sync ongoing for this discussion, return the promise. - return this.getOngoingSync(contextId, siteId)!; + return currentSyncPromise; } return this.addOngoingSync(contextId, this.syncActivityData(contextId, siteId), siteId); diff --git a/src/addons/mod/quiz/services/quiz-sync.ts b/src/addons/mod/quiz/services/quiz-sync.ts index 25c101242..6990384f1 100644 --- a/src/addons/mod/quiz/services/quiz-sync.ts +++ b/src/addons/mod/quiz/services/quiz-sync.ts @@ -263,9 +263,10 @@ export class AddonModQuizSyncProvider extends CoreCourseActivitySyncBaseProvider syncQuiz(quiz: AddonModQuizQuizWSData, askPreflight?: boolean, siteId?: string): Promise { siteId = siteId || CoreSites.getCurrentSiteId(); - if (this.isSyncing(quiz.id, siteId)) { + const currentSyncPromise = this.getOngoingSync(quiz.id, siteId); + if (currentSyncPromise) { // There's already a sync ongoing for this quiz, return the promise. - return this.getOngoingSync(quiz.id, siteId)!; + return currentSyncPromise; } // Verify that quiz isn't blocked. diff --git a/src/addons/mod/scorm/services/scorm-sync.ts b/src/addons/mod/scorm/services/scorm-sync.ts index 3ebcfec81..5c9e1f158 100644 --- a/src/addons/mod/scorm/services/scorm-sync.ts +++ b/src/addons/mod/scorm/services/scorm-sync.ts @@ -579,9 +579,10 @@ export class AddonModScormSyncProvider extends CoreCourseActivitySyncBaseProvide syncScorm(scorm: AddonModScormScorm, siteId?: string): Promise { siteId = siteId || CoreSites.getCurrentSiteId(); - if (this.isSyncing(scorm.id, siteId)) { + const currentSyncPromise = this.getOngoingSync(scorm.id, siteId); + if (currentSyncPromise) { // There's already a sync ongoing for this SCORM, return the promise. - return this.getOngoingSync(scorm.id, siteId)!; + return currentSyncPromise; } // Verify that SCORM isn't blocked. diff --git a/src/addons/mod/survey/services/survey-sync.ts b/src/addons/mod/survey/services/survey-sync.ts index f9c70bf91..e128b0b1e 100644 --- a/src/addons/mod/survey/services/survey-sync.ts +++ b/src/addons/mod/survey/services/survey-sync.ts @@ -124,10 +124,11 @@ export class AddonModSurveySyncProvider extends CoreCourseActivitySyncBaseProvid userId = userId || site.getUserId(); const syncId = this.getSyncId(surveyId, userId); + const currentSyncPromise = this.getOngoingSync(syncId, siteId); - if (this.isSyncing(syncId, siteId)) { + if (currentSyncPromise) { // There's already a sync ongoing for this site, return the promise. - return this.getOngoingSync(syncId, siteId)!; + return currentSyncPromise; } this.logger.debug(`Try to sync survey '${surveyId}' for user '${userId}'`); diff --git a/src/addons/mod/wiki/services/wiki-sync.ts b/src/addons/mod/wiki/services/wiki-sync.ts index 691a08e9b..da3d9ae21 100644 --- a/src/addons/mod/wiki/services/wiki-sync.ts +++ b/src/addons/mod/wiki/services/wiki-sync.ts @@ -167,10 +167,11 @@ export class AddonModWikiSyncProvider extends CoreSyncBaseProvider { siteId = siteId || CoreSites.getCurrentSiteId(); - if (this.isSyncing(workshopId, siteId)) { + const currentSyncPromise = this.getOngoingSync(workshopId, siteId); + if (currentSyncPromise) { // There's already a sync ongoing for this discussion, return the promise. - return this.getOngoingSync(workshopId, siteId)!; + return currentSyncPromise; } // Verify that workshop isn't blocked. diff --git a/src/addons/notes/services/notes-sync.ts b/src/addons/notes/services/notes-sync.ts index a9dfac04d..218b18418 100644 --- a/src/addons/notes/services/notes-sync.ts +++ b/src/addons/notes/services/notes-sync.ts @@ -112,9 +112,10 @@ export class AddonNotesSyncProvider extends CoreSyncBaseProvider { siteId = siteId || CoreSites.getCurrentSiteId(); - if (this.isSyncing(courseId, siteId)) { + const currentSyncPromise = this.getOngoingSync(courseId, siteId); + if (currentSyncPromise) { // There's already a sync ongoing for notes, return the promise. - return this.getOngoingSync(courseId, siteId)!; + return currentSyncPromise; } this.logger.debug('Try to sync notes for course ' + courseId); diff --git a/src/core/classes/base-sync.ts b/src/core/classes/base-sync.ts index 9c5fc0229..8849ec24c 100644 --- a/src/core/classes/base-sync.ts +++ b/src/core/classes/base-sync.ts @@ -105,7 +105,7 @@ export class CoreSyncBaseProvider { try { return await promise; } finally { - delete this.syncPromises[siteId!][uniqueId]; + delete this.syncPromises[siteId][uniqueId]; } } @@ -133,15 +133,11 @@ export class CoreSyncBaseProvider { */ getOngoingSync(id: string | number, siteId?: string): Promise | undefined { siteId = siteId || CoreSites.getCurrentSiteId(); - - if (!this.isSyncing(id, siteId)) { - return; - } - - // There's already a sync ongoing for this id, return the promise. const uniqueId = this.getUniqueSyncId(id); - return this.syncPromises[siteId][uniqueId]; + if (this.syncPromises[siteId] && this.syncPromises[siteId][uniqueId] !== undefined) { + return this.syncPromises[siteId][uniqueId]; + } } /** @@ -223,11 +219,7 @@ export class CoreSyncBaseProvider { * @return Whether it's synchronizing. */ isSyncing(id: string | number, siteId?: string): boolean { - siteId = siteId || CoreSites.getCurrentSiteId(); - - const uniqueId = this.getUniqueSyncId(id); - - return !!(this.syncPromises[siteId] && this.syncPromises[siteId][uniqueId]); + return !!this.getOngoingSync(id, siteId); } /** @@ -331,10 +323,10 @@ export class CoreSyncBaseProvider { */ protected get componentTranslate(): string { if (!this.componentTranslateInternal) { - this.componentTranslateInternal = Translate.instant(this.componentTranslatableString); + this.componentTranslateInternal = Translate.instant(this.componentTranslatableString); } - return this.componentTranslateInternal!; + return this.componentTranslateInternal; } } diff --git a/src/core/features/comments/services/comments-sync.ts b/src/core/features/comments/services/comments-sync.ts index 1055c8c4b..f402366f8 100644 --- a/src/core/features/comments/services/comments-sync.ts +++ b/src/core/features/comments/services/comments-sync.ts @@ -158,10 +158,11 @@ export class CoreCommentsSyncProvider extends CoreSyncBaseProvider { siteId = siteId || CoreSites.getCurrentSiteId(); - if (this.isSyncing(courseId, siteId)) { + const currentSyncPromise = this.getOngoingSync(courseId, siteId); + if (currentSyncPromise) { // There's already a sync ongoing for this discussion, return the promise. - return this.getOngoingSync(courseId, siteId)!; + return currentSyncPromise; } this.logger.debug(`Try to sync course '${courseId}'`); diff --git a/src/core/features/rating/services/rating-sync.ts b/src/core/features/rating/services/rating-sync.ts index 9e6027ca9..09f1af9c2 100644 --- a/src/core/features/rating/services/rating-sync.ts +++ b/src/core/features/rating/services/rating-sync.ts @@ -157,9 +157,10 @@ export class CoreRatingSyncProvider extends CoreSyncBaseProvider { siteId = siteId || CoreSites.getCurrentSiteId(); const syncId = 'preferences'; + const currentSyncPromise = this.getOngoingSync(syncId, siteId); - if (this.isSyncing(syncId, siteId)) { + if (currentSyncPromise) { // There's already a sync ongoing, return the promise. - return this.getOngoingSync(syncId, siteId)!; + return currentSyncPromise; } this.logger.debug('Try to sync user preferences');