From ffca56f7cdc070b0412a0f1c25dd1d4531424fa5 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Mon, 2 Dec 2013 12:44:06 +0000 Subject: [PATCH] refactor(dom) saveDatatToStorage && getDataFromStorage do not call checkStorageHash --- lib/client/dom.js | 49 +++++++++++++++++++++++----------------------- lib/client/edit.js | 23 ++++++++++++++-------- 2 files changed, 40 insertions(+), 32 deletions(-) diff --git a/lib/client/dom.js b/lib/client/dom.js index 95d4f0e0..47419a6e 100644 --- a/lib/client/dom.js +++ b/lib/client/dom.js @@ -1520,14 +1520,15 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog; error : pCallBack.error }; - DOM.getDataFromStorage(lPath, function(data, hash) { - if (data) - lFunc(data); - else { - lHash = hash; - DOM.getCurrentFileContent(lParams, lCurrentFile); - } - }); + + DOM.checkStorageHash(lPath, function(error, equal, hash) { + Util.ifExec(!error && equal, function() { + DOM.getDataFromStorage(lPath, lFunc); + }, function() { + lHash = hash; + DOM.getCurrentFileContent(lParams, lCurrentFile); + }); + }); }; @@ -1797,7 +1798,7 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog; * @param data * @param callback */ - this.saveDataToStorage = function(name, data, callback) { + this.saveDataToStorage = function(name, data, hash, callback) { CloudCmd.getConfig(function(config) { var allowed = config.localStorage, isDir = DOM.isCurrentIsDir(), @@ -1807,13 +1808,16 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog; if (!allowed || isDir) Util.exec(callback); else - DOM.checkStorageHash(name, function(error, equal, hash) { - if (!error && !equal) { - Storage.set(nameHash, hash); - Storage.set(nameData, data); - } + Util.ifExec(hash, function() { + Storage.set(nameHash, hash); + Storage.set(nameData, data); Util.exec(callback, hash); + }, function(callback) { + DOM.loadCurrentHash(function(loadHash) { + hash = loadHash; + callback(); + }); }); }); }; @@ -1827,7 +1831,7 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog; */ this.getDataFromStorage = function(name, callback) { CloudCmd.getConfig(function(config) { - var hash, + var data, hash, nameHash = name + '-hash', nameData = name + '-data', allowed = config.localStorage, @@ -1835,15 +1839,12 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog; if (!allowed || isDir) Util.exec(callback); - else - DOM.checkStorageHash(name, function(error, equal, hash) { - var data; - - if (!error && equal) - data = Storage.get(nameData, data); - - Util.exec(callback, data, hash); - }); + else { + data = Storage.get(nameData); + hash = Storage.get(nameHash); + + Util.exec(callback, data, hash); + } }); }; diff --git a/lib/client/edit.js b/lib/client/edit.js index fcfa903c..3375bebe 100644 --- a/lib/client/edit.js +++ b/lib/client/edit.js @@ -159,7 +159,12 @@ var CloudCmd, Util, DOM, CloudFunc, ace, DiffProto, diff_match_patch; var query, isAllowed = DOM.Storage.isAllowed(), isString = Util.isString(patch), - lessLength = patch.length < lValue.length; + lessLength = patch.length < lValue.length, + onSave = function(hash) { + return function(data) { + onSave(data, hash); + }; + }; Value = lValue; @@ -169,15 +174,15 @@ var CloudCmd, Util, DOM, CloudFunc, ace, DiffProto, diff_match_patch; } - Util.ifExec(!isAllowed, function() { - DOM.RESTful.save(lPath, lValue, onSave, query); + Util.ifExec(!isAllowed, function(loadHash) { + DOM.RESTful.save(lPath, lValue, onSave(loadHash), query); }, function(callback) { - DOM.checkStorageHash(lPath, function(error, equal) { + DOM.checkStorageHash(lPath, function(error, equal, loadHash) { if (!error) { if (!equal) query = ''; - callback(); + callback(loadHash); } }); }); @@ -206,8 +211,10 @@ var CloudCmd, Util, DOM, CloudFunc, ace, DiffProto, diff_match_patch; patch = Diff.createPatch(Value, pNewValue); Util.exec(pCallBack, patch); }, function(callback) { - DOM.getCurrentData(function(data) { - Value = data.data; + var path = DOM.getCurrentPath(); + + DOM.getDataFromStorage(path, function(data) { + Value = data; callback(); }); }); @@ -254,7 +261,7 @@ var CloudCmd, Util, DOM, CloudFunc, ace, DiffProto, diff_match_patch; if (!isError) { Edit.showMessage(text); - DOM.saveDataToStorage(path, Value); + DOM.saveDataToStorage(path, Value, hash); } else { ret = Dialog.confirm(text + msg);