Commit 09252852 authored by Tristan Cavelier's avatar Tristan Cavelier

davstorage updated to use RSVP

parent 44b3186d
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
*/ */
/*jslint indent: 2, maxlen: 80, nomen: true, regexp: true, unparam: true */ /*jslint indent: 2, maxlen: 80, nomen: true, regexp: true, unparam: true */
/*global define, window, jIO, promy, btoa, DOMParser, Blob */ /*global define, window, jIO, RSVP, btoa, DOMParser, Blob */
// JIO Dav Storage Description : // JIO Dav Storage Description :
// { // {
...@@ -45,8 +45,8 @@ ...@@ -45,8 +45,8 @@
return define(dependencies, module); return define(dependencies, module);
} }
window.dav_storage = {}; window.dav_storage = {};
module(window.dav_storage, promy, jIO); module(window.dav_storage, RSVP, jIO);
}(['exports', 'promy', 'jio'], function (exports, promy, jIO) { }(['exports', 'rsvp', 'jio'], function (exports, RSVP, jIO) {
"use strict"; "use strict";
/** /**
...@@ -167,13 +167,11 @@ ...@@ -167,13 +167,11 @@
} }
function promiseSucceed(promise) { function promiseSucceed(promise) {
var deferred = new promy.Deferred(); return new RSVP.Promise(function (resolve, reject, notify) {
promise.then( promise.then(resolve, reject, notify);
deferred.resolve.bind(deferred), }, function () {
deferred.resolve.bind(deferred), promise.cancel();
deferred.notify.bind(deferred) });
);
return deferred.promise;
} }
/** /**
...@@ -265,7 +263,7 @@ ...@@ -265,7 +263,7 @@
return {"target": { return {"target": {
"status": e.target.status, "status": e.target.status,
"statusText": e.target.statusText, "statusText": e.target.statusText,
"response": JSON.parse(e.target.response) "response": JSON.parse(e.target.responseText)
}}; }};
} catch (err) { } catch (err) {
throw {"target": { throw {"target": {
...@@ -284,6 +282,13 @@ ...@@ -284,6 +282,13 @@
null, null,
this._login this._login
); );
// .then(function (v) { // for sinon js compatibility
// return {"target": {
// "status": v.target.status,
// "statusText": v.target.statusText,
// "response": new Blob([v.target.responseText])
// }};
// });
}; };
DavStorage.prototype._remove = function (param) { DavStorage.prototype._remove = function (param) {
...@@ -316,7 +321,7 @@ ...@@ -316,7 +321,7 @@
this._login this._login
).then(function (e) { ).then(function (e) {
var i, rows = [], row, responses = new DOMParser().parseFromString( var i, rows = [], row, responses = new DOMParser().parseFromString(
e.target.response, e.target.responseText,
"text/xml" "text/xml"
).querySelectorAll( ).querySelectorAll(
"D\\:response, response" "D\\:response, response"
...@@ -401,8 +406,7 @@ ...@@ -401,8 +406,7 @@
o.notify_message = "Updating metadata"; o.notify_message = "Updating metadata";
o.error_message = "DavStorage, unable to update document."; o.error_message = "DavStorage, unable to update document.";
o.percentage = [30, 100]; o.percentage = [30, 100];
this._put(metadata).progress(o.notifyProgress). this._put(metadata).then(o.success, o.reject, o.notifyProgress);
done(o.success).fail(o.reject);
}.bind(this), }.bind(this),
errorDocumentExists: function (e) { errorDocumentExists: function (e) {
command.error( command.error(
...@@ -422,8 +426,7 @@ ...@@ -422,8 +426,7 @@
o.percentage = [30, 100]; o.percentage = [30, 100];
o.notify_message = "Updating metadata"; o.notify_message = "Updating metadata";
o.error_message = "DavStorage, unable to create document."; o.error_message = "DavStorage, unable to create document.";
this._put(metadata).progress(o.notifyProgress). this._put(metadata).then(o.success, o.reject, o.notifyProgress);
done(o.success).fail(o.reject);
}.bind(this), }.bind(this),
success: function (e) { success: function (e) {
command.success(e.target.status, {"id": metadata._id}); command.success(e.target.status, {"id": metadata._id});
...@@ -437,9 +440,11 @@ ...@@ -437,9 +440,11 @@
} }
}; };
this._get(metadata).progress(o.notifyProgress). this._get(metadata).then(
done(method === 'post' ? o.errorDocumentExists : o.putMetadata). method === 'post' ? o.errorDocumentExists : o.putMetadata,
fail(o.putMetadataIfPossible); o.putMetadataIfPossible,
o.notifyProgress
);
}; };
/** /**
...@@ -481,9 +486,6 @@ ...@@ -481,9 +486,6 @@
percentage: [0, 30], percentage: [0, 30],
notify_message: "Getting metadata", notify_message: "Getting metadata",
notifyProgress: function (e) { notifyProgress: function (e) {
if (e === null) {
return;
}
command.notify({ command.notify({
"method": "putAttachment", "method": "putAttachment",
"message": o.notify_message, "message": o.notify_message,
...@@ -498,15 +500,15 @@ ...@@ -498,15 +500,15 @@
o.percentage = [30, 70]; o.percentage = [30, 70];
o.notify_message = "Putting attachment"; o.notify_message = "Putting attachment";
o.remote_metadata = e.target.response; o.remote_metadata = e.target.response;
return promy.join( return RSVP.all([
this._putAttachment(param), this._putAttachment(param),
jIO.util.readBlobAsBinaryString(param._blob) jIO.util.readBlobAsBinaryString(param._blob)
).then(null, null, function (e) { ]).then(null, null, function (e) {
// propagate only putAttachment progress // propagate only putAttachment progress
if (e.index === 0) { if (e.index === 0) {
return e.answer; return e.value;
} }
return null; throw null;
}); });
}.bind(this), }.bind(this),
putMetadata: function (answers) { putMetadata: function (answers) {
...@@ -538,7 +540,7 @@ ...@@ -538,7 +540,7 @@
this._get(param). this._get(param).
then(o.putAttachmentAndReadBlob). then(o.putAttachmentAndReadBlob).
then(o.putMetadata). then(o.putMetadata).
done(o.success).fail(o.reject).progress(o.notifyProgress); then(o.success, o.reject, o.notifyProgress);
}; };
/** /**
...@@ -572,8 +574,7 @@ ...@@ -572,8 +574,7 @@
} }
}; };
this._get(param). this._get(param).then(o.success, o.reject, o.notifyGetProgress);
done(o.success).fail(o.reject).progress(o.notifyGetProgress);
}; };
/** /**
...@@ -634,7 +635,7 @@ ...@@ -634,7 +635,7 @@
this._get(param). this._get(param).
then(o.getAttachment). then(o.getAttachment).
done(o.success).fail(o.reject).progress(o.notifyProgress); then(o.success, o.reject, o.notifyProgress);
}; };
/** /**
...@@ -691,8 +692,8 @@ ...@@ -691,8 +692,8 @@
} }
o.count = 0; o.count = 0;
o.nb_requests = requests.length; o.nb_requests = requests.length;
return promy.join.apply(null, requests).then(null, null, function (e) { return RSVP.all(requests).then(null, null, function (e) {
if (e.answer.loaded === e.answer.total) { if (e.value.loaded === e.value.total) {
o.count += 1; o.count += 1;
command.notify({ command.notify({
"method": "remove", "method": "remove",
...@@ -723,7 +724,7 @@ ...@@ -723,7 +724,7 @@
this._get(param). this._get(param).
then(o.removeDocument). then(o.removeDocument).
then(o.removeAllAttachments). then(o.removeAllAttachments).
done(o.success).fail(o.reject).progress(o.notifyProgress); then(o.success, o.reject, o.notifyProgress);
}; };
/** /**
...@@ -797,7 +798,7 @@ ...@@ -797,7 +798,7 @@
this._get(param). this._get(param).
then(o.updateMetadata). then(o.updateMetadata).
then(o.removeAttachment). then(o.removeAttachment).
done(o.success).fail(o.reject).progress(o.notifyProgress); then(o.success, o.reject, o.notifyProgress);
}; };
/** /**
...@@ -843,8 +844,8 @@ ...@@ -843,8 +844,8 @@
o.nb_requests = requests.length; o.nb_requests = requests.length;
o.error_message = "DavStorage, an error occured while " + o.error_message = "DavStorage, an error occured while " +
"getting document metadata"; "getting document metadata";
return promy.join.apply(null, requests).then(null, null, function (e) { return RSVP.all(requests).then(null, null, function (e) {
if (e.answer.loaded === e.answer.total) { if (e.value.loaded === e.value.total) {
o.count += 1; o.count += 1;
command.notify({ command.notify({
"method": "allDocs", "method": "allDocs",
...@@ -857,7 +858,7 @@ ...@@ -857,7 +858,7 @@
) )
}); });
} }
return null; throw null;
}); });
}.bind(this), }.bind(this),
success: function () { success: function () {
...@@ -876,7 +877,7 @@ ...@@ -876,7 +877,7 @@
this._allDocs(param, options). this._allDocs(param, options).
then(o.getAllMetadataIfNecessary). then(o.getAllMetadataIfNecessary).
done(o.success).fail(o.reject).progress(o.notifyProgress); then(o.success, o.reject, o.notifyProgress);
}; };
jIO.addStorage('dav', DavStorage); jIO.addStorage('dav', DavStorage);
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
<link rel="stylesheet" href="../../lib/qunit/qunit.css" /> <link rel="stylesheet" href="../../lib/qunit/qunit.css" />
<script src="../../lib/qunit/qunit.js"></script> <script src="../../lib/qunit/qunit.js"></script>
<script src="../../src/promy/promy.js"></script> <script src="../../lib/rsvp/rsvp-custom.js"></script>
<script src="../../src/sha256.amd.js"></script> <script src="../../src/sha256.amd.js"></script>
<script src="../../jio.js"></script> <script src="../../jio.js"></script>
<script src="../../complex_queries.js"></script> <script src="../../complex_queries.js"></script>
......
/*jslint indent: 2, maxlen: 80, nomen: true */ /*jslint indent: 2, maxlen: 80, nomen: true */
/*global module, test, stop, start, expect, ok, deepEqual, location, sinon, /*global module, test, stop, start, expect, ok, deepEqual, location, sinon,
davstorage_spec, promy, jIO, test_util, dav_storage, btoa */ davstorage_spec, RSVP, jIO, test_util, dav_storage, btoa */
(function () { (function () {
"use strict"; "use strict";
...@@ -25,13 +25,11 @@ ...@@ -25,13 +25,11 @@
module("Dav Storage"); module("Dav Storage");
function success(promise) { function success(promise) {
var deferred = new promy.Deferred(); return new RSVP.Promise(function (resolve, reject, notify) {
promise.then( promise.then(resolve, resolve, notify);
deferred.resolve.bind(deferred), }, function () {
deferred.resolve.bind(deferred), promise.cancel();
deferred.notify.bind(deferred) });
);
return deferred.promise;
} }
/** /**
...@@ -940,59 +938,57 @@ ...@@ -940,59 +938,57 @@
// # Post new documents, list them and remove them // # Post new documents, list them and remove them
// post a 201 // post a 201
postNewDocument().done(postNewDocumentTest). postNewDocument().then(postNewDocumentTest).
// get 200 // get 200
then(getCreatedDocument).done(getCreatedDocumentTest). then(getCreatedDocument).then(getCreatedDocumentTest).
// post b 201 // post b 201
then(postSpecificDocument).done(postSpecificDocumentTest). then(postSpecificDocument).then(postSpecificDocumentTest).
// allD 200 2 documents // allD 200 2 documents
then(listDocuments).done(list2DocumentsTest). then(listDocuments).then(list2DocumentsTest).
// remove a 204 // remove a 204
then(removeCreatedDocument).done(removeCreatedDocumentTest). then(removeCreatedDocument).then(removeCreatedDocumentTest).
// remove b 204 // remove b 204
then(removeSpecificDocument).done(removeSpecificDocumentTest). then(removeSpecificDocument).then(removeSpecificDocumentTest).
// allD 200 empty storage // allD 200 empty storage
then(listEmptyStorage).done(listEmptyStorageTest). then(listEmptyStorage).then(listEmptyStorageTest).
// # Create and update documents, and some attachment and remove them // # Create and update documents, and some attachment and remove them
// put 201 // put 201
then(putNewDocument).done(putNewDocumentTest). then(putNewDocument).then(putNewDocumentTest).
// get 200 // get 200
then(getCreatedDocument2).done(getCreatedDocument2Test). then(getCreatedDocument2).then(getCreatedDocument2Test).
// post 409 // post 409
then(postSameDocument).done(postSameDocumentTest). then(postSameDocument).then(postSameDocumentTest).
// putA a 204 // putA a 204
then(createAttachment).done(createAttachmentTest). then(createAttachment).then(createAttachmentTest).
// putA a 204 // putA a 204
then(updateAttachment).done(updateAttachmentTest). then(updateAttachment).then(updateAttachmentTest).
// putA b 204 // putA b 204
then(createAnotherAttachment).done(createAnotherAttachmentTest). then(createAnotherAttachment).then(createAnotherAttachmentTest).
// put 204 // put 204
then(updateLastDocument).done(updateLastDocumentTest). then(updateLastDocument).then(updateLastDocumentTest).
// getA a 200 // getA a 200
then(getFirstAttachment). then(getFirstAttachment).then(getFirstAttachmentTest).
then(getFirstAttachmentTest).
// getA b 200 // getA b 200
then(getSecondAttachment). then(getSecondAttachment).then(getSecondAttachmentTest).
then(getSecondAttachmentTest).
// get 200 // get 200
then(getLastDocument).done(getLastDocumentTest). then(getLastDocument).then(getLastDocumentTest).
// removeA b 204 // removeA b 204
then(removeSecondAttachment).done(removeSecondAttachmentTest). then(removeSecondAttachment).then(removeSecondAttachmentTest).
// getA b 404 // getA b 404
then(getInexistentSecondAttachment). then(getInexistentSecondAttachment).
done(getInexistentSecondAttachmentTest). then(getInexistentSecondAttachmentTest).
// get 200 // get 200
then(getOneAttachmentDocument).done(getOneAttachmentDocumentTest). then(getOneAttachmentDocument).then(getOneAttachmentDocumentTest).
// removeA b 404 // removeA b 404
then(removeSecondAttachmentAgain).done(removeSecondAttachmentAgainTest). then(removeSecondAttachmentAgain).then(removeSecondAttachmentAgainTest).
// remove 204 // remove 204
then(removeDocument).done(removeDocumentTest). then(removeDocument).then(removeDocumentTest).
// getA a 404 // getA a 404
then(getInexistentFirstAttachment).done(getInexistentFirstAttachmentTest). then(getInexistentFirstAttachment).then(getInexistentFirstAttachmentTest).
// get 404 // get 404
then(getInexistentDocument).done(getInexistentDocumentTest). then(getInexistentDocument).then(getInexistentDocumentTest).
// remove 404 // remove 404
then(removeInexistentDocument).done(removeInexistentDocumentTest). then(removeInexistentDocument).then(removeInexistentDocumentTest).
// check 204 // check 204
//then(checkDocument).done(checkDocumentTest). //then(checkDocument).done(checkDocumentTest).
//then(checkStorage).done(checkStorageTest). //then(checkStorage).done(checkStorageTest).
......
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