mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 02:35:49 +00:00
refactor(dom) RESTful: add tryToCatch wrapper
This commit is contained in:
parent
8ac3211a86
commit
7b6e767ccb
10 changed files with 40 additions and 51 deletions
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
});
|
||||
},
|
||||
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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});
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
|
|
|
|||
|
|
@ -122,7 +122,6 @@ const runUserMenu = async (value, options, userMenu) => {
|
|||
const [e] = await tryToCatch(userMenu[value], {
|
||||
DOM,
|
||||
CloudCmd,
|
||||
tryToCatch,
|
||||
});
|
||||
|
||||
if (e)
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
};
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue