From 83cf1b6990ae45375924f4d1242acde74cf6aa86 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Wed, 18 Sep 2019 17:03:36 +0300 Subject: [PATCH] feature(package) load.js v3.0.0 --- client/client.js | 7 ++----- client/cloudcmd.js | 6 ++---- client/dom/load-remote.js | 30 ++++++++++-------------------- client/load-module.js | 3 +-- client/modules/cloud.js | 5 ++--- client/modules/config/index.js | 2 +- client/modules/edit.js | 2 +- client/modules/konsole.js | 3 +-- client/modules/operation/index.js | 28 +++++++++++++--------------- client/modules/terminal-run.js | 4 +--- client/modules/terminal.js | 5 +---- client/modules/user-menu/index.js | 3 +-- client/modules/view.js | 3 +-- package.json | 2 +- 14 files changed, 38 insertions(+), 65 deletions(-) diff --git a/client/client.js b/client/client.js index 1a228e98..aaf0abf1 100644 --- a/client/client.js +++ b/client/client.js @@ -20,9 +20,6 @@ const currify = require('currify'); const noJS = (a) => a.replace(/.js$/, ''); -const loadCSS = promisify(load.css); -const loadJS = promisify(load.js); - const { apiURL, formatMsg, @@ -158,7 +155,7 @@ function CloudCmdProto(DOM) { CloudCmd.MIN_ONE_PANEL_WIDTH = Infinity; if (!document.body.scrollIntoViewIfNeeded) - await loadJS(prefix + CloudCmd.DIRCLIENT_MODULES + 'polyfill.js'); + await load.js(prefix + CloudCmd.DIRCLIENT_MODULES + 'polyfill.js'); await initModules(); await baseInit(); @@ -171,7 +168,7 @@ function CloudCmdProto(DOM) { const {prefix} = CloudCmd; const name = prefix + '/dist/cloudcmd.common.css'; - await loadCSS(name); + await load.css(name); } this.route = (path) => { diff --git a/client/cloudcmd.js b/client/cloudcmd.js index 53ef2391..a1bcdacb 100644 --- a/client/cloudcmd.js +++ b/client/cloudcmd.js @@ -5,7 +5,6 @@ require('../css/nojs.css'); require('../css/columns/name-size-date.css'); require('../css/columns/name-size.css'); -const {promisify} = require('es6-promisify'); const wraptile = require('wraptile'); const load = require('load.js'); @@ -55,10 +54,9 @@ const onUpdateFound = wraptile(async (config) => { const {DOM} = window; const prefix = getPrefix(config.prefix); - const js = promisify(load.js); - await js(`${prefix}/dist/cloudcmd.common.js`); - await js(`${prefix}/dist/cloudcmd.js`); + await load.js(`${prefix}/dist/cloudcmd.common.js`); + await load.js(`${prefix}/dist/cloudcmd.js`); console.log('cloudcmd: sw: updated'); diff --git a/client/dom/load-remote.js b/client/dom/load-remote.js index 52e66934..b4e0aa22 100644 --- a/client/dom/load-remote.js +++ b/client/dom/load-remote.js @@ -2,16 +2,14 @@ /* global CloudCmd */ -const exec = require('execon'); const rendy = require('rendy'); const itype = require('itype'); -const wraptile = require('wraptile'); const load = require('load.js'); +const tryToCatch = require('try-to-catch'); const {findObjByNameInArr} = require('../../common/util'); const Files = require('./files'); -const parallel = wraptile(load.parallel); module.exports = (name, options, callback = options) => { const {prefix, config} = CloudCmd; @@ -20,7 +18,7 @@ module.exports = (name, options, callback = options) => { if (o.name && window[o.name]) return callback(); - Files.get('modules').then((modules) => { + Files.get('modules').then(async (modules) => { const online = config('online') && navigator.onLine; const module = findObjByNameInArr(modules.remote, name); @@ -48,23 +46,15 @@ module.exports = (name, options, callback = options) => { }); }); - const on = funcON(localURL, remoteURL, callback); - const off = funcOFF(localURL, callback); + if (online) { + const [e] = await tryToCatch(load.parallel, remoteURL); + + if (!e) + return callback(); + } - exec.if(online, on, off); + const [e] = await tryToCatch(load.parallel, localURL); + callback(e); }); }; -function funcOFF(local, callback) { - return parallel(local, callback); -} - -function funcON (local, remote,callback) { - return parallel(remote, (error) => { - if (error) - return funcOFF(); - - callback(); - }); -} - diff --git a/client/load-module.js b/client/load-module.js index a60638b7..5111259c 100644 --- a/client/load-module.js +++ b/client/load-module.js @@ -4,8 +4,7 @@ const exec = require('execon'); const tryToCatch = require('try-to-catch'); -const {promisify} = require('es6-promisify'); -const loadJS = promisify(require('load.js').js); +const loadJS = require('load.js').js; const pascalCase = require('just-pascal-case'); const noJS = (a) => a.replace(/.js$/, ''); diff --git a/client/modules/cloud.js b/client/modules/cloud.js index af019f05..59c203c9 100644 --- a/client/modules/cloud.js +++ b/client/modules/cloud.js @@ -4,8 +4,7 @@ const exec = require('execon'); const currify = require('currify'); -const {promisify} = require('es6-promisify'); -const loadJS = promisify(require('load.js').js); +const load = require('load.js'); const {log} = CloudCmd; @@ -62,7 +61,7 @@ async function loadFiles() { return Promise.all([ Files.get('modules'), - loadJS(js), + load.js(js), ]); } diff --git a/client/modules/config/index.js b/client/modules/config/index.js index 75ec79b5..014ea2fe 100644 --- a/client/modules/config/index.js +++ b/client/modules/config/index.js @@ -44,7 +44,7 @@ const Config = {}; let Template; -const loadCSS = promisify(load.css); +const loadCSS = load.css; module.exports.init = async () => { if (!CloudCmd.config('configDialog')) diff --git a/client/modules/edit.js b/client/modules/edit.js index 3f89d39b..1807145b 100644 --- a/client/modules/edit.js +++ b/client/modules/edit.js @@ -6,7 +6,7 @@ const {promisify} = require('es6-promisify'); const tryToCatch = require('try-to-catch'); const createElement = require('@cloudcmd/create-element'); const load = require('load.js'); -const loadJS = promisify(load.js); +const loadJS = load.js; const {MAX_FILE_SIZE: maxSize} = require('../../common/cloudfunc'); const {time, timeEnd} = require('../../common/util'); diff --git a/client/modules/konsole.js b/client/modules/konsole.js index 5c973f84..e1fda7d5 100644 --- a/client/modules/konsole.js +++ b/client/modules/konsole.js @@ -8,10 +8,9 @@ CloudCmd.Konsole = exports; const exec = require('execon'); -const {promisify} = require('es6-promisify'); const currify = require('currify'); const tryToCatch = require('try-to-catch'); -const loadJS = promisify(require('load.js').js); +const loadJS = require('load.js').js; const createElement = require('@cloudcmd/create-element'); const Images = require('../dom/images'); diff --git a/client/modules/operation/index.js b/client/modules/operation/index.js index 09a15a99..fff15efb 100644 --- a/client/modules/operation/index.js +++ b/client/modules/operation/index.js @@ -9,7 +9,8 @@ const currify = require('currify'); const wraptile = require('wraptile'); const {promisify} = require('es6-promisify'); const exec = require('execon'); -const loadJS = require('load.js').js; +const load = require('load.js'); +const tryToCatch = require('try-to-catch'); const {encode} = require('../../../common/entity'); const callbackify = require('../../../common/callbackify'); @@ -60,7 +61,7 @@ module.exports.init = promisify((callback) => { exec.series([ DOM.loadSocket, - (callback) => { + async (callback) => { if (!config('progress') || config('dropbox')) return callback(); @@ -68,7 +69,9 @@ module.exports.init = promisify((callback) => { prefix, prefixSocket, } = CloudCmd; - load(initOperations(prefix, prefixSocket, callback)); + + await tryToCatch(loadAll, initOperations(prefix, prefixSocket)); + callback(); }, (callback) => { Loaded = true; @@ -514,21 +517,16 @@ async function prompt(msg, to, names) { return Dialog.prompt(msg, to); } -function load(callback) { +async function loadAll() { const {prefix} = CloudCmd; const file = `${prefix}/fileop/fileop.js`; - loadJS(file, (error) => { - if (error) { - Dialog.alert(error.message); - return exec(callback); - } - - Loaded = true; - Util.timeEnd(Name + ' load'); - exec(callback); - }); + const [error] = await tryToCatch(load.js, file); - Util.time(Name + ' load'); + if (error) + Dialog.alert(error.message); + + Loaded = true; + Util.timeEnd(Name + ' load'); } diff --git a/client/modules/terminal-run.js b/client/modules/terminal-run.js index eaadfe5f..5e0334ba 100644 --- a/client/modules/terminal-run.js +++ b/client/modules/terminal-run.js @@ -13,8 +13,6 @@ const load = require('load.js'); const DOM = require('../dom'); const Images = require('../dom/images'); -const loadParallel = promisify(load.parallel); - const {Dialog} = DOM; const { Key, @@ -36,7 +34,7 @@ const loadAll = async () => { const js = `${prefixGritty}/gritty.js`; const css = `${prefix}/dist/terminal.css`; - const [e] = await tryToCatch(loadParallel, [js, css]); + const [e] = await tryToCatch(load.parallel, [js, css]); if (e) { const src = e.target.src.replace(window.location.href, ''); diff --git a/client/modules/terminal.js b/client/modules/terminal.js index 20a85392..6ddc632c 100644 --- a/client/modules/terminal.js +++ b/client/modules/terminal.js @@ -1,8 +1,5 @@ 'use strict'; -/* global CloudCmd, gritty */ - -const {promisify} = require('es6-promisify'); const tryToCatch = require('try-to-catch'); require('../../css/terminal.css'); @@ -12,7 +9,7 @@ const load = require('load.js'); const DOM = require('../dom'); const Images = require('../dom/images'); -const loadParallel = promisify(load.parallel); +const loadParallel = load.parallel; const {Dialog} = DOM; const { diff --git a/client/modules/user-menu/index.js b/client/modules/user-menu/index.js index 27f61ad9..770762fc 100644 --- a/client/modules/user-menu/index.js +++ b/client/modules/user-menu/index.js @@ -6,7 +6,6 @@ require('../../../css/user-menu.css'); const currify = require('currify'); const wraptile = require('wraptile'); -const {promisify} = require('es6-promisify'); const fullstore = require('fullstore'); const load = require('load.js'); const createElement = require('@cloudcmd/create-element'); @@ -20,7 +19,7 @@ const Dialog = require('../../dom/dialog'); const getUserMenu = require('./get-user-menu'); const navigate = require('./navigate'); -const loadCSS = promisify(load.css); +const loadCSS = load.css; const sourceStore = fullstore(); const Name = 'UserMenu'; diff --git a/client/modules/view.js b/client/modules/view.js index ab7b5890..c5f6743d 100644 --- a/client/modules/view.js +++ b/client/modules/view.js @@ -8,7 +8,6 @@ const itype = require('itype'); const rendy = require('rendy'); const exec = require('execon'); const currify = require('currify'); -const {promisify} = require('es6-promisify'); const tryToCatch = require('try-to-catch'); const modal = require('@cloudcmd/modal'); @@ -31,7 +30,7 @@ const lifo = currify((fn, el, cb, name) => fn(name, el, cb)); const addEvent = lifo(Events.add); const getRegExp = (ext) => RegExp(`\\.${ext}$`, 'i'); -const loadCSS = promisify(load.css); +const loadCSS = load.css; module.exports.show = show; module.exports.hide = hide; diff --git a/package.json b/package.json index da8dc640..b4876a14 100644 --- a/package.json +++ b/package.json @@ -190,7 +190,7 @@ "inherits": "^2.0.3", "just-pascal-case": "^1.1.0", "limier": "^2.0.0", - "load.js": "^2.0.0", + "load.js": "^3.0.0", "madrun": "^3.0.1", "memfs": "^2.9.0", "minor": "^1.2.2",