MOBILE-3608 block: Add CoreCache and some types

main
Pau Ferrer Ocaña 2020-12-11 15:40:34 +01:00
parent 253d6829a9
commit 361edb1252
8 changed files with 182 additions and 46 deletions

View File

@ -0,0 +1,112 @@
// (C) Copyright 2015 Moodle Pty Ltd.
//
// 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: {
[key: string]: CoreCacheEntry;
} = {};
/**
* Clear the cache.
*/
clear(): void {
this.cacheStore = {};
}
/**
* Get all the data stored in the cache for a certain id.
*
* @param id The ID to identify the entry.
* @return The data from the cache. Undefined if not found.
*/
getEntry(id: string): CoreCacheEntry {
if (!this.cacheStore[id]) {
this.cacheStore[id] = {};
}
return this.cacheStore[id];
}
/**
* Get the status of a module from the "cache".
*
* @param id The ID to identify the entry.
* @param name Name of the value to get.
* @param ignoreInvalidate Whether it should always return the cached data, even if it's expired.
* @return Cached value. Undefined if not cached or expired.
*/
getValue<T = unknown>(id: string, name: string, ignoreInvalidate = false): T | undefined {
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 id The ID to identify the entry.
*/
invalidate(id: string): void {
const entry = this.getEntry(id);
for (const name in entry) {
entry[name].timemodified = 0;
}
}
/**
* Update the status of a module in the "cache".
*
* @param id The ID to identify the entry.
* @param name Name of the value to set.
* @param value Value to set.
* @return The set value.
*/
setValue<T>(id: string, name: string, value: T): T {
const entry = this.getEntry(id);
entry[name] = {
value: value,
timemodified: Date.now(),
};
return value;
}
}
/**
* Cache entry
*/
export type CoreCacheEntry = {
[name: string]: {
value?: any; // eslint-disable-line @typescript-eslint/no-explicit-any
timemodified: number;
};
};

View File

