From 009628c3ee0f601d3c259eb1d1d9b6d0d6830036 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Mon, 1 Apr 2019 15:29:14 +0300 Subject: [PATCH] feature(cloudcmd) improved progress of file operations --- client/modules/operation/format.js | 9 +++ client/modules/operation/index.js | 88 ++++++++++++++++++----- client/modules/operation/set-listeners.js | 49 +++++++++---- package.json | 2 +- 4 files changed, 116 insertions(+), 32 deletions(-) create mode 100644 client/modules/operation/format.js diff --git a/client/modules/operation/format.js b/client/modules/operation/format.js new file mode 100644 index 00000000..373d069c --- /dev/null +++ b/client/modules/operation/format.js @@ -0,0 +1,9 @@ +'use strict'; + +module.exports = (operation, from, to) => { + if (!to) + return `${operation} ${from}`; + + return `${operation} ${from} -> ${to}`; +}; + diff --git a/client/modules/operation/index.js b/client/modules/operation/index.js index 989e6017..bd9bb79a 100644 --- a/client/modules/operation/index.js +++ b/client/modules/operation/index.js @@ -103,35 +103,87 @@ function _initOperations(prefix, socketPrefix, fn) { } function onConnect(operator) { - packTarFn = (data, callback) => { - operator.tar(data.from, data.to, data.names) - .then(setListeners({noContinue: true}, callback)); + packTarFn = ({from, to, names}, callback) => { + const operation = 'Tar'; + const listen = setListeners({ + operation, + callback, + noContinue: true, + from, + to, + }); + + operator.tar(from, to, names) + .then(listen); }; - packZipFn = (data, callback) => { - operator.zip(data.from, data.to, data.names) - .then(setListeners({noContinue: true}, callback)); + packZipFn = ({from, to, names}, callback) => { + const operation = 'Zip'; + const listen = setListeners({ + operation, + callback, + noContinue: true, + from, + to, + }); + + operator.zip(from, to, names) + .then(listen); }; deleteFn = (from, files, callback) => { from = removeQuery(from); + + const operation = 'Delete'; + const listen = setListeners({ + operation, + callback, + from, + }); + operator.remove(from, files) - .then(setListeners(callback)); + .then(listen); }; - copyFn = (data, callback) => { - operator.copy(data.from, data.to, data.names) - .then(setListeners(callback)); + copyFn = ({from, to, names}, callback) => { + const operation = 'Copy'; + const listen = setListeners({ + operation, + callback, + from, + to, + names, + }); + + operator.copy(from, to, names) + .then(listen); }; - moveFn = (data, callback) => { - operator.move(data.from, data.to, data.names) - .then(setListeners(callback)); + moveFn = ({from, to, names}, callback) => { + const operation = 'Move'; + const listen = setListeners({ + operation, + callback, + from, + to, + }); + + operator.move(from, to, names) + .then(listen); }; - extractFn = (data, callback) => { - operator.extract(data.from, data.to) - .then(setListeners({noContinue: true}, callback)); + extractFn = ({from, to}, callback) => { + const operation = 'Extract'; + const listen = setListeners({ + operation, + callback, + noContinue: true, + from, + to, + }); + + operator.extract(from, to) + .then(listen); }; } @@ -393,8 +445,6 @@ function twopack(operation, type) { let fileFrom; let currentName = Info.name; - const {Images} = DOM; - const { path, dirPath, @@ -437,7 +487,7 @@ function twopack(operation, type) { break; } - Images.show.load('top'); + showLoad(); op(fileFrom, (error) => { !error && CloudCmd.refresh({ diff --git a/client/modules/operation/set-listeners.js b/client/modules/operation/set-listeners.js index 1f29b8ae..d5538c2a 100644 --- a/client/modules/operation/set-listeners.js +++ b/client/modules/operation/set-listeners.js @@ -3,37 +3,62 @@ /* global DOM */ /* global CloudCmd */ +const smalltalk = require('smalltalk'); + const { - Images, Dialog, + Images, } = DOM; const forEachKey = require('for-each-key/legacy'); +const wraptile = require('wraptile/legacy'); const {TITLE} = CloudCmd; -module.exports = (options, callback) => (emitter) => { - if (!callback) { - callback = options; - options = {}; - } +const format = require('./format'); + +module.exports = (options) => (emitter) => { + const { + operation, + callback, + noContinue, + from, + to, + } = options; let done; let lastError; + const onAbort = wraptile(({emitter, operation}) => { + emitter.abort(); + + const msg = `${operation} aborted`; + lastError = true; + + Dialog.alert(TITLE, msg, { + noCancel: true, + }); + }); + const removeListener = emitter.removeListener.bind(emitter); const on = emitter.on.bind(emitter); + const message = format(operation, from, to); + + const progress = smalltalk.progress(TITLE, message); + + progress.catch(onAbort({ + emitter, + operation, + })); + const listeners = { progress: (value) => { done = value === 100; - Images.setProgress(value); + progress.setProgress(value); }, end: () => { - Images - .hide() - .clearProgress(); - + Images.hide(); forEachKey(removeListener, listeners); if (lastError || done) @@ -43,7 +68,7 @@ module.exports = (options, callback) => (emitter) => { error: (error) => { lastError = error; - if (options.noContinue) { + if (noContinue) { listeners.end(error); Dialog.alert(TITLE, error); return; diff --git a/package.json b/package.json index 1cc289e8..88fec9ec 100644 --- a/package.json +++ b/package.json @@ -188,7 +188,7 @@ "scroll-into-view-if-needed": "^2.2.5", "serve-once": "^1.5.0", "serviceworker-webpack-plugin": "^1.0.1", - "smalltalk": "^3.1.0", + "smalltalk": "^3.3.0", "style-loader": "^0.23.0", "stylelint": "^9.0.0", "stylelint-config-standard": "^18.0.0",