MOBILE-2908 site: Check for duplicate requests

main
Albert Gasset 2019-03-07 16:42:39 +01:00
parent 4015641ee5
commit f2784cb659
2 changed files with 26 additions and 12 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

@ -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); // There are some ongoing retry calls, wait for timeout.
if (this.retryCalls.length > 0) {
this.logger.warn('Calls locked, trying later...');
if (!promise) { return this.addToRetryQueue(method, siteUrl, data, preSets);
// There are some ongoing retry calls, wait for timeout. } else {
if (this.retryCalls.length > 0) { return this.performPost(method, siteUrl, data, preSets);
this.logger.warn('Calls locked, trying later...');
promise = this.addToRetryQueue(method, siteUrl, data, preSets);
} else {
promise = this.performPost(method, siteUrl, data, preSets);
}
} }
return promise;
} }
/** /**