@ -345,7 +345,7 @@ export class CoreCourseProvider {
ignoreCache: boolean = false, ignoreCache: boolean = false,
siteId?: string, siteId?: string,
modName?: string, modName?: string,
): Promise<CoreCourseModule> { ): Promise<CoreCourseModuleData> {
siteId = siteId || CoreSites.instance.getCurrentSiteId(); siteId = siteId || CoreSites.instance.getCurrentSiteId();
// Helper function to do the WS request without processing the result. // Helper function to do the WS request without processing the result.
@ -439,7 +439,7 @@ export class CoreCourseProvider {
sections = await this.getSections(courseId, false, false, preSets, siteId); sections = await this.getSections(courseId, false, false, preSets, siteId);
} }
let foundModule: CoreCourseModule | undefined; let foundModule: CoreCourseModuleData | undefined;
const foundSection = sections.some((section) => { const foundSection = sections.some((section) => {
if (sectionId != null && if (sectionId != null &&
@ -739,12 +739,12 @@ export class CoreCourseProvider {
* @param sections Sections. * @param sections Sections.
* @return Modules. * @return Modules.
*/ */
getSectionsModules(sections: CoreCourseSection[]): CoreCourseModule[] { getSectionsModules(sections: CoreCourseSection[]): CoreCourseModuleData[] {
if (!sections || !sections.length) { if (!sections || !sections.length) {
return []; return [];
} }
return sections.reduce((previous: CoreCourseModule[], section) => previous.concat(section.modules || []), []); return sections.reduce((previous: CoreCourseModuleData[], section) => previous.concat(section.modules || []), []);
} }
/** /**
@ -829,7 +829,7 @@ export class CoreCourseProvider {
* @return Promise resolved when loaded. * @return Promise resolved when loaded.
*/ */
async loadModuleContents( async loadModuleContents(
module: CoreCourseModule & CoreCourseModuleBasicInfo, module: CoreCourseModuleData & CoreCourseModuleBasicInfo,
courseId?: number, courseId?: number,
sectionId?: number, sectionId?: number,
preferCache?: boolean, preferCache?: boolean,
@ -964,7 +964,7 @@ export class CoreCourseProvider {
* @param module The module object. * @param module The module object.
* @return Whether the module has a view page. * @return Whether the module has a view page.
*/ */
moduleHasView(module: CoreCourseModuleSummary | CoreCourseModule): boolean { moduleHasView(module: CoreCourseModuleSummary | CoreCourseModuleData): boolean {
return !!module.url; return !!module.url;
} }
@ -1345,7 +1345,7 @@ export type CoreCourseSection = {
hiddenbynumsections?: number; // Whether is a section hidden in the course format. hiddenbynumsections?: number; // Whether is a section hidden in the course format.
uservisible?: boolean; // Is the section visible for the user?. uservisible?: boolean; // Is the section visible for the user?.
availabilityinfo?: string; // Availability information. availabilityinfo?: string; // Availability information.
modules: CoreCourseModule[]; modules: CoreCourseModuleData[];
}; };
/** /**
@ -1371,11 +1371,10 @@ export type CoreCourseGetCourseModuleWSResponse = {
warnings?: CoreStatusWithWarningsWSResponse[]; warnings?: CoreStatusWithWarningsWSResponse[];
}; };
/** /**
* Course module type. * Course module type.
*/ */
export type CoreCourseModule = { // List of module. export type CoreCourseModuleData = { // List of module.
id: number; // Activity id. id: number; // Activity id.
course?: number; // The course id. course?: number; // The course id.
url?: string; // Activity url. url?: string; // Activity url.
@ -1403,12 +1402,23 @@ export type CoreCourseModule = { // List of module.
overrideby: number; // The user id who has overriden the status. overrideby: number; // The user id who has overriden the status.
valueused?: boolean; // Whether the completion status affects the availability of another activity. valueused?: boolean; // Whether the completion status affects the availability of another activity.
}; };
contents: { contents: CoreCourseModuleContentFile[];
contentsinfo?: { // Contents summary information.
filescount: number; // Total number of files.
filessize: number; // Total files size.
lastmodified: number; // Last time files were modified.
mimetypes: string[]; // Files mime types.
repositorytype?: string; // The repository type for the main file.
};
};
export type CoreCourseModuleContentFile = {
type: string; // A file or a folder or external link. type: string; // A file or a folder or external link.
filename: string; // Filename. filename: string; // Filename.
filepath: string; // Filepath. filepath: string; // Filepath.
filesize: number; // Filesize. filesize: number; // Filesize.
fileurl?: string; // Downloadable file url. fileurl?: string; // Downloadable file url.
url?: string; // @deprecated. Use fileurl instead.
content?: string; // Raw content, will be used when type is content. content?: string; // Raw content, will be used when type is content.
timecreated: number; // Time created. timecreated: number; // Time created.
timemodified: number; // Time modified. timemodified: number; // Time modified.
@ -1431,14 +1441,6 @@ export type CoreCourseModule = { // List of module.
ordering: number; // Tag ordering. ordering: number; // Tag ordering.
flag: number; // Whether the tag is flagged as inappropriate. flag: number; // Whether the tag is flagged as inappropriate.
}[]; }[];
}[];
contentsinfo?: { // Contents summary information.
filescount: number; // Total number of files.
filessize: number; // Total files size.
lastmodified: number; // Last time files were modified.
mimetypes: string[]; // Files mime types.
repositorytype?: string; // The repository type for the main file.
};
}; };
/** /**

View File

@ -16,7 +16,7 @@ import { Injectable } from '@angular/core';
import { Md5 } from 'ts-md5/dist/md5'; import { Md5 } from 'ts-md5/dist/md5';
import { CoreApp } from '@services/app'; import { CoreApp } from '@services/app';
import { CoreEvents } from '@singletons/events'; import { CoreEventPackageStatusChanged, CoreEvents } from '@singletons/events';
import { CoreFile } from '@services/file'; import { CoreFile } from '@services/file';
import { CorePluginFileDelegate } from '@services/plugin-file-delegate'; import { CorePluginFileDelegate } from '@services/plugin-file-delegate';
import { CoreSites } from '@services/sites'; import { CoreSites } from '@services/sites';
@ -544,7 +544,7 @@ export class CoreFilepoolProvider {
entries.forEach((entry) => { entries.forEach((entry) => {
// Trigger module status changed, setting it as not downloaded. // Trigger module status changed, setting it as not downloaded.
this.triggerPackageStatusChanged(siteId, CoreConstants.NOT_DOWNLOADED, entry.component, entry.componentId); this.triggerPackageStatusChanged(siteId, CoreConstants.NOT_DOWNLOADED, entry.component!, entry.componentId);
}); });
} }
@ -2949,8 +2949,8 @@ export class CoreFilepoolProvider {
* @param component Package's component. * @param component Package's component.
* @param componentId An ID to use in conjunction with the component. * @param componentId An ID to use in conjunction with the component.
*/ */
protected triggerPackageStatusChanged(siteId: string, status: string, component?: string, componentId?: string | number): void { protected triggerPackageStatusChanged(siteId: string, status: string, component: string, componentId?: string | number): void {
const data = { const data: CoreEventPackageStatusChanged = {
component, component,
componentId: this.fixComponentId(componentId), componentId: this.fixComponentId(componentId),
status, status,

View File

@ -133,7 +133,7 @@ export class CorePluginFileDelegateService extends CoreDelegate<CorePluginFileHa
* @param siteId Site ID. If not defined, current site. * @param siteId Site ID. If not defined, current site.
* @return Promise resolved with file size and a boolean to indicate if it is the total size or only partial. * @return Promise resolved with file size and a boolean to indicate if it is the total size or only partial.
*/ */
async getFilesDownloadSize(files: CoreWSExternalFile[], siteId: string): Promise<{ size: number; total: boolean }> { async getFilesDownloadSize(files: CoreWSExternalFile[], siteId: string): Promise<CoreFileSizeSum> {
const filteredFiles = <CoreWSExternalFile[]>[]; const filteredFiles = <CoreWSExternalFile[]>[];
await Promise.all(files.map(async (file) => { await Promise.all(files.map(async (file) => {
@ -154,7 +154,7 @@ export class CorePluginFileDelegateService extends CoreDelegate<CorePluginFileHa
* @param siteId Site ID. If not defined, current site. * @param siteId Site ID. If not defined, current site.
* @return Promise resolved with file size and a boolean to indicate if it is the total size or only partial. * @return Promise resolved with file size and a boolean to indicate if it is the total size or only partial.
*/ */
async getFilesSize(files: CoreWSExternalFile[], siteId?: string): Promise<{ size: number; total: boolean }> { async getFilesSize(files: CoreWSExternalFile[], siteId?: string): Promise<CoreFileSizeSum> {
const result = { const result = {
size: 0, size: 0,
total: true, total: true,
@ -402,3 +402,11 @@ export type CorePluginFileDownloadableResult = {
*/ */
reason?: string; reason?: string;
}; };
/**
* Sum of file sizes.
*/
export type CoreFileSizeSum = {
size: number; // Sum of file sizes.
total: boolean; // False if any file size is not avalaible.
};

View File

@ -304,6 +304,9 @@ export class CoreSitesProvider {
async siteExists(siteUrl: string): Promise<void> { async siteExists(siteUrl: string): Promise<void> {
let data: CoreSitesLoginTokenResponse; let data: CoreSitesLoginTokenResponse;
// Use a valid path first.
siteUrl = CoreUrlUtils.instance.removeUrlParams(siteUrl);
try { try {
data = await Http.instance.post(siteUrl + '/login/token.php', {}).pipe(timeout(CoreWS.instance.getRequestTimeout())) data = await Http.instance.post(siteUrl + '/login/token.php', {}).pipe(timeout(CoreWS.instance.getRequestTimeout()))
.toPromise(); .toPromise();

View File

@ -34,6 +34,7 @@ import { CoreSilentError } from '@classes/errors/silenterror';
import { makeSingleton, Translate, AlertController, LoadingController, ToastController } from '@singletons'; import { makeSingleton, Translate, AlertController, LoadingController, ToastController } from '@singletons';
import { CoreLogger } from '@singletons/logger'; import { CoreLogger } from '@singletons/logger';
import { CoreFileSizeSum } from '@services/plugin-file-delegate';
/* /*
* "Utils" service with helper functions for UI, DOM elements and HTML code. * "Utils" service with helper functions for UI, DOM elements and HTML code.
@ -134,7 +135,7 @@ export class CoreDomUtilsProvider {
* @return Promise resolved when the user confirms or if no confirm needed. * @return Promise resolved when the user confirms or if no confirm needed.
*/ */
async confirmDownloadSize( async confirmDownloadSize(
size: {size: number; total: boolean}, size: CoreFileSizeSum,
message?: string, message?: string,
unknownMessage?: string, unknownMessage?: string,
wifiThreshold?: number, wifiThreshold?: number,

View File

@ -28,6 +28,7 @@ import { CoreTextUtils } from '@services/utils/text';
import { CoreWSError } from '@classes/errors/wserror'; import { CoreWSError } from '@classes/errors/wserror';
import { makeSingleton, Clipboard, InAppBrowser, FileOpener, WebIntent, QRScanner, Translate } from '@singletons'; import { makeSingleton, Clipboard, InAppBrowser, FileOpener, WebIntent, QRScanner, Translate } from '@singletons';
import { CoreLogger } from '@singletons/logger'; import { CoreLogger } from '@singletons/logger';
import { CoreFileSizeSum } from '@services/plugin-file-delegate';
type TreeNode<T> = T & { children: TreeNode<T>[] }; type TreeNode<T> = T & { children: TreeNode<T>[] };
@ -1327,7 +1328,7 @@ export class CoreUtilsProvider {
* @return File size and a boolean to indicate if it is the total size or only partial. * @return File size and a boolean to indicate if it is the total size or only partial.
* @deprecated since 3.8.0. Use CorePluginFileDelegate.getFilesSize instead. * @deprecated since 3.8.0. Use CorePluginFileDelegate.getFilesSize instead.
*/ */
sumFileSizes(files: CoreWSExternalFile[]): { size: number; total: boolean } { sumFileSizes(files: CoreWSExternalFile[]): CoreFileSizeSum {
const result = { const result = {
size: 0, size: 0,
total: true, total: true,

View File

@ -242,6 +242,15 @@ export type CoreEventCourseStatusChanged = {
status: string; status: string;
}; };
/**
* Data passed to PACKAGE_STATUS_CHANGED event.
*/
export type CoreEventPackageStatusChanged = {
component: string;
componentId: string | number;
status: string;
};
/** /**
* Data passed to USER_DELETED event. * Data passed to USER_DELETED event.
*/ */