mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 02:35:49 +00:00
feature(client) io: promisify -> async
This commit is contained in:
parent
807a1bc26d
commit
16a1eade2a
2 changed files with 46 additions and 91 deletions
130
client/dom/io.js
130
client/dom/io.js
|
|
@ -1,8 +1,7 @@
|
|||
'use strict';
|
||||
|
||||
/* global CloudCmd*/
|
||||
/* global CloudCmd */
|
||||
|
||||
const itype = require('itype');
|
||||
const {promisify} = require('es6-promisify');
|
||||
|
||||
const {FS} = require('../../common/cloudfunc');
|
||||
|
|
@ -23,161 +22,122 @@ function replaceHash(url) {
|
|||
return url.replace(/#/g, '%23');
|
||||
}
|
||||
|
||||
module.exports.delete = promisify((url, data, callback) => {
|
||||
const isFunc = itype.function(data);
|
||||
|
||||
if (!callback && isFunc) {
|
||||
callback = data;
|
||||
data = null;
|
||||
}
|
||||
|
||||
sendRequest({
|
||||
module.exports.delete = async (url, data) => {
|
||||
return await sendRequest({
|
||||
method : 'DELETE',
|
||||
url : FS + url,
|
||||
data,
|
||||
callback,
|
||||
imgPosition : {
|
||||
top: Boolean(data),
|
||||
},
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
module.exports.patch = promisify((url, data, callback) => {
|
||||
const isFunc = itype.function(data);
|
||||
|
||||
if (!callback && isFunc) {
|
||||
callback = data;
|
||||
data = null;
|
||||
}
|
||||
|
||||
sendRequest({
|
||||
module.exports.patch = async (url, data) => {
|
||||
return await sendRequest({
|
||||
method: 'PATCH',
|
||||
url: FS + url,
|
||||
data,
|
||||
callback,
|
||||
imgPosition,
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
module.exports.write = promisify((url, data, callback) => {
|
||||
const isFunc = itype.function(data);
|
||||
|
||||
if (!callback && isFunc) {
|
||||
callback = data;
|
||||
data = null;
|
||||
}
|
||||
|
||||
sendRequest({
|
||||
module.exports.write = async (url, data) => {
|
||||
return await sendRequest({
|
||||
method: 'PUT',
|
||||
url: FS + url,
|
||||
data,
|
||||
callback,
|
||||
imgPosition,
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
module.exports.read = promisify((url, dataType, callback) => {
|
||||
module.exports.read = async (url, dataType = 'text') => {
|
||||
const notLog = !url.includes('?');
|
||||
const isFunc = itype.function(dataType);
|
||||
|
||||
if (!callback && isFunc) {
|
||||
callback = dataType;
|
||||
dataType = 'text';
|
||||
}
|
||||
|
||||
sendRequest({
|
||||
return await sendRequest({
|
||||
method: 'GET',
|
||||
url: FS + url,
|
||||
callback,
|
||||
notLog,
|
||||
dataType,
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
module.exports.cp = promisify((data, callback) => {
|
||||
sendRequest({
|
||||
module.exports.cp = async (data) => {
|
||||
return await sendRequest({
|
||||
method: 'PUT',
|
||||
url: '/cp',
|
||||
data,
|
||||
callback,
|
||||
imgPosition,
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
module.exports.pack = promisify((data, callback) => {
|
||||
sendRequest({
|
||||
module.exports.pack = async (data) => {
|
||||
return await sendRequest({
|
||||
method: 'PUT',
|
||||
url: '/pack',
|
||||
data,
|
||||
callback,
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
module.exports.extract = promisify((data, callback) => {
|
||||
sendRequest({
|
||||
method : 'PUT',
|
||||
url : '/extract',
|
||||
module.exports.extract = async (data) => {
|
||||
return await sendRequest({
|
||||
method: 'PUT',
|
||||
url: '/extract',
|
||||
data,
|
||||
callback,
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
module.exports.mv = promisify((data, callback) => {
|
||||
sendRequest({
|
||||
method : 'PUT',
|
||||
url : '/mv',
|
||||
module.exports.mv = async (data) => {
|
||||
return await sendRequest({
|
||||
method: 'PUT',
|
||||
url: '/mv',
|
||||
data,
|
||||
callback,
|
||||
imgPosition,
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
module.exports.Config = {
|
||||
read: promisify((callback) => {
|
||||
sendRequest({
|
||||
read: async () => {
|
||||
return await sendRequest({
|
||||
method: 'GET',
|
||||
url: '/config',
|
||||
callback,
|
||||
imgPosition,
|
||||
notLog: true,
|
||||
});
|
||||
}),
|
||||
},
|
||||
|
||||
write: promisify((data, callback) => {
|
||||
sendRequest({
|
||||
write: async (data) => {
|
||||
return await sendRequest({
|
||||
method: 'PATCH',
|
||||
url: '/config',
|
||||
data,
|
||||
callback,
|
||||
imgPosition,
|
||||
});
|
||||
}),
|
||||
},
|
||||
};
|
||||
|
||||
module.exports.Markdown = {
|
||||
read: promisify((url, callback) => {
|
||||
sendRequest({
|
||||
read: async (url) => {
|
||||
return await sendRequest({
|
||||
method: 'GET',
|
||||
url: '/markdown' + url,
|
||||
callback,
|
||||
imgPosition,
|
||||
notLog: true,
|
||||
});
|
||||
}),
|
||||
},
|
||||
|
||||
render: promisify((data, callback) => {
|
||||
sendRequest({
|
||||
render: async (data) => {
|
||||
return await sendRequest({
|
||||
method: 'PUT',
|
||||
url: '/markdown',
|
||||
data,
|
||||
callback,
|
||||
imgPosition,
|
||||
notLog: true,
|
||||
});
|
||||
}),
|
||||
},
|
||||
};
|
||||
|
||||
function sendRequest(params) {
|
||||
const sendRequest = promisify((params, callback) => {
|
||||
const p = params;
|
||||
const {prefixURL} = CloudCmd;
|
||||
|
||||
|
|
@ -201,7 +161,7 @@ function sendRequest(params) {
|
|||
|
||||
const text = status === 404 ? response : statusText;
|
||||
|
||||
p.callback(Error(text));
|
||||
callback(Error(text));
|
||||
},
|
||||
success: (data) => {
|
||||
Images.hide();
|
||||
|
|
@ -209,8 +169,8 @@ function sendRequest(params) {
|
|||
if (!p.notLog)
|
||||
CloudCmd.log(data);
|
||||
|
||||
p.callback(null, data);
|
||||
callback(null, data);
|
||||
},
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -2,9 +2,6 @@
|
|||
|
||||
/* global CloudCmd */
|
||||
|
||||
const {promisify} = require('es6-promisify');
|
||||
const tryToCatch = require('try-to-catch');
|
||||
|
||||
CloudCmd.Markdown = exports;
|
||||
|
||||
const createElement = require('@cloudcmd/create-element');
|
||||
|
|
@ -13,8 +10,6 @@ const Images = require('../dom/images');
|
|||
const {Markdown} = require('../dom/rest');
|
||||
const {alert} = require('../dom/dialog');
|
||||
|
||||
const read = promisify(Markdown.read);
|
||||
|
||||
module.exports.init = async () => {
|
||||
Images.show.load('top');
|
||||
await CloudCmd.View();
|
||||
|
|
@ -38,7 +33,7 @@ async function show(name, options = {}) {
|
|||
if (relative)
|
||||
name += relativeQuery;
|
||||
|
||||
const [error, innerHTML] = await tryToCatch(read, name);
|
||||
const [error, innerHTML] = await Markdown.read(name);
|
||||
Images.hide();
|
||||
|
||||
if (error)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue