Merge pull request #1798 from albertgasset/MOBILE-2908

Mobile 2908
main
Juan Leyva 2019-03-11 13:21:18 +01:00 committed by GitHub
commit eaa1399087
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 13 deletions

View File

@ -185,6 +185,7 @@ export class CoreSite {
protected cleanUnicode = false; protected cleanUnicode = false;
protected lastAutoLogin = 0; protected lastAutoLogin = 0;
protected offlineDisabled = false; protected offlineDisabled = false;
protected ongoingRequests: { [cacheId: string]: Promise<any> } = {};
/** /**
* Create a site. * Create a site.
@ -571,7 +572,14 @@ export class CoreSite {
return Promise.reject(this.utils.createFakeWSError('core.unicodenotsupportedcleanerror', true)); return Promise.reject(this.utils.createFakeWSError('core.unicodenotsupportedcleanerror', true));
} }
return this.getFromCache(method, data, preSets, false, originalData).catch(() => { const cacheId = this.getCacheId(method, data);
// Check for an ongoing identical request if we're not ignoring cache.
if (preSets.getFromCache && this.ongoingRequests[cacheId]) {
return this.ongoingRequests[cacheId];
}
const promise = this.getFromCache(method, data, preSets, false, originalData).catch(() => {
// Do not pass those options to the core WS factory. // Do not pass those options to the core WS factory.
return this.wsProvider.call(method, data, wsPreSets).then((response) => { return this.wsProvider.call(method, data, wsPreSets).then((response) => {
if (preSets.saveToCache) { if (preSets.saveToCache) {
@ -676,6 +684,17 @@ export class CoreSite {
return response; return response;
}); });
this.ongoingRequests[cacheId] = promise;
// Clear ongoing request after setting the promise (just in case it's already resolved).
promise.finally(() => {
// Make sure we don't clear the promise of a newer request that ignores the cache.
if (this.ongoingRequests[cacheId] === promise) {
delete this.ongoingRequests[cacheId];
}
});
return promise;
} }
/** /**

View File

@ -28,7 +28,7 @@ export class CoreCourseLogHelperProvider {
// Variables for database. // Variables for database.
static ACTIVITY_LOG_TABLE = 'course_activity_log'; static ACTIVITY_LOG_TABLE = 'course_activity_log';
protected siteSchema: CoreSiteSchema = { protected siteSchema: CoreSiteSchema = {
name: 'CoreCourseOfflineProvider', name: 'CoreCourseLogHelperProvider',
version: 1, version: 1,
tables: [ tables: [
{ {

View File

@ -193,19 +193,14 @@ export class CoreWSProvider {
data.wstoken = preSets.wsToken; data.wstoken = preSets.wsToken;
siteUrl = preSets.siteUrl + '/webservice/rest/server.php?moodlewsrestformat=json'; siteUrl = preSets.siteUrl + '/webservice/rest/server.php?moodlewsrestformat=json';
let promise = this.getPromiseHttp('post', preSets.siteUrl, data);
if (!promise) {
// There are some ongoing retry calls, wait for timeout. // There are some ongoing retry calls, wait for timeout.
if (this.retryCalls.length > 0) { if (this.retryCalls.length > 0) {
this.logger.warn('Calls locked, trying later...'); this.logger.warn('Calls locked, trying later...');
promise = this.addToRetryQueue(method, siteUrl, data, preSets);
} else {
promise = this.performPost(method, siteUrl, data, preSets);
}
}
return promise; return this.addToRetryQueue(method, siteUrl, data, preSets);
} else {
return this.performPost(method, siteUrl, data, preSets);
}
} }
/** /**