MOBILE-2310 course: Implement prefetch delegate and base handler

main
Dani Palou 2018-01-16 12:35:16 +01:00
parent 9b6295e97c
commit 64f1551d56
7 changed files with 1881 additions and 25 deletions

View File

@ -0,0 +1,99 @@
// (C) Copyright 2015 Martin Dougiamas
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* A cache to store values in memory to speed up processes.
*
* The data is organized by "entries" that are identified by an ID. Each entry can have multiple values stored,
* and each value has its own timemodified.
*
* Values expire after a certain time.
*/
export class CoreCache {
protected cacheStore = {};
constructor() {}
/**
* Clear the cache.
*/
clear() {
this.cacheStore = {};
}
/**
* Get all the data stored in the cache for a certain id.
*
* @param {any} id The ID to identify the entry.
* @return {any} The data from the cache. Undefined if not found.
*/
getEntry(id: any) : any {
if (!this.cacheStore[id]) {
this.cacheStore[id] = {};
}
return this.cacheStore[id];
}
/**
* Get the status of a module from the "cache".
*
* @param {any} id The ID to identify the entry.
* @param {string} name Name of the value to get.
* @param {boolean} [ignoreInvalidate] Whether it should always return the cached data, even if it's expired.
* @return {any} Cached value. Undefined if not cached or expired.
*/
getValue(id: any, name: string, ignoreInvalidate?: boolean) : any {
const entry = this.getEntry(id);
if (entry[name] && typeof entry[name].value != 'undefined') {
const now = Date.now();
// Invalidate after 5 minutes.
if (ignoreInvalidate || entry[name].timemodified + 300000 >= now) {
return entry[name].value;
}
}
return undefined;
}
/**
* Invalidate all the cached data for a certain entry.
*
* @param {any} id The ID to identify the entry.
*/
invalidate(id: any) : void {
const entry = this.getEntry(id);
for (let name in entry) {
entry[name].timemodified = 0;
}
}
/**
* Update the status of a module in the "cache".
*
* @param {any} id The ID to identify the entry.
* @param {string} name Name of the value to set.
* @param {any} value Value to set.
* @return {any} The set value.
*/
setValue(id: any, name: string, value: any) : any {
const entry = this.getEntry(id);
entry[name] = {
value: value,
timemodified: Date.now()
};
return value;
}
}

View File

