feature(client) ajaxLoad: callback -> promise

This commit is contained in:
coderaiser 2020-04-04 13:35:11 +03:00
parent 0098fb9975
commit f69a6ea82b
2 changed files with 45 additions and 57 deletions

View file

@ -5,9 +5,7 @@
const Emitify = require('emitify'); const Emitify = require('emitify');
const inherits = require('inherits'); const inherits = require('inherits');
const rendy = require('rendy'); const rendy = require('rendy');
const exec = require('execon');
const load = require('load.js'); const load = require('load.js');
const {promisify} = require('es6-promisify');
const tryToCatch = require('try-to-catch'); const tryToCatch = require('try-to-catch');
const pascalCase = require('just-pascal-case'); const pascalCase = require('just-pascal-case');
@ -92,7 +90,7 @@ function CloudCmdProto(DOM) {
* } * }
* @param callback * @param callback
*/ */
this.loadDir = promisify((params, callback) => { this.loadDir = async (params) => {
const p = params; const p = params;
const refresh = p.isRefresh; const refresh = p.isRefresh;
@ -118,13 +116,13 @@ function CloudCmdProto(DOM) {
Images.show.load(imgPosition, panel); Images.show.load(imgPosition, panel);
/* загружаем содержимое каталога */ /* загружаем содержимое каталога */
ajaxLoad(p.path, { await ajaxLoad(p.path, {
refresh, refresh,
history, history,
noCurrent, noCurrent,
currentName, currentName,
}, panel, callback); }, panel);
}); };
/** /**
* Конструктор CloudClient, который * Конструктор CloudClient, который
@ -324,60 +322,49 @@ function CloudCmdProto(DOM) {
* @param callback * @param callback
* *
*/ */
async function ajaxLoad(path, options, panel, callback) { async function ajaxLoad(path, options = {}, panel) {
const {Dialog, RESTful} = DOM; const {RESTful} = DOM;
const create = async (error, json) => {
if (error)
return Dialog.alert(`Can't get from store: ${error.message}`);
const name = options.currentName || Info.name;
const isRefresh = options.refresh;
const {noCurrent} = options;
if (!isRefresh && json)
return await createFileTable(json, panel, options);
const position = DOM.getPanelPosition(panel);
const sort = CloudCmd.sort[position];
const order = CloudCmd.order[position];
const query = rendy('?sort={{ sort }}&order={{ order }}', {
sort,
order,
});
const [, newObj] = await RESTful.read(path + query, 'json');
if (!newObj)
return; // that's OK, error handled by RESTful
options.sort = sort;
options.order = order;
await createFileTable(newObj, panel, options);
if (isRefresh && !noCurrent)
DOM.setCurrentByName(name);
exec(callback);
if (!CloudCmd.config('dirStorage'))
return;
Storage.set(path, newObj);
};
if (!options)
options = {};
CloudCmd.log('reading dir: "' + path + '";'); CloudCmd.log('reading dir: "' + path + '";');
if (!CloudCmd.config('dirStorage')) const dirStorage = CloudCmd.config('dirStorage');
return create(); const json = dirStorage && await Storage.get(path);
const data = await Storage.get(path); const name = options.currentName || Info.name;
create(null, data); const {
noCurrent,
refresh,
} = options;
if (!refresh && json)
return await createFileTable(json, panel, options);
const position = DOM.getPanelPosition(panel);
const sort = CloudCmd.sort[position];
const order = CloudCmd.order[position];
const query = rendy('?sort={{ sort }}&order={{ order }}', {
sort,
order,
});
const [, newObj] = await RESTful.read(path + query, 'json');
if (!newObj)
return; // that's OK, error handled by RESTful
options.sort = sort;
options.order = order;
await createFileTable(newObj, panel, options);
if (refresh && !noCurrent)
DOM.setCurrentByName(name);
if (!CloudCmd.config('dirStorage'))
return;
Storage.set(path, newObj);
} }
/** /**

View file

@ -1,7 +1,8 @@
'use strict'; 'use strict';
/* global DOM */ /* global DOM */
/* global Info */
const Info = DOM.CurrentInfo;
/** /**
* Функция генерирует JSON из html-таблицы файлов и * Функция генерирует JSON из html-таблицы файлов и