refactor(dom) RESTful: add tryToCatch wrapper

This commit is contained in:
coderaiser 2019-05-22 21:14:11 +03:00
parent 8ac3211a86
commit 7b6e767ccb
10 changed files with 40 additions and 51 deletions

View file

@ -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;

View file

@ -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;

View file

@ -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,
});
},

View file

@ -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');

View file

@ -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) {

View file

@ -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});
});
}

View file

@ -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;

View file

@ -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');

View file

@ -122,7 +122,6 @@ const runUserMenu = async (value, options, userMenu) => {
const [e] = await tryToCatch(userMenu[value], {
DOM,
CloudCmd,
tryToCatch,
});
if (e)

View file

@ -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);
};
});