From 9f325e163f45809ec25850438abf03bdb3ee75a5 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Thu, 9 Apr 2020 15:25:23 +0200 Subject: [PATCH] MOBILE-3134 core: Debounce calls to update size in activity --- scripts/langindex.json | 1 + .../index/addon-mod-assign-index.html | 2 +- .../index/addon-mod-book-index.html | 2 +- .../index/addon-mod-choice-index.html | 2 +- .../index/addon-mod-data-index.html | 2 +- .../index/addon-mod-feedback-index.html | 2 +- .../index/addon-mod-folder-index.html | 2 +- .../index/addon-mod-forum-index.html | 2 +- .../index/addon-mod-glossary-index.html | 2 +- .../index/addon-mod-imscp-index.html | 2 +- .../index/addon-mod-lesson-index.html | 2 +- .../index/addon-mod-page-index.html | 2 +- .../index/addon-mod-quiz-index.html | 2 +- .../index/addon-mod-resource-index.html | 2 +- .../index/addon-mod-scorm-index.html | 2 +- .../index/addon-mod-survey-index.html | 2 +- .../index/addon-mod-wiki-index.html | 2 +- .../index/addon-mod-workshop-index.html | 2 +- src/assets/lang/en.json | 1 + src/core/constants.ts | 6 - .../course/classes/main-resource-component.ts | 12 +- src/core/course/lang/en.json | 1 + src/core/course/providers/helper.ts | 51 ++++++-- .../providers/module-prefetch-delegate.ts | 11 +- .../core-siteplugins-module-index.html | 2 +- src/providers/events.ts | 1 + src/providers/filepool.ts | 123 +++++++++++++----- src/providers/utils/utils.ts | 23 ++++ 28 files changed, 193 insertions(+), 73 deletions(-) diff --git a/scripts/langindex.json b/scripts/langindex.json index 212d23e5c..504e2bc4d 100644 --- a/scripts/langindex.json +++ b/scripts/langindex.json @@ -1390,6 +1390,7 @@ "core.course.allsections": "local_moodlemobileapp", "core.course.askadmintosupport": "local_moodlemobileapp", "core.course.availablespace": "local_moodlemobileapp", + "core.course.cannotdeletewhiledownloading": "local_moodlemobileapp", "core.course.confirmdeletemodulefiles": "local_moodlemobileapp", "core.course.confirmdownload": "local_moodlemobileapp", "core.course.confirmdownloadunknownsize": "local_moodlemobileapp", diff --git a/src/addon/mod/assign/components/index/addon-mod-assign-index.html b/src/addon/mod/assign/components/index/addon-mod-assign-index.html index 467f9e49d..ee087a9df 100644 --- a/src/addon/mod/assign/components/index/addon-mod-assign-index.html +++ b/src/addon/mod/assign/components/index/addon-mod-assign-index.html @@ -7,7 +7,7 @@ - + diff --git a/src/addon/mod/book/components/index/addon-mod-book-index.html b/src/addon/mod/book/components/index/addon-mod-book-index.html index 6fcc9f70d..cfd0ff551 100644 --- a/src/addon/mod/book/components/index/addon-mod-book-index.html +++ b/src/addon/mod/book/components/index/addon-mod-book-index.html @@ -9,7 +9,7 @@ - + diff --git a/src/addon/mod/choice/components/index/addon-mod-choice-index.html b/src/addon/mod/choice/components/index/addon-mod-choice-index.html index 762ad5066..bc5de5829 100644 --- a/src/addon/mod/choice/components/index/addon-mod-choice-index.html +++ b/src/addon/mod/choice/components/index/addon-mod-choice-index.html @@ -7,7 +7,7 @@ - + diff --git a/src/addon/mod/data/components/index/addon-mod-data-index.html b/src/addon/mod/data/components/index/addon-mod-data-index.html index 200fb4387..af4a82ad6 100644 --- a/src/addon/mod/data/components/index/addon-mod-data-index.html +++ b/src/addon/mod/data/components/index/addon-mod-data-index.html @@ -12,7 +12,7 @@ - + diff --git a/src/addon/mod/feedback/components/index/addon-mod-feedback-index.html b/src/addon/mod/feedback/components/index/addon-mod-feedback-index.html index 4355c5310..b491576c5 100644 --- a/src/addon/mod/feedback/components/index/addon-mod-feedback-index.html +++ b/src/addon/mod/feedback/components/index/addon-mod-feedback-index.html @@ -7,7 +7,7 @@ - + diff --git a/src/addon/mod/folder/components/index/addon-mod-folder-index.html b/src/addon/mod/folder/components/index/addon-mod-folder-index.html index 4627aaeba..2aa7976f3 100644 --- a/src/addon/mod/folder/components/index/addon-mod-folder-index.html +++ b/src/addon/mod/folder/components/index/addon-mod-folder-index.html @@ -6,7 +6,7 @@ - + diff --git a/src/addon/mod/forum/components/index/addon-mod-forum-index.html b/src/addon/mod/forum/components/index/addon-mod-forum-index.html index e941c514a..7e7935ae6 100644 --- a/src/addon/mod/forum/components/index/addon-mod-forum-index.html +++ b/src/addon/mod/forum/components/index/addon-mod-forum-index.html @@ -7,7 +7,7 @@ - + diff --git a/src/addon/mod/glossary/components/index/addon-mod-glossary-index.html b/src/addon/mod/glossary/components/index/addon-mod-glossary-index.html index 235c51297..0c01a1288 100644 --- a/src/addon/mod/glossary/components/index/addon-mod-glossary-index.html +++ b/src/addon/mod/glossary/components/index/addon-mod-glossary-index.html @@ -14,7 +14,7 @@ - + diff --git a/src/addon/mod/imscp/components/index/addon-mod-imscp-index.html b/src/addon/mod/imscp/components/index/addon-mod-imscp-index.html index 35cbae509..6be152d02 100644 --- a/src/addon/mod/imscp/components/index/addon-mod-imscp-index.html +++ b/src/addon/mod/imscp/components/index/addon-mod-imscp-index.html @@ -9,7 +9,7 @@ - + diff --git a/src/addon/mod/lesson/components/index/addon-mod-lesson-index.html b/src/addon/mod/lesson/components/index/addon-mod-lesson-index.html index f7ac672e0..834abcf38 100644 --- a/src/addon/mod/lesson/components/index/addon-mod-lesson-index.html +++ b/src/addon/mod/lesson/components/index/addon-mod-lesson-index.html @@ -7,7 +7,7 @@ - + diff --git a/src/addon/mod/page/components/index/addon-mod-page-index.html b/src/addon/mod/page/components/index/addon-mod-page-index.html index 378796f11..12f6ddbd3 100644 --- a/src/addon/mod/page/components/index/addon-mod-page-index.html +++ b/src/addon/mod/page/components/index/addon-mod-page-index.html @@ -6,7 +6,7 @@ - + diff --git a/src/addon/mod/quiz/components/index/addon-mod-quiz-index.html b/src/addon/mod/quiz/components/index/addon-mod-quiz-index.html index 88de59576..ec8685448 100644 --- a/src/addon/mod/quiz/components/index/addon-mod-quiz-index.html +++ b/src/addon/mod/quiz/components/index/addon-mod-quiz-index.html @@ -7,7 +7,7 @@ - + diff --git a/src/addon/mod/resource/components/index/addon-mod-resource-index.html b/src/addon/mod/resource/components/index/addon-mod-resource-index.html index 7d2e36a94..9fcfdaa83 100644 --- a/src/addon/mod/resource/components/index/addon-mod-resource-index.html +++ b/src/addon/mod/resource/components/index/addon-mod-resource-index.html @@ -6,7 +6,7 @@ - + diff --git a/src/addon/mod/scorm/components/index/addon-mod-scorm-index.html b/src/addon/mod/scorm/components/index/addon-mod-scorm-index.html index a9fcfd058..cd5e6b01a 100644 --- a/src/addon/mod/scorm/components/index/addon-mod-scorm-index.html +++ b/src/addon/mod/scorm/components/index/addon-mod-scorm-index.html @@ -7,7 +7,7 @@ - + diff --git a/src/addon/mod/survey/components/index/addon-mod-survey-index.html b/src/addon/mod/survey/components/index/addon-mod-survey-index.html index 42c41ddd0..eb101c594 100644 --- a/src/addon/mod/survey/components/index/addon-mod-survey-index.html +++ b/src/addon/mod/survey/components/index/addon-mod-survey-index.html @@ -7,7 +7,7 @@ - + diff --git a/src/addon/mod/wiki/components/index/addon-mod-wiki-index.html b/src/addon/mod/wiki/components/index/addon-mod-wiki-index.html index e043a6299..44a57c1ca 100644 --- a/src/addon/mod/wiki/components/index/addon-mod-wiki-index.html +++ b/src/addon/mod/wiki/components/index/addon-mod-wiki-index.html @@ -19,7 +19,7 @@ - + diff --git a/src/addon/mod/workshop/components/index/addon-mod-workshop-index.html b/src/addon/mod/workshop/components/index/addon-mod-workshop-index.html index 3ce7e2c32..c10216d25 100644 --- a/src/addon/mod/workshop/components/index/addon-mod-workshop-index.html +++ b/src/addon/mod/workshop/components/index/addon-mod-workshop-index.html @@ -7,7 +7,7 @@ - + diff --git a/src/assets/lang/en.json b/src/assets/lang/en.json index 3561e6111..3c3ea32d7 100644 --- a/src/assets/lang/en.json +++ b/src/assets/lang/en.json @@ -1390,6 +1390,7 @@ "core.course.allsections": "All sections", "core.course.askadmintosupport": "Contact the site administrator and tell them you want to use this activity with the Moodle Mobile app.", "core.course.availablespace": " You currently have about {{available}} free space.", + "core.course.cannotdeletewhiledownloading": "Files cannot be deleted while the activity is being downloaded. Please wait for the download to finish.", "core.course.confirmdeletemodulefiles": "Are you sure you want to delete these files?", "core.course.confirmdownload": "You are about to download {{size}}.{{availableSpace}} Are you sure you want to continue?", "core.course.confirmdownloadunknownsize": "It was not possible to calculate the size of the download.{{availableSpace}} Are you sure you want to continue?", diff --git a/src/core/constants.ts b/src/core/constants.ts index f498ff8a9..c9784ccc3 100644 --- a/src/core/constants.ts +++ b/src/core/constants.ts @@ -68,12 +68,6 @@ export class CoreConstants { static OUTDATED = 'outdated'; static NOT_DOWNLOADABLE = 'notdownloadable'; - // File status constants. - static FILE_ACTION_DOWNLOAD = 'download'; - static FILE_ACTION_DOWNLOADING = 'downloading'; - static FILE_ACTION_DELETED = 'deleted'; - static FILE_ACTION_OUTDATED = 'outdated'; - // Constants from Moodle's resourcelib. static RESOURCELIB_DISPLAY_AUTO = 0; // Try the best way. static RESOURCELIB_DISPLAY_EMBED = 1; // Display using object tag. diff --git a/src/core/course/classes/main-resource-component.ts b/src/core/course/classes/main-resource-component.ts index e568fec05..1bbff628d 100644 --- a/src/core/course/classes/main-resource-component.ts +++ b/src/core/course/classes/main-resource-component.ts @@ -261,9 +261,17 @@ export class CoreCourseModuleMainResourceComponent implements OnInit, OnDestroy, /** * Confirm and remove downloaded files. + * + * @param done Function to call when done. */ - removeFiles(): void { - this.courseHelper.confirmAndRemoveFiles(this.module, this.courseId); + removeFiles(done?: () => void): void { + if (this.prefetchStatus == CoreConstants.DOWNLOADING) { + this.domUtils.showAlertTranslated(null, 'core.course.cannotdeletewhiledownloading'); + + return; + } + + this.courseHelper.confirmAndRemoveFiles(this.module, this.courseId, done); } /** diff --git a/src/core/course/lang/en.json b/src/core/course/lang/en.json index 2fe5518e9..69cbe5cf9 100644 --- a/src/core/course/lang/en.json +++ b/src/core/course/lang/en.json @@ -5,6 +5,7 @@ "allsections": "All sections", "askadmintosupport": "Contact the site administrator and tell them you want to use this activity with the Moodle Mobile app.", "availablespace": " You currently have about {{available}} free space.", + "cannotdeletewhiledownloading": "Files cannot be deleted while the activity is being downloaded. Please wait for the download to finish.", "confirmdeletemodulefiles": "Are you sure you want to delete these files?", "confirmdownload": "You are about to download {{size}}.{{availableSpace}} Are you sure you want to continue?", "confirmdownloadunknownsize": "It was not possible to calculate the size of the download.{{availableSpace}} Are you sure you want to continue?", diff --git a/src/core/course/providers/helper.ts b/src/core/course/providers/helper.ts index c2b34853f..b93d3191b 100644 --- a/src/core/course/providers/helper.ts +++ b/src/core/course/providers/helper.ts @@ -13,12 +13,12 @@ // limitations under the License. import { Injectable, Injector } from '@angular/core'; -import { NavController } from 'ionic-angular'; +import { NavController, Loading } from 'ionic-angular'; import { TranslateService } from '@ngx-translate/core'; import { CoreAppProvider } from '@providers/app'; import { CoreEventsProvider } from '@providers/events'; import { CoreFileProvider } from '@providers/file'; -import { CoreFilepoolProvider } from '@providers/filepool'; +import { CoreFilepoolProvider, CoreFilepoolComponentFileEventData } from '@providers/filepool'; import { CoreFileHelperProvider } from '@providers/file-helper'; import { CoreSitesProvider } from '@providers/sites'; import { CoreDomUtilsProvider } from '@providers/utils/dom'; @@ -408,16 +408,29 @@ export class CoreCourseHelperProvider { * * @param module Module to remove the files. * @param courseId Course ID the module belongs to. + * @param done Function to call when done. It will close the context menu. * @return Promise resolved when done. */ - confirmAndRemoveFiles(module: any, courseId: number): Promise { - return this.domUtils.showDeleteConfirm('core.course.confirmdeletemodulefiles').then(() => { - return this.prefetchDelegate.removeModuleFiles(module, courseId); - }).catch((error) => { + async confirmAndRemoveFiles(module: any, courseId: number, done?: () => void): Promise { + let modal: Loading; + + try { + + await this.domUtils.showDeleteConfirm('core.course.confirmdeletemodulefiles'); + + modal = this.domUtils.showModalLoading(); + + await this.prefetchDelegate.removeModuleFiles(module, courseId); + + done && done(); + + } catch (error) { if (error) { this.domUtils.showErrorModal(error); } - }); + } finally { + modal && modal.dismiss(); + } } /** @@ -831,15 +844,27 @@ export class CoreCourseHelperProvider { } if (typeof instance.contextFileStatusObserver == 'undefined' && component) { - const componentFileChangeStatusEvent = CoreFilepoolProvider.getComponentEventName(siteId, component, module.id); + // Debounce the update size function to prevent too many calls when downloading or deleting a whole activity. + const debouncedUpdateSize = this.utils.debounce(() => { + this.prefetchDelegate.getModuleDownloadedSize(module, courseId).then((moduleSize) => { + instance.size = moduleSize > 0 ? this.textUtils.bytesToSize(moduleSize, 2) : 0; + }); + }, 1000); - instance.contextFileStatusObserver = this.eventsProvider.on(componentFileChangeStatusEvent, (data) => { + instance.contextFileStatusObserver = this.eventsProvider.on(CoreEventsProvider.COMPONENT_FILE_ACTION, + (data: CoreFilepoolComponentFileEventData) => { - if (((data.action == CoreConstants.FILE_ACTION_DOWNLOAD && data.success == true) || - data.action == CoreConstants.FILE_ACTION_DELETED) && - instance.prefetchStatus != CoreConstants.DOWNLOADING) { - this.fillContextMenu(instance, module, courseId, true, component); + if (data.component != component || data.componentId != module.id) { + // The event doesn't belong to this component, ignore. + return; } + + if (!this.filepoolProvider.isFileEventDownloadedOrDeleted(data)) { + return; + } + + // Update the module size. + debouncedUpdateSize(); }, siteId); } }); diff --git a/src/core/course/providers/module-prefetch-delegate.ts b/src/core/course/providers/module-prefetch-delegate.ts index 6e05b283b..ef0c1ad04 100644 --- a/src/core/course/providers/module-prefetch-delegate.ts +++ b/src/core/course/providers/module-prefetch-delegate.ts @@ -15,7 +15,7 @@ import { Injectable } from '@angular/core'; import { CoreEventsProvider } from '@providers/events'; import { CoreFileProvider } from '@providers/file'; -import { CoreFilepoolProvider } from '@providers/filepool'; +import { CoreFilepoolProvider, CoreFilepoolComponentFileEventData } from '@providers/filepool'; import { CoreLoggerProvider } from '@providers/logger'; import { CoreSitesProvider, CoreSiteSchema } from '@providers/sites'; import { CoreTimeUtilsProvider } from '@providers/utils/time'; @@ -276,6 +276,15 @@ export class CoreCourseModulePrefetchDelegate extends CoreDelegate { eventsProvider.on(CoreEventsProvider.PACKAGE_STATUS_CHANGED, (data) => { this.updateStatusCache(data.status, data.component, data.componentId); }, this.sitesProvider.getCurrentSiteId()); + + // If a file inside a module is downloaded/deleted, clear the corresponding cache. + eventsProvider.on(CoreEventsProvider.COMPONENT_FILE_ACTION, (data: CoreFilepoolComponentFileEventData) => { + if (!this.filepoolProvider.isFileEventDownloadedOrDeleted(data)) { + return; + } + + this.statusCache.invalidate(this.filepoolProvider.getPackageId(data.component, data.componentId)); + }, this.sitesProvider.getCurrentSiteId()); } /** diff --git a/src/core/siteplugins/components/module-index/core-siteplugins-module-index.html b/src/core/siteplugins/components/module-index/core-siteplugins-module-index.html index f3c8b832d..538055ea5 100644 --- a/src/core/siteplugins/components/module-index/core-siteplugins-module-index.html +++ b/src/core/siteplugins/components/module-index/core-siteplugins-module-index.html @@ -5,7 +5,7 @@ - + diff --git a/src/providers/events.ts b/src/providers/events.ts index abed87a95..4da2900c0 100644 --- a/src/providers/events.ts +++ b/src/providers/events.ts @@ -47,6 +47,7 @@ export class CoreEventsProvider { static PACKAGE_STATUS_CHANGED = 'package_status_changed'; static COURSE_STATUS_CHANGED = 'course_status_changed'; static SECTION_STATUS_CHANGED = 'section_status_changed'; + static COMPONENT_FILE_ACTION = 'component_file_action'; static SITE_PLUGINS_LOADED = 'site_plugins_loaded'; static SITE_PLUGINS_COURSE_RESTRICT_UPDATED = 'site_plugins_course_restrict_updated'; static LOGIN_SITE_CHECKED = 'login_site_checked'; diff --git a/src/providers/filepool.ts b/src/providers/filepool.ts index 6235c973a..a3889773d 100644 --- a/src/providers/filepool.ts +++ b/src/providers/filepool.ts @@ -212,6 +212,51 @@ export interface CoreFilepoolComponentLink { componentId?: string | number; } +/** + * File actions. + */ +export const enum CoreFilepoolFileActions { + DOWNLOAD = 'download', + DOWNLOADING = 'downloading', + DELETED = 'deleted', + OUTDATED = 'outdated', +} + +/** + * Data sent to file events. + */ +export interface CoreFilepoolFileEventData { + /** + * The file ID. + */ + fileId: string; + + /** + * The file ID. + */ + action: CoreFilepoolFileActions; + + /** + * Whether the action was a success. Only for DOWNLOAD action. + */ + success?: boolean; +} + +/** + * Data sent to component file events. + */ +export interface CoreFilepoolComponentFileEventData extends CoreFilepoolFileEventData { + /** + * The component. + */ + component: string; + + /** + * The component ID. + */ + componentId: string | number; +} + /* * Factory for handling downloading files and retrieve downloaded files. * @@ -1218,8 +1263,9 @@ export class CoreFilepoolProvider { downloadUrl(siteId: string, fileUrl: string, ignoreStale?: boolean, component?: string, componentId?: string | number, timemodified: number = 0, onProgress?: (event: any) => any, filePath?: string, options: any = {}, revision?: number) : Promise { - let fileId, - promise; + let fileId; + let promise; + let alreadyDownloaded = true; if (this.fileProvider.isAvailable()) { return this.fixPluginfileURL(siteId, fileUrl).then((file) => { @@ -1239,13 +1285,15 @@ export class CoreFilepoolProvider { if (typeof fileObject === 'undefined') { // We do not have the file, download and add to pool. this.notifyFileDownloading(siteId, fileId, links); + alreadyDownloaded = false; return this.downloadForPoolByUrl(siteId, fileUrl, options, filePath, onProgress); } else if (this.isFileOutdated(fileObject, options.revision, options.timemodified) && - this.appProvider.isOnline() && !ignoreStale) { + this.appProvider.isOnline() && !ignoreStale) { // The file is outdated, force the download and update it. this.notifyFileDownloading(siteId, fileId, links); + alreadyDownloaded = false; return this.downloadForPoolByUrl(siteId, fileUrl, options, filePath, onProgress, fileObject); } @@ -1262,6 +1310,7 @@ export class CoreFilepoolProvider { }, () => { // The file was not found in the pool, weird. this.notifyFileDownloading(siteId, fileId, links); + alreadyDownloaded = false; return this.downloadForPoolByUrl(siteId, fileUrl, options, filePath, onProgress, fileObject); }); @@ -1269,6 +1318,7 @@ export class CoreFilepoolProvider { }, () => { // The file is not in the pool just yet. this.notifyFileDownloading(siteId, fileId, links); + alreadyDownloaded = false; return this.downloadForPoolByUrl(siteId, fileUrl, options, filePath, onProgress); }).then((response) => { @@ -1277,7 +1327,10 @@ export class CoreFilepoolProvider { // Ignore errors. }); } - this.notifyFileDownloaded(siteId, fileId, links); + + if (!alreadyDownloaded) { + this.notifyFileDownloaded(siteId, fileId, links); + } return response; }, (err) => { @@ -1434,18 +1487,6 @@ export class CoreFilepoolProvider { }); } - /** - * Get the name of the event used to notify file status to component (CoreEventsProvider). - * - * @param siteId The site ID. - * @param component The component name. - * @param componentId The component ID. - * @return Event name. - */ - static getComponentEventName(siteId: string, component: string, componentId: string | number): string { - return 'CoreFilepoolComponentFile:' + siteId + ':' + component + ':' + componentId; - } - /** * Convenience function to get component files. * @@ -2450,6 +2491,17 @@ export class CoreFilepoolProvider { }); } + /** + * Whether a file action indicates a file was downloaded or deleted. + * + * @param data Event data. + * @return Whether downloaded or deleted. + */ + isFileEventDownloadedOrDeleted(data: CoreFilepoolFileEventData): boolean { + return (data.action == CoreFilepoolFileActions.DOWNLOAD && data.success == true) || + data.action == CoreFilepoolFileActions.DELETED; + } + /** * Check whether a file is downloadable. * @@ -2508,14 +2560,19 @@ export class CoreFilepoolProvider { * Notify an action performed on a file to a list of components. * * @param siteId The site ID. - * @param eventData The event data. + * @param eventData The file event data. * @param links The links to the components. */ - protected notifyFileActionToComponents(siteId: string, eventData: any, links: CoreFilepoolComponentLink[]): void { + protected notifyFileActionToComponents(siteId: string, eventData: CoreFilepoolFileEventData, + links: CoreFilepoolComponentLink[]): void { links.forEach((link) => { - this.eventsProvider.trigger(CoreFilepoolProvider.getComponentEventName(siteId, link.component, link.componentId), - eventData, siteId); + const data: CoreFilepoolComponentFileEventData = Object.assign({ + component: link.component, + componentId: link.componentId, + }, eventData); + + this.eventsProvider.trigger(CoreEventsProvider.COMPONENT_FILE_ACTION, data, siteId); }); } @@ -2527,9 +2584,9 @@ export class CoreFilepoolProvider { * @param links The links to components. */ protected notifyFileDeleted(siteId: string, fileId: string, links: CoreFilepoolComponentLink[]): void { - const data = { + const data: CoreFilepoolFileEventData = { fileId: fileId, - action: CoreConstants.FILE_ACTION_DELETED + action: CoreFilepoolFileActions.DELETED, }; this.eventsProvider.trigger(this.getFileEventName(siteId, fileId), data); @@ -2544,10 +2601,10 @@ export class CoreFilepoolProvider { * @param links The links to components. */ protected notifyFileDownloaded(siteId: string, fileId: string, links: CoreFilepoolComponentLink[]): void { - const data = { + const data: CoreFilepoolFileEventData = { fileId: fileId, - action: CoreConstants.FILE_ACTION_DOWNLOAD, - success: true + action: CoreFilepoolFileActions.DOWNLOAD, + success: true, }; this.eventsProvider.trigger(this.getFileEventName(siteId, fileId), data); @@ -2562,10 +2619,10 @@ export class CoreFilepoolProvider { * @param links The links to components. */ protected notifyFileDownloadError(siteId: string, fileId: string, links: CoreFilepoolComponentLink[]): void { - const data = { + const data: CoreFilepoolFileEventData = { fileId: fileId, - action: CoreConstants.FILE_ACTION_DOWNLOAD, - success: false + action: CoreFilepoolFileActions.DOWNLOAD, + success: false, }; this.eventsProvider.trigger(this.getFileEventName(siteId, fileId), data); @@ -2580,9 +2637,9 @@ export class CoreFilepoolProvider { * @param links The links to components. */ protected notifyFileDownloading(siteId: string, fileId: string, links: CoreFilepoolComponentLink[]): void { - const data = { + const data: CoreFilepoolFileEventData = { fileId: fileId, - action: CoreConstants.FILE_ACTION_DOWNLOADING + action: CoreFilepoolFileActions.DOWNLOADING, }; this.eventsProvider.trigger(this.getFileEventName(siteId, fileId), data); @@ -2598,8 +2655,9 @@ export class CoreFilepoolProvider { * @param links The links to components. */ protected notifyFileOutdated(siteId: string, fileId: string, links: CoreFilepoolComponentLink[]): void { - const data = { - action: CoreConstants.FILE_ACTION_OUTDATED + const data: CoreFilepoolFileEventData = { + fileId: fileId, + action: CoreFilepoolFileActions.OUTDATED, }; this.eventsProvider.trigger(this.getFileEventName(siteId, fileId), data); @@ -2729,7 +2787,6 @@ export class CoreFilepoolProvider { }).finally(() => { this.treatQueueDeferred(siteId, fileId, true); }); - this.notifyFileDownloaded(siteId, fileId, links); return; } diff --git a/src/providers/utils/utils.ts b/src/providers/utils/utils.ts index bedd0e4b0..bebc3ce1c 100644 --- a/src/providers/utils/utils.ts +++ b/src/providers/utils/utils.ts @@ -1392,4 +1392,27 @@ export class CoreUtilsProvider { return filtered; } + + /** + * Debounce a function so consecutive calls are ignored until a certain time has passed since the last call. + * + * @param context The context to apply to the function. + * @param fn Function to debounce. + * @param delay Time that must pass until the function is called. + * @return Debounced function. + */ + debounce(fn: (...args: any[]) => any, delay: number): (...args: any[]) => void { + + let timeoutID: number; + + const debounced = (...args: any[]): void => { + clearTimeout(timeoutID); + + timeoutID = window.setTimeout(() => { + fn.apply(null, args); + }, delay); + }; + + return debounced; + } }