Commit ad9d385a authored by François Billioud's avatar François Billioud

comment and debug IndexedStorage

parent 360f62d8
...@@ -395,8 +395,28 @@ ...@@ -395,8 +395,28 @@
}, },
/*************************************************************** /**
*********************** IndexedStorage *************************/ * load the list of the documents in this storage
* @param option : optional object containing
* "success" : the function to execute when the load is done
* "errorHandler" : the function to execute if an error occures
* @return null if the request is asynchronous, the list of documents otherwise.
* @example {"file1":{fileName:"file1",creationDate:"Tue, 23 Aug 2011 15:18:32 GMT",lastModified:"Tue, 23 Aug 2011 15:18:32 GMT"},...}
*/
getDocumentList: function(option) {
var list = this.documents;
if(option.success) option.success(list);
return list;
},
save: function() {
localStorage[this.userName]=JSON.stringify(this.documents);
}
}
/***************************************************************
*********************** IndexedStorage *************************/
/** /**
* Class IndexedStorage * Class IndexedStorage
* @class provides usual API to create an index while storing documents * @class provides usual API to create an index while storing documents
...@@ -411,17 +431,18 @@ ...@@ -411,17 +431,18 @@
//initialize the index //initialize the index
var storage = this; var storage = this;
(function initialize() {//try to download jio.index (function initialize() {//try to download jio.index
if(this.storage) { if(storage.storage) {
this.storage.loadDocument("jio.index","text",function(data) {storage.index = this;},function(error) { var loadOption = {
if(error.status==404) {createIndex()} else {alert("error "+error.status+" while trying to download jio.index")} success: function(data) {storage.index = JSON.parse(data);},
}); errorHandler: function(error) {
if(error.status==404) {storage.createIndex()} else {alert("error "+error.status+" while trying to download jio.index")}
}
}
storage.storage.loadDocument("jio.index", loadOption);
} else {//if the storage is not ready } else {//if the storage is not ready
setTimeout(initialize,50); setTimeout(initialize,50);
} }
})() })()
function createIndex() {//create a new index if doesn't exist
storage.index = {}//for the moment, just consider that the folder is empty
}
} }
JIO.IndexedStorage.prototype = { JIO.IndexedStorage.prototype = {
/* delegate the call to the indexed storage */ /* delegate the call to the indexed storage */
...@@ -435,7 +456,13 @@ ...@@ -435,7 +456,13 @@
* @return the content of metaData, or the content of the document * @return the content of metaData, or the content of the document
*/ */
loadDocument: function(fileName, option) { loadDocument: function(fileName, option) {
var indexedStorage = this;
if(this.getIndex()===undefined) {
setTimeout(function() {indexedStorage.loadDocument(fileName, option)},50);
return null;
} else {
return option.metaDataOnly ? this.getIndex()[fileName] : this.storage.loadDocument.apply(this.storage,arguments) return option.metaDataOnly ? this.getIndex()[fileName] : this.storage.loadDocument.apply(this.storage,arguments)
}
}, },
...@@ -448,22 +475,22 @@ ...@@ -448,22 +475,22 @@
* "metaData" : information to store about the document * "metaData" : information to store about the document
*/ */
saveDocument: function(data, fileName, option) { saveDocument: function(data, fileName, option) {
var fileAlreadyExist = this.getIndex()[fileName];
return this.storage.saveDocument(data,fileName,generateSaveOption(fileAlreadyExist));
function generateSaveOption(fileAlreadyExist) {
var indexedStorage = this; var indexedStorage = this;
var saveOption = copy(option); if(this.getIndex()==null) {
saveOption.success = function(data) { setTimeout(function() {indexedStorage.saveDocument(data, fileName, option)},50);
return null;
} else {
var fileAlreadyExist = this.getIndex()[fileName]!==undefined;
var instruction = function() {
var time = Date.now(); var time = Date.now();
indexedStorage.getIndex()[fileName] = option.metaData; indexedStorage.getIndex()[fileName] = option.metaData || {};
indexedStorage.getIndex()[fileName].lastModified = time; indexedStorage.getIndex()[fileName].lastModified = time;
indexedStorage.getIndex()[fileName].fileName = fileName; indexedStorage.getIndex()[fileName].fileName = fileName;
if(!fileAlreadyExist) {indexedStorage.getIndex()[fileName].creationDate = time;} if(!fileAlreadyExist) {indexedStorage.getIndex()[fileName].creationDate = time;}
indexedStorage.save(); indexedStorage.save();
option.success(data);
} }
return saveOption option.success = addInstruction(instruction, option.success, true);
return this.storage.saveDocument(data,fileName,option);
} }
}, },
...@@ -477,16 +504,21 @@ ...@@ -477,16 +504,21 @@
*/ */
deleteDocument: function(file, option) { deleteDocument: function(file, option) {
var indexedStorage = this; var indexedStorage = this;
if(this.getIndex()===undefined) {
setTimeout(function() {indexedStorage.deleteDocument.call(indexedStorage, file, option)},50);
return null;
} else {
var newOption = copy(option); var newOption = copy(option);
newOption.success = function() { newOption.success = function() {
if(option.success) {option.success()}
oneOrEach(file,deleteFileData); oneOrEach(file,deleteFileData);
indexedStorage.save(); indexedStorage.save();
if(option.success) {option.success()}
} }
return this.storage.deleteDocument(file, newOption); return this.storage.deleteDocument(file, newOption);
}
function deleteFileData(fileName) { function deleteFileData(fileName) {
delete this.getIndex()[fileName]; delete indexedStorage.getIndex()[fileName];
} }
}, },
...@@ -499,11 +531,27 @@ ...@@ -499,11 +531,27 @@
* @example {"file1":{fileName:"file1",creationDate:"Tue, 23 Aug 2011 15:18:32 GMT",lastModified:"Tue, 23 Aug 2011 15:18:32 GMT"},...} * @example {"file1":{fileName:"file1",creationDate:"Tue, 23 Aug 2011 15:18:32 GMT",lastModified:"Tue, 23 Aug 2011 15:18:32 GMT"},...}
*/ */
getDocumentList: function(option) { getDocumentList: function(option) {
var indexedStorage = this;
if(this.getIndex()===undefined) {
setTimeout(function() {indexedStorage.getDocumentList.call(indexedStorage, fileName, option)},50);
return null;
} else {
var list = this.getIndex(); var list = this.getIndex();
if(option.success) option.success(list); if(option.success) option.success(list);
return list; return list;
}
}, },
getIndex: function() {return this.index},
save: function() {
this.storage.saveDocument(JSON.stringify(this.getIndex()), "jio.index", "text", true);
},
createIndex: function () {//create a new index if doesn't exist
this.index = {}//for the moment, just consider that the folder is empty
}
}
getIndex: function() {return this.index}, getIndex: function() {return this.index},
save: function() { save: function() {
this.storage.saveDocument(JSON.stringify(this.getIndex()), "jio.index", "text", true); this.storage.saveDocument(JSON.stringify(this.getIndex()), "jio.index", "text", true);
......
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