@ -0,0 +1,514 @@
// (C) Copyright 2015 Martin Dougiamas
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { Injector } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { CoreAppProvider } from '../../../providers/app';
import { CoreFilepoolProvider } from '../../../providers/filepool';
import { CoreSitesProvider } from '../../../providers/sites';
import { CoreDomUtilsProvider } from '../../../providers/utils/dom';
import { CoreUtilsProvider } from '../../../providers/utils/utils';
import { CoreCourseProvider } from '../providers/course';
import { CoreCourseModulePrefetchHandler } from '../providers/module-prefetch-delegate';
import { CoreConstants } from '../../constants';
/**
* A prefetch function to be passed to prefetchPackage.
* This function should NOT call storePackageStatus, downloadPackage or prefetchPakage from filepool.
* It receives the same params as prefetchPackage except the function itself. This includes all extra parameters sent after siteId.
* The string returned by this function will be stored as "extra" data in the filepool package. If you don't need to store
* extra data, don't return anything.
*
* @param {any} module Module.
* @param {number} courseId Course ID the module belongs to.
* @param {boolean} single True if we're downloading a single module, false if we're downloading a whole section.
* @param {string} siteId Site ID. If not defined, current site.
* @return {Promise<string>} Promise resolved when the prefetch finishes. The string returned will be stored as "extra" data in the
* filepool package. If you don't need to store extra data, don't return anything.
*/
export type prefetchFunction = (module: any, courseId: number, single: boolean, siteId: string, ...args) => Promise<string>;
/**
* Base prefetch handler to be registered in CoreCourseModulePrefetchDelegate. It is useful to minimize the amount of
* functions that handlers need to implement. It also provides some helper features like preventing a module to be
* downloaded twice at the same time.
*
* If your handler inherits from this service, you just need to override the functions that you want to change.
*
* The implementation of this default handler is aimed for resources that only need to prefetch files, not WebService calls.
*
* By default, prefetching a module will only download its files (downloadOrPrefetch). This might be enough for resources.
* If you need to prefetch WebServices, then you need to override the "download" and "prefetch" functions. In this case, it's
* recommended to call the prefetchPackage function since it'll handle changing the status of the module.
*/
export class CoreCourseModulePrefetchHandlerBase implements CoreCourseModulePrefetchHandler {
/**
* A name to identify the addon.
* @type {string}
*/
name = 'CoreCourseModulePrefetchHandlerBase';
/**
* Name of the module. It should match the "modname" of the module returned in core_course_get_contents.
* @type {string}
*/
modname = '';
/**
* The handler's component.
* @type {string}
*/
component = 'core_module';
/**
* The RegExp to check updates. If a module has an update whose name matches this RegExp, the module will be marked
* as outdated. This RegExp is ignored if hasUpdates function is defined.
* @type {RegExp}
*/
updatesNames = /^.*files$/;
/**
* Whether the module is a resource (true) or an activity (false).
* @type {boolean}
*/
isResource: boolean;
/**
* List of download promises to prevent downloading the module twice at the same time.
* @type {Object}
*/
protected downloadPromises: {[s: string]: {[s: string]: Promise<any>}} = {};
// List of services that will be injected using injector. It's done like this so subclasses don't have to send all the
// services to the parent in the constructor.
protected translate: TranslateService;
protected appProvider: CoreAppProvider;
protected courseProvider: CoreCourseProvider;
protected filepoolProvider: CoreFilepoolProvider;
protected sitesProvider: CoreSitesProvider;
protected domUtils: CoreDomUtilsProvider;
protected utils: CoreUtilsProvider;
constructor(injector: Injector) {
this.translate = injector.get(TranslateService);
this.appProvider = injector.get(CoreAppProvider);
this.courseProvider = injector.get(CoreCourseProvider);
this.filepoolProvider = injector.get(CoreFilepoolProvider);
this.sitesProvider = injector.get(CoreSitesProvider);
this.domUtils = injector.get(CoreDomUtilsProvider);
this.utils = injector.get(CoreUtilsProvider);
}
/**
* Add an ongoing download to the downloadPromises list. When the promise finishes it will be removed.
*
* @param {number} id Unique identifier per component.
* @param {Promise<any>} promise Promise to add.
* @param {String} [siteId] Site ID. If not defined, current site.
* @return {Promise<any>} Promise of the current download.
*/
addOngoingDownload(id: number, promise: Promise<any>, siteId?: string) : Promise<any> {
siteId = siteId || this.sitesProvider.getCurrentSiteId();
const uniqueId = this.getUniqueId(id);
if (!this.downloadPromises[siteId]) {
this.downloadPromises[siteId] = {};
}
this.downloadPromises[siteId][uniqueId] = promise.finally(() => {
delete this.downloadPromises[siteId][uniqueId];
});
return this.downloadPromises[siteId][uniqueId];
}
/**
* Download the module.
*
* @param {any} module The module object returned by WS.
* @param {number} courseId Course ID.
* @return {Promise<any>} Promise resolved when all content is downloaded.
*/
download(module: any, courseId: number) : Promise<any> {
return this.downloadOrPrefetch(module, courseId, false);
}
/**
* Download or prefetch the content.
*
* @param {any} module The module object returned by WS.
* @param {number} courseId Course ID.
* @param {boolean} [prefetch] True to prefetch, false to download right away.
* @param {string} [dirPath] Path of the directory where to store all the content files. This is to keep the files
* relative paths and make the package work in an iframe. Undefined to download the files
* in the filepool root folder.
* @return {Promise<any>} Promise resolved when all content is downloaded. Data returned is not reliable.
*/
downloadOrPrefetch(module: any, courseId: number, prefetch?: boolean, dirPath?: string) : Promise<any> {
if (!this.appProvider.isOnline()) {
// Cannot download in offline.
return Promise.reject(this.translate.instant('core.networkerrormsg'));
}
const siteId = this.sitesProvider.getCurrentSiteId();
// Load module contents (ignore cache so we always have the latest data).
return this.loadContents(module, courseId, true).then(() => {
// Get the intro files.
return this.getIntroFiles(module, courseId);
}).then((introFiles) => {
let downloadFn = prefetch ? this.filepoolProvider.prefetchPackage.bind(this.filepoolProvider) :
this.filepoolProvider.downloadPackage.bind(this.filepoolProvider),
contentFiles = this.getContentDownloadableFiles(module),
promises = [];
if (dirPath) {
// Download intro files in filepool root folder.
promises.push(this.filepoolProvider.downloadOrPrefetchFiles(siteId, introFiles, prefetch, false,
this.component, module.id));
// Download content files inside dirPath.
promises.push(downloadFn(siteId, contentFiles, this.component, module.id, undefined, dirPath));
} else {
// No dirPath, download everything in filepool root folder.
let files = introFiles.concat(contentFiles);
promises.push(downloadFn(siteId, files, this.component, module.id));
}
return Promise.all(promises);
});
}
/**
* Returns a list of content files that can be downloaded.
*
* @param {any} module The module object returned by WS.
* @return {any[]} List of files.
*/
getContentDownloadableFiles(module: any) {
let files = [];
if (module.contents && module.contents.length) {
module.contents.forEach((content) => {
if (this.isFileDownloadable(content)) {
files.push(content);
}
});
}
return files;
}
/**
* Get the download size of a module.
*
* @param {any} module Module.
* @param {Number} courseId Course ID the module belongs to.
* @param {boolean} [single] True if we're downloading a single module, false if we're downloading a whole section.
* @return {Promise<{size: number, total: boolean}>} Promise resolved with the size and a boolean indicating if it was able
* to calculate the total size.
*/
getDownloadSize(module: any, courseId: number, single?: boolean) : Promise<{size: number, total: boolean}> {
return this.getFiles(module, courseId).then((files) => {
return this.utils.sumFileSizes(files);
}).catch(() => {
return {size: -1, total: false};
});
}
/**
* Get the downloaded size of a module. If not defined, we'll use getFiles to calculate it (it can be slow).
*
* @param {any} module Module.
* @param {number} courseId Course ID the module belongs to.
* @return {number|Promise<number>} Size, or promise resolved with the size.
*/
getDownloadedSize?(module: any, courseId: number) : number|Promise<number> {
const siteId = this.sitesProvider.getCurrentSiteId();
return this.filepoolProvider.getFilesSizeByComponent(siteId, this.component, module.id);
}
/**
* Get list of files. If not defined, we'll assume they're in module.contents.
*
* @param {any} module Module.
* @param {Number} courseId Course ID the module belongs to.
* @param {boolean} [single] True if we're downloading a single module, false if we're downloading a whole section.
* @return {Promise<any[]>} Promise resolved with the list of files.
*/
getFiles(module: any, courseId: number, single?: boolean) : Promise<any[]> {
// Load module contents if needed.
return this.loadContents(module, courseId).then(() => {
return this.getIntroFiles(module, courseId).then((files) => {
return files.concat(this.getContentDownloadableFiles(module));
});
});
}
/**
* Returns module intro files.
*
* @param {Object} module The module object returned by WS.
* @param {Number} courseId Course ID.
* @return {Promise} Promise resolved with list of intro files.
*/
getIntroFiles(module: any, courseId: number) : Promise<any[]> {
return Promise.resolve(this.getIntroFilesFromInstance(module));
}
/**
* Returns module intro files from instance.
*
* @param {any} module The module object returned by WS.
* @param {any} [instance] The instance to get the intro files (book, assign, ...). If not defined, module will be used.
* @return {any[]} List of intro files.
*/
getIntroFilesFromInstance(module: any, instance?: any) {
if (instance) {
if (typeof instance.introfiles != 'undefined') {
return instance.introfiles;
} else if (instance.intro) {
return this.domUtils.extractDownloadableFilesFromHtmlAsFakeFileObjects(instance.intro);
}
}
if (module.description) {
return this.domUtils.extractDownloadableFilesFromHtmlAsFakeFileObjects(module.description);
}
return [];
}
/**
* If there's an ongoing download for a certain identifier return it.
*
* @param {number} id Unique identifier per component.
* @param {string} [siteId] Site ID. If not defined, current site.
* @return {Promise<any>} Promise of the current download.
*/
getOngoingDownload(id: number, siteId?: string) : Promise<any> {
siteId = siteId || this.sitesProvider.getCurrentSiteId();
if (this.isDownloading(id, siteId)) {
// There's already a download ongoing, return the promise.
return this.downloadPromises[siteId][this.getUniqueId(id)];
}
return Promise.resolve();
}
/**
* Create unique identifier using component and id.
*
* @param {number} id Unique ID inside component.
* @return {string} Unique ID.
*/
getUniqueId(id: number) {
return this.component + '#' + id;
}
/**
* Invalidate the prefetched content.
*
* @param {number} moduleId The module ID.
* @return {Promise<any>} Promise resolved when the data is invalidated.
*/
invalidateContent(moduleId: number) : Promise<any> {
const promises = [],
siteId = this.sitesProvider.getCurrentSiteId();
promises.push(this.courseProvider.invalidateModule(moduleId));
promises.push(this.filepoolProvider.invalidateFilesByComponent(siteId, this.component, moduleId));
return Promise.all(promises);
}
/**
* Invalidate WS calls needed to determine module status. It doesn't need to invalidate check updates.
* It should NOT invalidate files nor all the prefetched data.
*
* @param {any} module Module.
* @param {number} courseId Course ID the module belongs to.
* @return {Promise<any>} Promise resolved when invalidated.
*/
invalidateModule(module: any, courseId: number) : Promise<any> {
return this.courseProvider.invalidateModule(module.id);
}
/**
* Check if a module can be downloaded. If the function is not defined, we assume that all modules are downloadable.
*
* @param {any} module Module.
* @param {number} courseId Course ID the module belongs to.
* @return {boolean|Promise<boolean>} Whether the module can be downloaded. The promise should never be rejected.
*/
isDownloadable(module: any, courseId: number) : boolean|Promise<boolean> {
// By default, mark all instances as downloadable.
return true;
}
/**
* Check if a there's an ongoing download for the given identifier.
*
* @param {number} id Unique identifier per component.
* @param {string} [siteId] Site ID. If not defined, current site.
* @return {Boolean} True if downloading, false otherwise.
*/
isDownloading(id: number, siteId?: string) : boolean {
siteId = siteId || this.sitesProvider.getCurrentSiteId();
return !!(this.downloadPromises[siteId] && this.downloadPromises[siteId][this.getUniqueId(id)]);
}
/**
* Whether or not the handler is enabled on a site level.
*
* @return {boolean|Promise<boolean>} A boolean, or a promise resolved with a boolean, indicating if the handler is enabled.
*/
isEnabled() : boolean|Promise<boolean> {
return true;
}
/**
* Check if a file is downloadable.
*
* @param {any} file File to check.
* @return {boolean} Whether the file is downloadable.
*/
isFileDownloadable(file: any) : boolean {
return file.type === 'file';
}
/**
* Load module contents into module.contents if they aren't loaded already.
*
* @param {any} module Module to load the contents.
* @param {number} [courseId] The course ID. Recommended to speed up the process and minimize data usage.
* @param {boolean} [ignoreCache] True if it should ignore cached data (it will always fail in offline or server down).
* @return {Promise} Promise resolved when loaded.
*/
loadContents(module: any, courseId: number, ignoreCache?: boolean) : Promise<void> {
if (this.isResource) {
return this.courseProvider.loadModuleContents(module, courseId, undefined, false, ignoreCache);
}
return Promise.resolve();
}
/**
* Prefetch a module.
*
* @param {any} module Module.
* @param {number} courseId Course ID the module belongs to.
* @param {boolean} [single] True if we're downloading a single module, false if we're downloading a whole section.
* @return {Promise<any>} Promise resolved when done.
*/
prefetch(module: any, courseId?: number, single?: boolean): Promise<any> {
return this.downloadOrPrefetch(module, courseId, true);
}
/**
* Prefetch the module, setting package status at start and finish.
*
* Example usage from a child instance:
* return this.prefetchPackage(module, courseId, single, this.prefetchModule.bind(this), siteId, someParam, anotherParam);
*
* Then the function "prefetchModule" will receive params:
* prefetchModule(module, courseId, single, siteId, someParam, anotherParam)
*
* @param {any} module Module.
* @param {number} courseId Course ID the module belongs to.
* @param {boolean} [single] True if we're downloading a single module, false if we're downloading a whole section.
* @param {prefetchFunction} downloadFn Function to perform the prefetch. Please check the documentation of prefetchFunction.
* @param {string} [siteId] Site ID. If not defined, current site.
* @return {Promise<any>} Promise resolved when the module has been downloaded. Data returned is not reliable.
*/
prefetchPackage(module: any, courseId: number, single: boolean, downloadFn: prefetchFunction, siteId?: string, ...args) :
Promise<any> {
siteId = siteId || this.sitesProvider.getCurrentSiteId();
if (!this.appProvider.isOnline()) {
// Cannot prefetch in offline.
return Promise.reject(this.translate.instant('core.networkerrormsg'));
}
if (this.isDownloading(module.id, siteId)) {
// There's already a download ongoing for this module, return the promise.
return this.getOngoingDownload(module.id, siteId);
}
const prefetchPromise = this.setDownloading(module.id, siteId).then(() => {
// Package marked as downloading, call the download function.
// Send all the params except downloadFn. This includes all params passed after siteId.
return downloadFn.apply(downloadFn, [module, courseId, single, siteId].concat(args));
}).then((extra: string) => {
// Prefetch finished, mark as downloaded.
return this.setDownloaded(module.id, siteId, extra);
}).catch((error) => {
// Error prefetching, go back to previous status and reject the promise.
return this.setPreviousStatusAndReject(module.id, error, siteId);
});
return this.addOngoingDownload(module.id, prefetchPromise, siteId);
}
/**
* Mark the module as downloaded.
*
* @param {number} id Unique identifier per component.
* @param {string} [siteId] Site ID. If not defined, current site.
* @param {string} [extra] Extra data to store.
* @return {Promise<any>} Promise resolved when done.
*/
setDownloaded(id: number, siteId?: string, extra?: string) : Promise<any> {
siteId = siteId || this.sitesProvider.getCurrentSiteId();
return this.filepoolProvider.storePackageStatus(siteId, CoreConstants.DOWNLOADED, this.component, id, extra);
}
/**
* Mark the module as downloading.
*
* @param {number} id Unique identifier per component.
* @param {string} [siteId] Site ID. If not defined, current site.
* @return {Promise<any>} Promise resolved when done.
*/
setDownloading(id: number, siteId?: string) : Promise<any> {
siteId = siteId || this.sitesProvider.getCurrentSiteId();
return this.filepoolProvider.storePackageStatus(siteId, CoreConstants.DOWNLOADING, this.component, id);
}
/**
* Set previous status and return a rejected promise.
*
* @param {number} id Unique identifier per component.
* @param {any} [error] Error to return.
* @param {string} [siteId] Site ID. If not defined, current site.
* @return {Promise<never>} Rejected promise.
*/
setPreviousStatusAndReject(id: number, error?: any, siteId?: string) : Promise<never> {
siteId = siteId || this.sitesProvider.getCurrentSiteId();
return this.filepoolProvider.setPackagePreviousStatus(siteId, this.component, id).then(() => {
return Promise.reject(error);
});
}
/**
* Remove module downloaded files. If not defined, we'll use getFiles to remove them (slow).
*
* @param {any} module Module.
* @param {number} courseId Course ID the module belongs to.
* @return {Promise<any>} Promise resolved when done.
*/
removeFiles(module: any, courseId: number) : Promise<any> {
return this.filepoolProvider.removeFilesByComponent(this.sitesProvider.getCurrentSiteId(), this.component, module.id);
}
}

View File

@ -17,6 +17,7 @@ import { CoreCourseProvider } from './providers/course';
import { CoreCourseHelperProvider } from './providers/helper';
import { CoreCourseFormatDelegate } from './providers/format-delegate';
import { CoreCourseModuleDelegate } from './providers/module-delegate';
import { CoreCourseModulePrefetchDelegate } from './providers/module-prefetch-delegate';
import { CoreCourseFormatDefaultHandler } from './providers/default-format';
import { CoreCourseFormatTopicsModule} from './formats/topics/topics.module';
import { CoreCourseFormatWeeksModule } from './formats/weeks/weeks.module';
@ -32,6 +33,7 @@ import { CoreCourseFormatWeeksModule } from './formats/weeks/weeks.module';
CoreCourseHelperProvider,
CoreCourseFormatDelegate,
CoreCourseModuleDelegate,
CoreCourseModulePrefetchDelegate,
CoreCourseFormatDefaultHandler
],
exports: []

View File

@ -62,7 +62,7 @@ export class CoreCourseProvider {
type: 'INTEGER'
}
]
}
};
protected logger;
protected CORE_MODULES = [
@ -482,6 +482,26 @@ export class CoreCourseProvider {
return this.ROOT_CACHE_KEY + 'sections:' + courseId;
}
/**
* Given a list of sections, returns the list of modules in the sections.
*
* @param {any[]} sections Sections.
* @return {any[]} Modules.
*/
getSectionsModules(sections: any[]) : any[] {
if (!sections || !sections.length) {
return [];
}
let modules = [];
sections.forEach((section) => {
if (section.modules) {
modules = modules.concat(section.modules);
}
});
return modules;
}
/**
* Invalidates module WS call.
*

View File

@ -82,26 +82,6 @@ export class CoreCourseHelperProvider {
});
}
/**
* Given a list of sections, returns the list of modules in the sections.
*
* @param {any[]} sections Sections.
* @return {any[]} Modules.
*/
getSectionsModules(sections: any[]) : any[] {
if (!sections || !sections.length) {
return [];
}
let modules = [];
sections.forEach((section) => {
if (section.modules) {
modules = modules.concat(section.modules);
}
});
return modules;
}
/**
* Check if a section has content.
*

File diff suppressed because it is too large Load Diff

View File

@ -104,10 +104,11 @@ export class CoreUtilsProvider {
*
* @param {any[]} array The array to convert.
* @param {string} propertyName The name of the property to use as the key.
* @param {any} [result] Object where to put the properties. If not defined, a new object will be created.
* @return {any} The object.
*/
arrayToObject(array: any[], propertyName: string) : any {
let result = {};
arrayToObject(array: any[], propertyName: string, result?: any) : any {
result = result || {};
array.forEach((entry) => {
result[entry[propertyName]] = entry;
});
@ -1154,9 +1155,9 @@ export class CoreUtilsProvider {
* Sum the filesizes from a list of files checking if the size will be partial or totally calculated.
*
* @param {any[]} files List of files to sum its filesize.
* @return {object} Object with the file size and a boolean to indicate if it is the total size or only partial.
* @return {{size: number, total: boolean}} File size and a boolean to indicate if it is the total size or only partial.
*/
sumFileSizes(files: any[]) : object {
sumFileSizes(files: any[]) : {size: number, total: boolean} {
let result = {
size: 0,
total: true