mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-08-31 21:19:21 +03:00
Promises (first look)
Thread dropdown Small fixes
This commit is contained in:
parent
dd41d30f9b
commit
c23ba31e17
61 changed files with 12442 additions and 130 deletions
172
dev/Promises/AbstractAjax.js
Normal file
172
dev/Promises/AbstractAjax.js
Normal file
|
|
@ -0,0 +1,172 @@
|
|||
|
||||
(function () {
|
||||
|
||||
'use strict';
|
||||
|
||||
var
|
||||
_ = require('_'),
|
||||
$ = require('$'),
|
||||
Q = require('Q'),
|
||||
|
||||
Consts = require('Common/Consts'),
|
||||
Enums = require('Common/Enums'),
|
||||
Utils = require('Common/Utils'),
|
||||
Links = require('Common/Links'),
|
||||
|
||||
Settings = require('Storage/Settings')
|
||||
;
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
*/
|
||||
function AbstractAjaxPromises()
|
||||
{
|
||||
this.oRequests = {};
|
||||
}
|
||||
|
||||
AbstractAjaxPromises.prototype.func = function (fFunc)
|
||||
{
|
||||
fFunc();
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
AbstractAjaxPromises.prototype.fastPromise = function (mData)
|
||||
{
|
||||
var oDeferred = Q.defer();
|
||||
oDeferred.resolve(mData);
|
||||
return oDeferred.promise;
|
||||
};
|
||||
|
||||
AbstractAjaxPromises.prototype.abort = function (sAction, bClearOnly)
|
||||
{
|
||||
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;
|
||||
};
|
||||
|
||||
AbstractAjaxPromises.prototype.ajaxRequest = function (sAction, bPost, iTimeOut, oParameters, sAdditionalGetString, fTrigger)
|
||||
{
|
||||
var
|
||||
oH = null,
|
||||
self = this,
|
||||
iStart = Utils.microtime(),
|
||||
oDeferred = Q.defer()
|
||||
;
|
||||
|
||||
iTimeOut = Utils.isNormal(iTimeOut) ? iTimeOut : Consts.Defaults.DefaultAjaxTimeout;
|
||||
sAdditionalGetString = Utils.isUnd(sAdditionalGetString) ? '' : Utils.pString(sAdditionalGetString);
|
||||
|
||||
if (bPost)
|
||||
{
|
||||
oParameters['XToken'] = Settings.settingsGet('Token');
|
||||
}
|
||||
|
||||
if (fTrigger)
|
||||
{
|
||||
fTrigger(true);
|
||||
}
|
||||
|
||||
oH = $.ajax({
|
||||
'type': bPost ? 'POST' : 'GET',
|
||||
'url': Links.ajax(sAdditionalGetString),
|
||||
'async': true,
|
||||
'dataType': 'json',
|
||||
'data': bPost ? (oParameters || {}) : {},
|
||||
'timeout': iTimeOut,
|
||||
'global': true
|
||||
}).always(function (oData, sTextStatus) {
|
||||
|
||||
var bCached = false;
|
||||
if (oData && oData['Time'])
|
||||
{
|
||||
bCached = Utils.pInt(oData['Time']) > Utils.microtime() - iStart;
|
||||
}
|
||||
|
||||
if ('success' === sTextStatus)
|
||||
{
|
||||
if (oData && oData.Result && sAction === oData.Action)
|
||||
{
|
||||
oData.Result.__cached__ = bCached;
|
||||
oDeferred.resolve(oData.Result);
|
||||
}
|
||||
else if (oData && oData.Action)
|
||||
{
|
||||
oDeferred.reject(oData.ErrorCode ? oData.ErrorCode : Enums.Notification.AjaxFalse);
|
||||
}
|
||||
else
|
||||
{
|
||||
oDeferred.reject(Enums.Notification.AjaxParse);
|
||||
}
|
||||
}
|
||||
else if ('timeout' === sTextStatus)
|
||||
{
|
||||
oDeferred.reject(Enums.Notification.AjaxTimeout);
|
||||
}
|
||||
else if ('abort' === sTextStatus)
|
||||
{
|
||||
if (!oData || !oData.__aborted)
|
||||
{
|
||||
oDeferred.reject(Enums.Notification.AjaxAbort);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
oDeferred.reject(Enums.Notification.AjaxParse);
|
||||
}
|
||||
|
||||
if (self.oRequests[sAction])
|
||||
{
|
||||
self.oRequests[sAction] = null;
|
||||
delete self.oRequests[sAction];
|
||||
}
|
||||
|
||||
if (fTrigger)
|
||||
{
|
||||
fTrigger(false);
|
||||
}
|
||||
});
|
||||
|
||||
if (oH)
|
||||
{
|
||||
if (this.oRequests[sAction])
|
||||
{
|
||||
this.oRequests[sAction] = null;
|
||||
delete this.oRequests[sAction];
|
||||
}
|
||||
|
||||
this.oRequests[sAction] = oH;
|
||||
}
|
||||
|
||||
return oDeferred.promise;
|
||||
};
|
||||
|
||||
AbstractAjaxPromises.prototype.getRequest = function (sAction, fTrigger, sAdditionalGetString, iTimeOut)
|
||||
{
|
||||
sAdditionalGetString = Utils.isUnd(sAdditionalGetString) ? '' : Utils.pString(sAdditionalGetString);
|
||||
sAdditionalGetString = sAction + '/' + sAdditionalGetString;
|
||||
|
||||
return this.ajaxRequest(sAction, false, iTimeOut, null, sAdditionalGetString, fTrigger);
|
||||
};
|
||||
|
||||
AbstractAjaxPromises.prototype.postRequest = function (sAction, fTrigger, oParameters, iTimeOut)
|
||||
{
|
||||
oParameters = oParameters || {};
|
||||
oParameters['Action'] = sAction;
|
||||
|
||||
return this.ajaxRequest(sAction, true, iTimeOut, oParameters, '', fTrigger);
|
||||
};
|
||||
|
||||
module.exports = AbstractAjaxPromises;
|
||||
|
||||
}());
|
||||
58
dev/Promises/User/Ajax.js
Normal file
58
dev/Promises/User/Ajax.js
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
|
||||
(function () {
|
||||
|
||||
'use strict';
|
||||
|
||||
var
|
||||
_ = require('_'),
|
||||
ko = require('ko'),
|
||||
|
||||
AbstractAjaxPromises = require('Promises/AbstractAjax'),
|
||||
|
||||
MessageModel = require('Model/Message')
|
||||
;
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @extends AbstractAjaxPromises
|
||||
*/
|
||||
function PromisesUserAjax()
|
||||
{
|
||||
AbstractAjaxPromises.call(this);
|
||||
|
||||
this.messageListSimple.loading = ko.observable(false).extend({'rateLimit': 1});
|
||||
this.messageListSimple.hash = '';
|
||||
this.messageListSimple.cache = null;
|
||||
}
|
||||
|
||||
PromisesUserAjax.prototype.messageListSimple = function (sFolder, aUids)
|
||||
{
|
||||
var self = this, sHash = sFolder + '~' + aUids.join('/');
|
||||
if (sHash === this.messageListSimple.hash && this.messageListSimple.cache)
|
||||
{
|
||||
return this.fastPromise(this.messageListSimple.cache);
|
||||
}
|
||||
|
||||
return this.abort('MessageListSimple')
|
||||
.postRequest('MessageListSimple', this.messageListSimple.loading, {
|
||||
'Folder': sFolder,
|
||||
'Uids': aUids
|
||||
}).then(function (aData) {
|
||||
|
||||
var aResult = _.compact(_.map(aData, function (aItem) {
|
||||
return MessageModel.newInstanceFromJson(aItem);
|
||||
}));
|
||||
|
||||
self.messageListSimple.hash = sHash;
|
||||
self.messageListSimple.cache = aResult;
|
||||
|
||||
return aResult;
|
||||
})
|
||||
;
|
||||
};
|
||||
|
||||
_.extend(PromisesUserAjax.prototype, AbstractAjaxPromises.prototype);
|
||||
|
||||
module.exports = new PromisesUserAjax();
|
||||
|
||||
}());
|
||||
Loading…
Add table
Add a link
Reference in a new issue