Commit d4e4af04 authored by Tristan Cavelier's avatar Tristan Cavelier

replicate return first fulfilled instead of last

parent 7999c878
...@@ -257,6 +257,52 @@ ...@@ -257,6 +257,52 @@
}); });
} }
/**
* firstFulfilled(promises): promises< last_fulfilment_value >
*
* Responds with the first resolved promise answer recieved. If all promises
* are rejected, it returns the latest rejected promise answer
* received. Promises are cancelled only by calling
* `firstFulfilled(promises).cancel()`.
*
* @param {Array} promises An array of promises
* @return {Promise} A new promise
*/
function firstFulfilled(promises) {
var length = promises.length;
function onCancel() {
var i, l, promise;
for (i = 0, l = promises.length; i < l; i += 1) {
promise = promises[i];
if (typeof promise.cancel === "function") {
promise.cancel();
}
}
}
return new Promise(function (resolve, reject, notify) {
var i, count = 0;
function resolver(answer) {
resolve(answer);
onCancel();
}
function rejecter(answer) {
count += 1;
if (count === length) {
return reject(answer);
}
}
for (i = 0; i < length; i += 1) {
promises[i].then(resolver, rejecter, notify);
}
}, onCancel);
}
// //////////////////////////////////////////////////////////////////////
// /** // /**
// * An Universal Unique ID generator // * An Universal Unique ID generator
// * // *
...@@ -349,13 +395,11 @@ ...@@ -349,13 +395,11 @@
// return; // return;
// } // }
for (index = 0; index < length; index += 1) { for (index = 0; index < length; index += 1) {
promise_list[index] = success( promise_list[index] =
command.storage(this._storage_list[index]).post(metadata, option) command.storage(this._storage_list[index]).post(metadata, option);
);
} }
sequence([function () { firstFulfilled(promise_list).
return last(promise_list); then(command.success, command.error, command.notify);
}, [command.success, command.error]]);
}; };
ReplicateStorage.prototype.put = function (command, metadata, option) { ReplicateStorage.prototype.put = function (command, metadata, option) {
...@@ -372,33 +416,28 @@ ...@@ -372,33 +416,28 @@
promise_list[index] = promise_list[index] =
command.storage(this._storage_list[index]).put(metadata, option); command.storage(this._storage_list[index]).put(metadata, option);
} }
sequence([function () { firstFulfilled(promise_list).
return last(promise_list); then(command.success, command.error, command.notify);
}, [command.success, command.error]]);
}; };
ReplicateStorage.prototype.putAttachment = function (command, param, option) { ReplicateStorage.prototype.putAttachment = function (command, param, option) {
var promise_list = [], index, length = this._storage_list.length; var promise_list = [], index, length = this._storage_list.length;
for (index = 0; index < length; index += 1) { for (index = 0; index < length; index += 1) {
promise_list[index] = success( promise_list[index] =
command.storage(this._storage_list[index]).putAttachment(param, option) command.storage(this._storage_list[index]).putAttachment(param, option);
);
} }
sequence([function () { firstFulfilled(promise_list).
return last(promise_list); then(command.success, command.error, command.notify);
}, [command.success, command.error]]);
}; };
ReplicateStorage.prototype.remove = function (command, param, option) { ReplicateStorage.prototype.remove = function (command, param, option) {
var promise_list = [], index, length = this._storage_list.length; var promise_list = [], index, length = this._storage_list.length;
for (index = 0; index < length; index += 1) { for (index = 0; index < length; index += 1) {
promise_list[index] = success( promise_list[index] =
command.storage(this._storage_list[index]).remove(param, option) command.storage(this._storage_list[index]).remove(param, option);
);
} }
sequence([function () { firstFulfilled(promise_list).
return last(promise_list); then(command.success, command.error, command.notify);
}, [command.success, command.error]]);
}; };
ReplicateStorage.prototype.removeAttachment = function ( ReplicateStorage.prototype.removeAttachment = function (
...@@ -408,14 +447,12 @@ ...@@ -408,14 +447,12 @@
) { ) {
var promise_list = [], index, length = this._storage_list.length; var promise_list = [], index, length = this._storage_list.length;
for (index = 0; index < length; index += 1) { for (index = 0; index < length; index += 1) {
promise_list[index] = success( promise_list[index] =
command.storage(this._storage_list[index]). command.storage(this._storage_list[index]).
removeAttachment(param, option) removeAttachment(param, option);
);
} }
sequence([function () { firstFulfilled(promise_list).
return last(promise_list); then(command.success, command.error, command.notify);
}, [command.success, command.error]]);
}; };
/** /**
...@@ -476,13 +513,11 @@ ...@@ -476,13 +513,11 @@
ReplicateStorage.prototype.getAttachment = function (command, param, option) { ReplicateStorage.prototype.getAttachment = function (command, param, option) {
var promise_list = [], index, length = this._storage_list.length; var promise_list = [], index, length = this._storage_list.length;
for (index = 0; index < length; index += 1) { for (index = 0; index < length; index += 1) {
promise_list[index] = success( promise_list[index] =
command.storage(this._storage_list[index]).getAttachment(param, option) command.storage(this._storage_list[index]).getAttachment(param, option);
);
} }
sequence([function () { firstFulfilled(promise_list).
return last(promise_list); then(command.success, command.error, command.notify);
}, [command.success, command.error]]);
}; };
ReplicateStorage.prototype.allDocs = function (command, param, option) { ReplicateStorage.prototype.allDocs = function (command, param, option) {
......
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