2014-09-04 18:06:25 +02:00
|
|
|
var App = App || {};
|
|
|
|
|
2014-10-22 18:43:09 +02:00
|
|
|
App.Promise = function(_, jQuery, progress) {
|
2014-09-04 18:06:25 +02:00
|
|
|
|
2014-12-20 12:54:39 +01:00
|
|
|
function BrokenPromiseError(promiseId) {
|
|
|
|
this.name = 'BrokenPromiseError';
|
|
|
|
this.message = 'Broken promise (promise ID: ' + promiseId + ')';
|
|
|
|
}
|
|
|
|
BrokenPromiseError.prototype = new Error();
|
|
|
|
|
2014-10-02 00:30:25 +02:00
|
|
|
var active = [];
|
2014-10-04 13:15:18 +02:00
|
|
|
var promiseId = 0;
|
2014-10-02 00:30:25 +02:00
|
|
|
|
|
|
|
function make(callback) {
|
|
|
|
var deferred = jQuery.Deferred();
|
|
|
|
var promise = deferred.promise();
|
2014-10-04 13:15:18 +02:00
|
|
|
promise.promiseId = ++ promiseId;
|
2014-10-02 00:30:25 +02:00
|
|
|
|
2014-10-22 19:09:25 +02:00
|
|
|
progress.start();
|
2014-10-02 00:30:25 +02:00
|
|
|
callback(function() {
|
2014-10-22 19:09:25 +02:00
|
|
|
try {
|
|
|
|
deferred.resolve.apply(deferred, arguments);
|
|
|
|
active = _.without(active, promise.promiseId);
|
|
|
|
progress.done();
|
|
|
|
} catch (e) {
|
2014-12-20 12:54:39 +01:00
|
|
|
if (!(e instanceof BrokenPromiseError)) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
2014-10-22 19:09:25 +02:00
|
|
|
progress.reset();
|
|
|
|
}
|
2014-10-02 00:30:25 +02:00
|
|
|
}, function() {
|
2014-10-22 19:09:25 +02:00
|
|
|
try {
|
|
|
|
deferred.reject.apply(deferred, arguments);
|
|
|
|
active = _.without(active, promise.promiseId);
|
|
|
|
progress.done();
|
|
|
|
} catch (e) {
|
2014-12-20 12:54:39 +01:00
|
|
|
if (!(e instanceof BrokenPromiseError)) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
2014-10-22 19:09:25 +02:00
|
|
|
progress.reset();
|
|
|
|
}
|
2014-10-02 00:30:25 +02:00
|
|
|
});
|
|
|
|
|
2014-10-04 22:46:28 +02:00
|
|
|
active.push(promise.promiseId);
|
|
|
|
|
2014-10-10 18:13:46 +02:00
|
|
|
promise.always(function() {
|
2014-10-04 13:15:18 +02:00
|
|
|
if (!_.contains(active, promise.promiseId)) {
|
2014-12-20 12:54:39 +01:00
|
|
|
throw new BrokenPromiseError(promise.promiseId);
|
2014-10-02 00:30:25 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
function wait() {
|
|
|
|
var promises = arguments;
|
2014-09-04 18:06:25 +02:00
|
|
|
var deferred = jQuery.Deferred();
|
2014-10-02 00:30:25 +02:00
|
|
|
return jQuery.when.apply(jQuery, promises)
|
|
|
|
.then(function() {
|
|
|
|
return deferred.resolve.apply(deferred, arguments);
|
|
|
|
}).fail(function() {
|
|
|
|
return deferred.reject.apply(deferred, arguments);
|
|
|
|
});
|
2014-09-04 18:06:25 +02:00
|
|
|
}
|
|
|
|
|
2014-10-02 00:30:25 +02:00
|
|
|
function abortAll() {
|
|
|
|
active = [];
|
2014-09-04 18:06:25 +02:00
|
|
|
}
|
|
|
|
|
2014-10-02 00:30:25 +02:00
|
|
|
function getActive() {
|
|
|
|
return active.length;
|
2014-09-04 18:06:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
make: make,
|
|
|
|
wait: wait,
|
2014-10-02 00:30:25 +02:00
|
|
|
getActive: getActive,
|
|
|
|
abortAll: abortAll,
|
2014-09-04 18:06:25 +02:00
|
|
|
};
|
|
|
|
|
2014-09-08 22:02:28 +02:00
|
|
|
};
|
2014-09-04 18:06:25 +02:00
|
|
|
|
2014-10-22 18:43:09 +02:00
|
|
|
App.DI.registerSingleton('promise', ['_', 'jQuery', 'progress'], App.Promise);
|