diff --git a/client/client.js b/client/client.js index 254780d0..f151c803 100644 --- a/client/client.js +++ b/client/client.js @@ -372,7 +372,10 @@ function CloudCmdProto(DOM) { order, }); - const newObj = await RESTful.read(path + query, 'json'); + const [, newObj] = await RESTful.read(path + query, 'json'); + + if (!newObj) + return; /* eslint require-atomic-updates:0 */ options.sort = sort; diff --git a/client/dom/files.js b/client/dom/files.js index dea2695f..c64f2a47 100644 --- a/client/dom/files.js +++ b/client/dom/files.js @@ -104,7 +104,7 @@ const getSystemFile = promisify((file, callback) => { }); }); -const getConfig = promisify((callback) => { +const getConfig = async () => { let is; if (!Promises.config) @@ -113,20 +113,18 @@ const getConfig = promisify((callback) => { return RESTful.Config.read(); }; - Promises.config().then((data) => { + const [, data] = await Promises.config(); + + if (data) is = false; - - callback(null, data); - - timeout(() => { - if (!is) - Promises.config = null; - }); - }, () => { + + timeout(() => { if (!is) Promises.config = null; }); -}); + + return data; +}; function getTimeoutOnce(time) { let is; diff --git a/client/dom/index.js b/client/dom/index.js index e731c893..6c276866 100644 --- a/client/dom/index.js +++ b/client/dom/index.js @@ -99,13 +99,13 @@ function CmdProto() { const name = getName(); - const [cancel, newName] = await Dialog.prompt(msg, name); + const [cancel, currentName] = await Dialog.prompt(msg, name); if (cancel) return; const path = (type) => { - const result = dir + newName; + const result = dir + currentName; if (!type) return result; @@ -115,7 +115,7 @@ function CmdProto() { await RESTful.write(path(type)); await CloudCmd.refresh({ - currentName: newName, + currentName, }); } @@ -240,7 +240,7 @@ function CmdProto() { if (name === '..') return; - const size = await RESTful.read(link + query); + const [, size] = await RESTful.read(link + query); DOM.setCurrentSize(size, current); Images.hide(); @@ -355,19 +355,6 @@ function CmdProto() { }); }; - /** - * unified way to save current file content - * - * @callback - function({data, name}) {} - * @currentFile - */ - this.saveCurrentData = (url, data, callback, query = '') => { - RESTful.write(url + query, data) - .then(() => { - DOM.saveDataToStorage(url, data); - }); - }; - /** * unified way to get RefreshButton */ @@ -768,7 +755,10 @@ function CmdProto() { to : dirPath + to, }; - await RESTful.mv(files); + const [e] = await RESTful.mv(files); + + if (e) + return; DOM.setCurrentName(to, current); Storage.remove(dirPath); @@ -869,7 +859,7 @@ function CmdProto() { return; CloudCmd.loadDir({ - path + path, }); }, diff --git a/client/dom/rest.js b/client/dom/rest.js index e6c84b7b..9b36e27a 100644 --- a/client/dom/rest.js +++ b/client/dom/rest.js @@ -3,7 +3,7 @@ /* global CloudCmd, DOM */ const itype = require('itype/legacy'); -const {promisify} = require('es6-promisify'); +const promisify = require('../../common/try-to-promisify'); const {FS} = require('../../common/cloudfunc'); const {encode} = require('../../common/entity'); diff --git a/client/modules/config/index.js b/client/modules/config/index.js index 4ca85b03..d85d2942 100644 --- a/client/modules/config/index.js +++ b/client/modules/config/index.js @@ -190,7 +190,7 @@ function hide() { CloudCmd.View.hide(); } -function onChange(el) { +async function onChange(el) { const obj = {}; const name = input.getName(el); const data = input.getValue(name, Element); @@ -202,7 +202,7 @@ function onChange(el) { obj[name] = data; - Config.save(obj); + await Config.save(obj); } function onSave(obj) { @@ -214,12 +214,14 @@ function onSave(obj) { }); } -function saveHttp(obj) { +async function saveHttp(obj) { const {RESTful} = DOM; + const [e] = await RESTful.Config.write(obj); - RESTful.Config.write(obj).then(() => { - onSave(obj); - }); + if (e) + return; + + onSave(obj); } function onAuthChange(checked) { diff --git a/client/modules/menu.js b/client/modules/menu.js index 6380ff66..502d4100 100644 --- a/client/modules/menu.js +++ b/client/modules/menu.js @@ -244,8 +244,11 @@ function uploadFromCloud() { CloudCmd.execFromModule('Cloud', 'saveFile', async (currentName, data) => { const path = DOM.getCurrentDirPath() + currentName; + const [e] = await RESTful.write(path, data); + + if (e) + return; - await RESTful.write(path, data); await CloudCmd.refresh({currentName}); }); } diff --git a/client/modules/user-menu/default-menu.js b/client/modules/user-menu/default-menu.js index 8504fded..fb75a834 100644 --- a/client/modules/user-menu/default-menu.js +++ b/client/modules/user-menu/default-menu.js @@ -14,7 +14,7 @@ module.exports = { await DOM.renameCurrent(); }, - 'C - Create User Menu File': async ({DOM, CloudCmd, tryToCatch}) => { + 'C - Create User Menu File': async ({DOM, CloudCmd}) => { const { RESTful, CurrentInfo, @@ -23,7 +23,7 @@ module.exports = { const {dirPath} = CurrentInfo; const path = `${dirPath}.cloudcmd.menu.js`; - const [e] = await tryToCatch(RESTful.write, path, data); + const [e] = await RESTful.write(path, data); if (e) return; diff --git a/client/modules/user-menu/default-menu.spec.js b/client/modules/user-menu/default-menu.spec.js index 731ab5f0..78514a2b 100644 --- a/client/modules/user-menu/default-menu.spec.js +++ b/client/modules/user-menu/default-menu.spec.js @@ -4,7 +4,6 @@ const test = require('supertape'); const stub = require('@cloudcmd/stub'); const wraptile = require('wraptile'); const defaultMenu = require('./default-menu'); -const tryToCatch = require('try-to-catch'); const {_data} = defaultMenu; const reject = wraptile(async (a) => { throw Error(a); @@ -19,7 +18,6 @@ test('cloudcmd: client: user menu: RESTful.write', async (t) => { await defaultMenu[name]({ DOM, CloudCmd, - tryToCatch, }); const path = '/.cloudcmd.menu.js'; @@ -36,7 +34,6 @@ test('cloudcmd: client: user menu: refresh', async (t) => { await defaultMenu[name]({ DOM, CloudCmd, - tryToCatch, }); t.ok(refresh.calledWith(), 'should call CloudCmd.refresh'); @@ -52,7 +49,6 @@ test('cloudcmd: client: user menu: setCurrentByName', async (t) => { await defaultMenu[name]({ DOM, CloudCmd, - tryToCatch, }); const fileName = '.cloudcmd.menu.js'; @@ -69,7 +65,6 @@ test('cloudcmd: client: user menu: EditFile.show', async (t) => { await defaultMenu[name]({ DOM, CloudCmd, - tryToCatch, }); t.ok(EditFile.show.called, 'should call EditFile.show'); @@ -88,7 +83,6 @@ test('cloudcmd: client: user menu: no EditFile.show', async (t) => { await defaultMenu[name]({ DOM, CloudCmd, - tryToCatch, }); t.notOk(EditFile.show.called, 'should not call EditFile.show'); diff --git a/client/modules/user-menu/index.js b/client/modules/user-menu/index.js index d76733b5..02d734e1 100644 --- a/client/modules/user-menu/index.js +++ b/client/modules/user-menu/index.js @@ -122,7 +122,6 @@ const runUserMenu = async (value, options, userMenu) => { const [e] = await tryToCatch(userMenu[value], { DOM, CloudCmd, - tryToCatch, }); if (e) diff --git a/common/try-to-promisify.js b/common/try-to-promisify.js index ab4860d2..0bb49b88 100644 --- a/common/try-to-promisify.js +++ b/common/try-to-promisify.js @@ -1,11 +1,11 @@ 'use strict'; const {promisify} = require('es6-promisify'); +const wraptile = require('wraptile/legacy'); const tryToCatch = require('try-to-catch/legacy'); -module.exports = (fn, ...args) => { +module.exports = wraptile((fn, ...args) => { const promise = promisify(fn); - return tryToCatch(promise, ...args); -}; +});