MOBILE-3127 core: Allow defining a different timeout in wifi/3g

main
Dani Palou 2019-08-22 11:22:47 +02:00
parent 152bbca149
commit add7792d24
5 changed files with 24 additions and 12 deletions

View File

@ -1334,7 +1334,7 @@ export class CoreSite {
return Promise.resolve({ code: 0 });
}
const promise = this.http.post(checkUrl, { service: service }).timeout(CoreConstants.WS_TIMEOUT).toPromise();
const promise = this.http.post(checkUrl, { service: service }).timeout(this.wsProvider.getRequestTimeout()).toPromise();
return promise.then((data: any) => {
if (typeof data != 'undefined' && data.errorcode === 'requirecorrectaccess') {

View File

@ -39,7 +39,8 @@ export class CoreConstants {
static SETTINGS_ANALYTICS_ENABLED = 'CoreSettingsAnalyticsEnabled';
// WS constants.
static WS_TIMEOUT = 30000;
static WS_TIMEOUT = 30000; // Timeout when not in WiFi.
static WS_TIMEOUT_WIFI = 30000; // Timeout when in WiFi.
static WS_PREFIX = 'local_mobile_';
// Login constants.

View File

@ -22,6 +22,7 @@ import { CoreSitesFactoryProvider } from './sites-factory';
import { CoreTextUtilsProvider } from './utils/text';
import { CoreUrlUtilsProvider } from './utils/url';
import { CoreUtilsProvider } from './utils/utils';
import { CoreWSProvider } from './ws';
import { CoreConstants } from '@core/constants';
import { CoreConfigConstants } from '../configconstants';
import { CoreSite } from '@classes/site';
@ -326,7 +327,7 @@ export class CoreSitesProvider {
constructor(logger: CoreLoggerProvider, private http: HttpClient, private sitesFactory: CoreSitesFactoryProvider,
private appProvider: CoreAppProvider, private translate: TranslateService, private urlUtils: CoreUrlUtilsProvider,
private eventsProvider: CoreEventsProvider, private textUtils: CoreTextUtilsProvider,
private utils: CoreUtilsProvider, private injector: Injector) {
private utils: CoreUtilsProvider, private injector: Injector, private wsProvider: CoreWSProvider) {
this.logger = logger.getInstance('CoreSitesProvider');
this.appDB = appProvider.getDB();
@ -515,7 +516,8 @@ export class CoreSitesProvider {
* @return {Promise} A promise to be resolved if the site exists.
*/
siteExists(siteUrl: string): Promise<void> {
return this.http.post(siteUrl + '/login/token.php', {}).timeout(CoreConstants.WS_TIMEOUT).toPromise().catch(() => {
return this.http.post(siteUrl + '/login/token.php', {}).timeout(this.wsProvider.getRequestTimeout()).toPromise()
.catch(() => {
// Default error messages are kinda bad, return our own message.
return Promise.reject({error: this.translate.instant('core.cannotconnect')});
}).then((data: any) => {
@ -555,7 +557,7 @@ export class CoreSitesProvider {
service: service
},
loginUrl = siteUrl + '/login/token.php',
promise = this.http.post(loginUrl, params).timeout(CoreConstants.WS_TIMEOUT).toPromise();
promise = this.http.post(loginUrl, params).timeout(this.wsProvider.getRequestTimeout()).toPromise();
return promise.then((data: any): any => {
if (typeof data == 'undefined') {

View File

@ -27,7 +27,6 @@ import { CoreLoggerProvider } from '../logger';
import { TranslateService } from '@ngx-translate/core';
import { CoreLangProvider } from '../lang';
import { CoreWSProvider, CoreWSError } from '../ws';
import { CoreConstants } from '@core/constants';
/**
* Deferred promise. It's similar to the result of $q.defer() in AngularJS.
@ -232,7 +231,8 @@ export class CoreUtilsProvider {
initOptions.signal = controller.signal;
}
return this.timeoutPromise(window.fetch(url, initOptions), CoreConstants.WS_TIMEOUT).then((response: Response) => {
return this.timeoutPromise(window.fetch(url, initOptions), this.wsProvider.getRequestTimeout())
.then((response: Response) => {
return response.redirected;
}).catch((error) => {
if (error.timeout && controller) {

View File

@ -253,9 +253,9 @@ export class CoreWSProvider {
if (preSets.noLogin && preSets.useGet) {
// Send params using GET.
siteUrl += '&args=' + encodeURIComponent(ajaxData);
promise = this.http.get(siteUrl).timeout(CoreConstants.WS_TIMEOUT).toPromise();
promise = this.http.get(siteUrl).timeout(this.getRequestTimeout()).toPromise();
} else {
promise = this.http.post(siteUrl, ajaxData).timeout(CoreConstants.WS_TIMEOUT).toPromise();
promise = this.http.post(siteUrl, ajaxData).timeout(this.getRequestTimeout()).toPromise();
}
return promise.then((data: any) => {
@ -516,6 +516,15 @@ export class CoreWSProvider {
});
}
/**
* Get a request timeout based on the network connection.
*
* @return {number} Timeout in ms.
*/
getRequestTimeout(): number {
return this.appProvider.isNetworkAccessLimited() ? CoreConstants.WS_TIMEOUT : CoreConstants.WS_TIMEOUT_WIFI;
}
/**
* Get the unique queue item id of the cache for a HTTP request.
*
@ -542,7 +551,7 @@ export class CoreWSProvider {
let promise = this.getPromiseHttp('head', url);
if (!promise) {
promise = this.commonHttp.head(url).timeout(CoreConstants.WS_TIMEOUT).toPromise();
promise = this.commonHttp.head(url).timeout(this.getRequestTimeout()).toPromise();
promise = this.setPromiseHttp(promise, 'head', url);
}
@ -573,7 +582,7 @@ export class CoreWSProvider {
const requestUrl = siteUrl + '&wsfunction=' + method;
// Perform the post request.
const promise = this.http.post(requestUrl, ajaxData, options).timeout(CoreConstants.WS_TIMEOUT).toPromise();
const promise = this.http.post(requestUrl, ajaxData, options).timeout(this.getRequestTimeout()).toPromise();
return promise.then((data: any) => {
// Some moodle web services return null.
@ -693,7 +702,7 @@ export class CoreWSProvider {
// HTTP not finished, but we should delete the promise after timeout.
timeout = setTimeout(() => {
delete this.ongoingCalls[queueItemId];
}, CoreConstants.WS_TIMEOUT);
}, this.getRequestTimeout());
// HTTP finished, delete from ongoing.
return promise.finally(() => {