Commit 6d8edb39 authored by Tristan Cavelier's avatar Tristan Cavelier

useless code removed

parent c0c9301e
......@@ -44,219 +44,10 @@
addStorageFunction = require('jio').addStorage,
uniqueJSONStringify = require('jio').util.uniqueJSONStringify;
/**
* Test if the a value is a date
*
* @param {String,Number,Date} date The date to test
* @return {Boolean} true if success, else false
*/
function isDate(date) {
return !isNaN((new Date(date === null ? undefined : date)).getTime());
}
/**
* Executes a sequence of *then* callbacks. It acts like
* `smth().then(callback).then(callback)...`. The first callback is called
* with no parameter.
*
* Elements of `then_list` array can be a function or an array contaning at
* most three *then* callbacks: *onFulfilled*, *onRejected*, *onNotified*.
*
* When `cancel()` is executed, each then promises are cancelled at the same
* time.
*
* sequence(then_list): Promise
*
* @param {Array} then_list An array of *then* callbacks
* @return {Promise} A new promise
*/
function sequence(then_list) {
var promise_list = [];
return new Promise(function (resolve, reject, notify) {
var i, length = then_list.length;
promise_list[0] = new Promise(function (resolve) {
resolve();
});
for (i = 0; i < length; i += 1) {
if (Array.isArray(then_list[i])) {
promise_list[i + 1] = promise_list[i].
then(then_list[i][0], then_list[i][1], then_list[i][2]);
} else {
promise_list[i + 1] = promise_list[i].then(then_list[i]);
}
}
promise_list[i].then(resolve, reject, notify);
}, function () {
var i, length = promise_list.length;
for (i = 0; i < length; i += 1) {
promise_list[i].cancel();
}
});
}
function success(promise) {
return promise.then(null, function (reason) { return reason; });
}
// /**
// * Awaits for an answer from one promise only. Promises are cancelled only
// * by calling `first(promise_list).cancel()`.
// *
// * first(promise_list): Promise
// *
// * @param {Array} promise_list An array of promises
// * @return {Promise} A new promise
// */
// function first(promise_list) {
// var length = promise_list.length;
// promise_list = promise_list.slice();
// return new Promise(function (resolve, reject, notify) {
// var index, count = 0;
// function rejecter(answer) {
// count += 1;
// if (count === length) {
// return reject(answer);
// }
// }
// function notifier(index) {
// return function (notification) {
// notify({
// "index": index,
// "value": notification
// });
// };
// }
// for (index = 0; index < length; index += 1) {
// promise_list[index].then(resolve, rejecter, notifier(index));
// }
// }, function () {
// var index;
// for (index = 0; index < length; index += 1) {
// promise_list[index].cancel();
// }
// });
// }
/**
* Responds with the last resolved promise answer recieved. If all promises
* are rejected, it returns the latest rejected promise answer
* received. Promises are cancelled only by calling
* `last(promise_list).cancel()`.
*
* last(promise_list): Promise
*
* @param {Array} promise_list An array of promises
* @return {Promise} A new promise
*/
function last(promise_list) {
var length = promise_list.length;
promise_list = promise_list.slice();
return new Promise(function (resolve, reject, notify) {
var index, last_answer, count = 0, error_count = 0;
function resolver() {
return function (answer) {
count += 1;
if (count === length) {
return resolve(answer);
}
last_answer = answer;
};
}
function rejecter() {
return function (answer) {
error_count += 1;
if (error_count === length) {
return reject(answer);
}
count += 1;
if (count === length) {
return resolve(last_answer);
}
};
}
function notifier(index) {
return function (notification) {
notify({
"index": index,
"value": notification
});
};
}
for (index = 0; index < length; index += 1) {
promise_list[index].then(resolver(), rejecter(), notifier(index));
}
}, function () {
var index;
for (index = 0; index < length; index += 1) {
promise_list[index].cancel();
}
});
}
/**
* Responds with the last modified document recieved. If all promises are
* rejected, it returns the latest rejected promise answer received. Promises
* are cancelled only by calling `lastModified(promise_list).cancel()`. USE
* THIS FUNCTION ONLY FOR GET METHOD!
*
* lastModified(promise_list): Promise
*
* @param {Array} promise_list An array of promises
* @return {Promise} A new promise
*/
function lastModified(promise_list) {
var length = promise_list.length;
promise_list = promise_list.slice();
return new Promise(function (resolve, reject, notify) {
var index, last_good_answer, last_answer, count = 0, error_count = 0;
function resolver(answer) {
last_answer = answer;
if (last_good_answer === undefined) {
if (isDate(answer.data.modified)) {
last_good_answer = answer;
}
} else {
if (isDate(answer.data.modified)) {
if (new Date(last_good_answer.data.modified) <
new Date(answer.data.modified)) {
last_good_answer = answer;
}
}
}
count += 1;
if (count === length) {
return resolve(last_good_answer);
}
}
function rejecter(answer) {
error_count += 1;
if (error_count === length) {
return reject(answer);
}
count += 1;
if (count === length) {
return resolve(last_good_answer || last_answer);
}
}
function notifier(index) {
return function (notification) {
notify({
"index": index,
"value": notification
});
};
}
for (index = 0; index < length; index += 1) {
promise_list[index].then(resolver, rejecter, notifier(index));
}
}, function () {
var index;
for (index = 0; index < length; index += 1) {
promise_list[index].cancel();
}
});
}
/**
* firstFulfilled(promises): promises< last_fulfilment_value >
*
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment