MOBILE-3127 core: Allow defining a different timeout in wifi/3g
parent
152bbca149
commit
add7792d24
|
@ -1334,7 +1334,7 @@ export class CoreSite {
|
||||||
return Promise.resolve({ code: 0 });
|
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) => {
|
return promise.then((data: any) => {
|
||||||
if (typeof data != 'undefined' && data.errorcode === 'requirecorrectaccess') {
|
if (typeof data != 'undefined' && data.errorcode === 'requirecorrectaccess') {
|
||||||
|
|
|
@ -39,7 +39,8 @@ export class CoreConstants {
|
||||||
static SETTINGS_ANALYTICS_ENABLED = 'CoreSettingsAnalyticsEnabled';
|
static SETTINGS_ANALYTICS_ENABLED = 'CoreSettingsAnalyticsEnabled';
|
||||||
|
|
||||||
// WS constants.
|
// 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_';
|
static WS_PREFIX = 'local_mobile_';
|
||||||
|
|
||||||
// Login constants.
|
// Login constants.
|
||||||
|
|
|
@ -22,6 +22,7 @@ import { CoreSitesFactoryProvider } from './sites-factory';
|
||||||
import { CoreTextUtilsProvider } from './utils/text';
|
import { CoreTextUtilsProvider } from './utils/text';
|
||||||
import { CoreUrlUtilsProvider } from './utils/url';
|
import { CoreUrlUtilsProvider } from './utils/url';
|
||||||
import { CoreUtilsProvider } from './utils/utils';
|
import { CoreUtilsProvider } from './utils/utils';
|
||||||
|
import { CoreWSProvider } from './ws';
|
||||||
import { CoreConstants } from '@core/constants';
|
import { CoreConstants } from '@core/constants';
|
||||||
import { CoreConfigConstants } from '../configconstants';
|
import { CoreConfigConstants } from '../configconstants';
|
||||||
import { CoreSite } from '@classes/site';
|
import { CoreSite } from '@classes/site';
|
||||||
|
@ -326,7 +327,7 @@ export class CoreSitesProvider {
|
||||||
constructor(logger: CoreLoggerProvider, private http: HttpClient, private sitesFactory: CoreSitesFactoryProvider,
|
constructor(logger: CoreLoggerProvider, private http: HttpClient, private sitesFactory: CoreSitesFactoryProvider,
|
||||||
private appProvider: CoreAppProvider, private translate: TranslateService, private urlUtils: CoreUrlUtilsProvider,
|
private appProvider: CoreAppProvider, private translate: TranslateService, private urlUtils: CoreUrlUtilsProvider,
|
||||||
private eventsProvider: CoreEventsProvider, private textUtils: CoreTextUtilsProvider,
|
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.logger = logger.getInstance('CoreSitesProvider');
|
||||||
|
|
||||||
this.appDB = appProvider.getDB();
|
this.appDB = appProvider.getDB();
|
||||||
|
@ -515,7 +516,8 @@ export class CoreSitesProvider {
|
||||||
* @return {Promise} A promise to be resolved if the site exists.
|
* @return {Promise} A promise to be resolved if the site exists.
|
||||||
*/
|
*/
|
||||||
siteExists(siteUrl: string): Promise<void> {
|
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.
|
// Default error messages are kinda bad, return our own message.
|
||||||
return Promise.reject({error: this.translate.instant('core.cannotconnect')});
|
return Promise.reject({error: this.translate.instant('core.cannotconnect')});
|
||||||
}).then((data: any) => {
|
}).then((data: any) => {
|
||||||
|
@ -555,7 +557,7 @@ export class CoreSitesProvider {
|
||||||
service: service
|
service: service
|
||||||
},
|
},
|
||||||
loginUrl = siteUrl + '/login/token.php',
|
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 => {
|
return promise.then((data: any): any => {
|
||||||
if (typeof data == 'undefined') {
|
if (typeof data == 'undefined') {
|
||||||
|
|
|
@ -27,7 +27,6 @@ import { CoreLoggerProvider } from '../logger';
|
||||||
import { TranslateService } from '@ngx-translate/core';
|
import { TranslateService } from '@ngx-translate/core';
|
||||||
import { CoreLangProvider } from '../lang';
|
import { CoreLangProvider } from '../lang';
|
||||||
import { CoreWSProvider, CoreWSError } from '../ws';
|
import { CoreWSProvider, CoreWSError } from '../ws';
|
||||||
import { CoreConstants } from '@core/constants';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deferred promise. It's similar to the result of $q.defer() in AngularJS.
|
* Deferred promise. It's similar to the result of $q.defer() in AngularJS.
|
||||||
|
@ -232,7 +231,8 @@ export class CoreUtilsProvider {
|
||||||
initOptions.signal = controller.signal;
|
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;
|
return response.redirected;
|
||||||
}).catch((error) => {
|
}).catch((error) => {
|
||||||
if (error.timeout && controller) {
|
if (error.timeout && controller) {
|
||||||
|
|
|
@ -253,9 +253,9 @@ export class CoreWSProvider {
|
||||||
if (preSets.noLogin && preSets.useGet) {
|
if (preSets.noLogin && preSets.useGet) {
|
||||||
// Send params using GET.
|
// Send params using GET.
|
||||||
siteUrl += '&args=' + encodeURIComponent(ajaxData);
|
siteUrl += '&args=' + encodeURIComponent(ajaxData);
|
||||||
promise = this.http.get(siteUrl).timeout(CoreConstants.WS_TIMEOUT).toPromise();
|
promise = this.http.get(siteUrl).timeout(this.getRequestTimeout()).toPromise();
|
||||||
} else {
|
} 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) => {
|
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.
|
* 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);
|
let promise = this.getPromiseHttp('head', url);
|
||||||
|
|
||||||
if (!promise) {
|
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);
|
promise = this.setPromiseHttp(promise, 'head', url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -573,7 +582,7 @@ export class CoreWSProvider {
|
||||||
const requestUrl = siteUrl + '&wsfunction=' + method;
|
const requestUrl = siteUrl + '&wsfunction=' + method;
|
||||||
|
|
||||||
// Perform the post request.
|
// 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) => {
|
return promise.then((data: any) => {
|
||||||
// Some moodle web services return null.
|
// Some moodle web services return null.
|
||||||
|
@ -693,7 +702,7 @@ export class CoreWSProvider {
|
||||||
// HTTP not finished, but we should delete the promise after timeout.
|
// HTTP not finished, but we should delete the promise after timeout.
|
||||||
timeout = setTimeout(() => {
|
timeout = setTimeout(() => {
|
||||||
delete this.ongoingCalls[queueItemId];
|
delete this.ongoingCalls[queueItemId];
|
||||||
}, CoreConstants.WS_TIMEOUT);
|
}, this.getRequestTimeout());
|
||||||
|
|
||||||
// HTTP finished, delete from ongoing.
|
// HTTP finished, delete from ongoing.
|
||||||
return promise.finally(() => {
|
return promise.finally(() => {
|
||||||
|
|
Loading…
Reference in New Issue