From 16a1eade2a993903296dc0cc010fd57e387bb9b5 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Wed, 5 Aug 2020 17:53:40 +0300 Subject: [PATCH] feature(client) io: promisify -> async --- client/dom/io.js | 130 +++++++++++++------------------------ client/modules/markdown.js | 7 +- 2 files changed, 46 insertions(+), 91 deletions(-) diff --git a/client/dom/io.js b/client/dom/io.js index cac32215..bd311192 100644 --- a/client/dom/io.js +++ b/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); }, }); -} +}); diff --git a/client/modules/markdown.js b/client/modules/markdown.js index 523814c7..e5810300 100644 --- a/client/modules/markdown.js +++ b/client/modules/markdown.js @@ -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)