snappymail/dev/Promises/AbstractAjax.js

194 lines
5.2 KiB
JavaScript
Raw Normal View History

import window from 'window';
import $ from '$';
2019-07-05 03:19:24 +08:00
import { ajax } from 'Common/Links';
import { microtime, isUnd, isNormal, pString, pInt } from 'Common/Utils';
2019-07-05 03:19:24 +08:00
import { DEFAULT_AJAX_TIMEOUT, TOKEN_ERROR_LIMIT, AJAX_ERROR_LIMIT } from 'Common/Consts';
import { StorageResultType, Notification } from 'Common/Enums';
import { data as GlobalsData } from 'Common/Globals';
import * as Plugins from 'Common/Plugins';
import * as Settings from 'Storage/Settings';
2019-07-05 03:19:24 +08:00
import { AbstractBasicPromises } from 'Promises/AbstractBasic';
2019-07-05 03:19:24 +08:00
class AbstractAjaxPromises extends AbstractBasicPromises {
2016-09-10 06:38:16 +08:00
oRequests = {};
constructor() {
super();
2016-09-10 06:38:16 +08:00
this.clear();
}
clear() {
this.oRequests = {};
}
abort(sAction, bClearOnly) {
2019-07-05 03:19:24 +08:00
if (this.oRequests[sAction]) {
if (!bClearOnly && this.oRequests[sAction].abort) {
this.oRequests[sAction].__aborted__ = true;
this.oRequests[sAction].abort();
}
this.oRequests[sAction] = null;
delete this.oRequests[sAction];
}
return this;
2016-06-30 08:02:45 +08:00
}
ajaxRequest(action, isPost, timeOut, params, additionalGetString, fTrigger) {
return new window.Promise((resolve, reject) => {
const start = microtime();
timeOut = isNormal(timeOut) ? timeOut : DEFAULT_AJAX_TIMEOUT;
additionalGetString = isUnd(additionalGetString) ? '' : pString(additionalGetString);
2019-07-05 03:19:24 +08:00
if (isPost) {
params.XToken = Settings.appSettingsGet('token');
}
2015-04-29 04:51:20 +08:00
Plugins.runHook('ajax-default-request', [action, params, additionalGetString]);
2015-04-29 04:51:20 +08:00
this.setTrigger(fTrigger, true);
2016-05-24 01:33:01 +08:00
const oH = $.ajax({
type: isPost ? 'POST' : 'GET',
url: ajax(additionalGetString),
async: true,
dataType: 'json',
2019-07-05 03:19:24 +08:00
data: isPost ? params || {} : {},
timeout: timeOut,
global: true
}).always((data, textStatus) => {
2019-07-05 03:19:24 +08:00
let isCached = false,
errorData = null;
2016-05-24 01:33:01 +08:00
2019-07-05 03:19:24 +08:00
if (data && data.Time) {
isCached = pInt(data.Time) > microtime() - start;
}
2016-05-24 01:33:01 +08:00
2020-01-01 01:44:41 +08:00
if (data && data.UpdateToken) {
if (GlobalsData.__APP__ && GlobalsData.__APP__.setClientSideToken) {
GlobalsData.__APP__.setClientSideToken(data.UpdateToken);
}
}
// backward capability
let type = '';
2019-07-05 03:19:24 +08:00
switch (true) {
case 'success' === textStatus && data && data.Result && action === data.Action:
type = StorageResultType.Success;
break;
case 'abort' === textStatus && (!data || !data.__aborted__):
type = StorageResultType.Abort;
break;
default:
type = StorageResultType.Error;
break;
}
2016-06-30 08:02:45 +08:00
Plugins.runHook('ajax-default-response', [
2019-07-05 03:19:24 +08:00
action,
StorageResultType.Success === type ? data : null,
type,
isCached,
params
]);
2016-05-24 01:33:01 +08:00
2019-07-05 03:19:24 +08:00
if ('success' === textStatus) {
if (data && data.Result && action === data.Action) {
data.__cached__ = isCached;
resolve(data);
2019-07-05 03:19:24 +08:00
} else if (data && data.Action) {
errorData = data;
reject(data.ErrorCode ? data.ErrorCode : Notification.AjaxFalse);
2019-07-05 03:19:24 +08:00
} else {
errorData = data;
reject(Notification.AjaxParse);
}
2019-07-05 03:19:24 +08:00
} else if ('timeout' === textStatus) {
errorData = data;
reject(Notification.AjaxTimeout);
2019-07-05 03:19:24 +08:00
} else if ('abort' === textStatus) {
if (!data || !data.__aborted__) {
reject(Notification.AjaxAbort);
}
2019-07-05 03:19:24 +08:00
} else {
errorData = data;
reject(Notification.AjaxParse);
2015-05-06 00:41:15 +08:00
}
2019-07-05 03:19:24 +08:00
if (this.oRequests[action]) {
this.oRequests[action] = null;
delete this.oRequests[action];
2016-06-30 08:02:45 +08:00
}
2015-05-06 00:41:15 +08:00
this.setTrigger(fTrigger, false);
2016-06-30 08:02:45 +08:00
2019-07-05 03:19:24 +08:00
if (errorData) {
if ([
2019-07-05 03:19:24 +08:00
Notification.AuthError,
Notification.AccessError,
Notification.ConnectionError,
Notification.DomainNotAllowed,
Notification.AccountNotAllowed,
Notification.MailServerError,
Notification.UnknownNotification,
Notification.UnknownError
].includes(errorData.ErrorCode)
2019-07-05 03:19:24 +08:00
) {
GlobalsData.iAjaxErrorCount += 1;
2015-05-06 00:41:15 +08:00
}
2019-07-05 03:19:24 +08:00
if (Notification.InvalidToken === errorData.ErrorCode) {
GlobalsData.iTokenErrorCount += 1;
}
2019-07-05 03:19:24 +08:00
if (TOKEN_ERROR_LIMIT < GlobalsData.iTokenErrorCount) {
if (GlobalsData.__APP__ && GlobalsData.__APP__.loginAndLogoutReload) {
GlobalsData.__APP__.loginAndLogoutReload(false, true);
}
2016-05-24 01:33:01 +08:00
}
2019-07-05 03:19:24 +08:00
if (errorData.ClearAuth || errorData.Logout || AJAX_ERROR_LIMIT < GlobalsData.iAjaxErrorCount) {
if (GlobalsData.__APP__ && GlobalsData.__APP__.clearClientSideToken) {
GlobalsData.__APP__.clearClientSideToken();
}
2019-07-05 03:19:24 +08:00
if (GlobalsData.__APP__ && !errorData.ClearAuth && GlobalsData.__APP__.loginAndLogoutReload) {
GlobalsData.__APP__.loginAndLogoutReload(false, true);
}
2015-05-06 00:41:15 +08:00
}
}
});
2019-07-05 03:19:24 +08:00
if (oH) {
if (this.oRequests[action]) {
this.oRequests[action] = null;
delete this.oRequests[action];
}
this.oRequests[action] = oH;
2016-05-24 01:33:01 +08:00
}
});
}
getRequest(sAction, fTrigger, sAdditionalGetString, iTimeOut) {
sAdditionalGetString = isUnd(sAdditionalGetString) ? '' : pString(sAdditionalGetString);
sAdditionalGetString = sAction + '/' + sAdditionalGetString;
return this.ajaxRequest(sAction, false, iTimeOut, null, sAdditionalGetString, fTrigger);
}
postRequest(action, fTrigger, params, timeOut) {
params = params || {};
params.Action = action;
return this.ajaxRequest(action, true, timeOut, params, '', fTrigger);
}
}
2019-07-05 03:19:24 +08:00
export { AbstractAjaxPromises, AbstractAjaxPromises as default };