From 25bfbf24e1d9c7aa4b686d3f903cdb57f31c2c30 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Mon, 8 Jun 2020 03:28:43 -0500 Subject: [PATCH 1/3] decaffeinate lib/misc --- lib/misc.coffee | 70 -------------------------------------- lib/misc.js | 90 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 70 deletions(-) delete mode 100644 lib/misc.coffee create mode 100644 lib/misc.js diff --git a/lib/misc.coffee b/lib/misc.coffee deleted file mode 100644 index d39813ee..00000000 --- a/lib/misc.coffee +++ /dev/null @@ -1,70 +0,0 @@ -{debounce} = require 'underscore-plus' - -module.exports = - paths: require './misc/paths' - blocks: require './misc/blocks' - cells: require './misc/cells' - words: require './misc/words' - weave: require './misc/weave' - colors: require './misc/colors' - scopes: require './misc/scopes' - - bufferLines: (t, f) -> - if not f? then [t, f] = [null, t] - buffer = [''] - flush = if not t? then -> else debounce (-> - if buffer[0] isnt '' - f buffer[0], false - buffer[0] = ''), t - (data) -> - lines = data.toString().split '\n' - buffer[0] += lines.shift() - buffer.push lines... - while buffer.length > 1 - f buffer.shift(), true - flush() - - time: (desc, p) -> - s = -> new Date().getTime()/1000 - t = s() - p.then -> console.log "#{desc}: #{(s()-t).toFixed(2)}s" - .catch -> - p - - hook: (obj, method, f) -> - souper = obj[method].bind obj - obj[method] = (a...) -> f souper, a... - - once: (f) -> - done = false - (args...) -> - return if done - done = true - f.call @, args... - - mutex: -> - wait = Promise.resolve() - (f) -> - current = wait - release = null - wait = new Promise((resolve) -> release = resolve).catch -> - current.then => f.call @, release - - exclusive: (f) -> - lock = module.exports.mutex() - (args...) -> - lock (release) => - result = f.call @, args... - release result - result - - # takes a time period in seconds and formats it as hh:mm:ss - formatTimePeriod: (dt) -> - return unless dt > 1 - h = Math.floor dt/(60*60) - m = Math.floor (dt -= h*60*60)/60 - s = Math.round (dt - m*60) - parts = [h, m, s] - for i, dt of parts - parts[i] = if dt < 10 then "0#{dt}" else "#{dt}" - parts.join ':' diff --git a/lib/misc.js b/lib/misc.js new file mode 100644 index 00000000..41bba765 --- /dev/null +++ b/lib/misc.js @@ -0,0 +1,90 @@ +'use babel' +// TODO use underscore +import { debounce } from 'underscore-plus'; + +export default { + // TODO Fix all of these dynamic requires and circular dependencies + paths: require('./misc/paths'), + blocks: require('./misc/blocks'), + cells: require('./misc/cells'), + words: require('./misc/words'), + weave: require('./misc/weave'), + colors: require('./misc/colors'), + scopes: require('./misc/scopes'), + + bufferLines(t, f) { + if (!f) { [t, f] = [null, t]; } + const buffer = ['']; + const flush = (t == null) ? ()=>{} : debounce(( () => { + if (buffer[0] !== '') { + f(buffer[0], false); + buffer[0] = ''; + }}), t); + return function(data) { + const lines = data.toString().split('\n'); + buffer[0] += lines.shift(); + buffer.push(...lines); + while (buffer.length > 1) { + f(buffer.shift(), true); + } + flush(); + }; + }, + + time(desc, p) { + const s = () => new Date().getTime()/1000; + const t = s(); + p.then(() => console.log(`${desc}: ${(s()-t).toFixed(2)}s`)) + .catch(()=>{}); + return p; + }, + + hook(obj, method, f) { + const souper = obj[method].bind(obj); + return obj[method] = (...a) => f(souper, ...a); + }, + + once(f) { + let done = false; + return function(...args) { + if (done) { return; } + done = true; + return f.call(this, ...args); + }; + }, + + mutex() { + let wait = Promise.resolve(); + return function(f) { + const current = wait; + let release = null; + wait = new Promise(resolve => release = resolve).catch(function() {}); + return current.then(() => f.call(this, release)); + }; + }, + + exclusive(f) { + const lock = module.exports.mutex(); + return function(...args) { + return lock(release => { + const result = f.call(this, ...args); + release(result); + return result; + }); + }; + }, + + // takes a time period in seconds and formats it as hh:mm:ss + formatTimePeriod(dt) { + if (dt <= 1) { return; } + const h = Math.floor(dt/(60*60)); + const m = Math.floor((dt -= h*60*60)/60); + const s = Math.round((dt - (m*60))); + const parts = [h, m, s]; + for (let i in parts) { + dt = parts[i]; + parts[i] = dt < 10 ? `0${dt}` : `${dt}`; + } + return parts.join(':'); + } +}; From 28b90c959d047569298da829cd48de8ed7caa9e3 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Mon, 8 Jun 2020 03:56:47 -0500 Subject: [PATCH 2/3] fix dynamic requires --- lib/misc.js | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/misc.js b/lib/misc.js index 41bba765..34f2f3b7 100644 --- a/lib/misc.js +++ b/lib/misc.js @@ -2,15 +2,25 @@ // TODO use underscore import { debounce } from 'underscore-plus'; +// TODO use babel to export ... from ... +import * as paths from './misc/paths' +import * as blocks from './misc/blocks' +import * as cells from './misc/cells' +import * as words from './misc/words' +import * as weave from './misc/weave' +import * as colors from './misc/colors' +import * as scopes from './misc/scopes' + export default { - // TODO Fix all of these dynamic requires and circular dependencies - paths: require('./misc/paths'), - blocks: require('./misc/blocks'), - cells: require('./misc/cells'), - words: require('./misc/words'), - weave: require('./misc/weave'), - colors: require('./misc/colors'), - scopes: require('./misc/scopes'), + // TODO remove these from the export default and export them directly (prevents expensive copy) + // TODO don't use this.message use message directly (prevents expensive copy) + paths: paths, + blocks: blocks, + cells: cells, + words: words, + weave: weave, + colors: colors, + scopes: scopes, bufferLines(t, f) { if (!f) { [t, f] = [null, t]; } From 9579f54adb3d30a27906a45e3b4403b8c2ce4c6a Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Thu, 11 Jun 2020 17:53:04 -0500 Subject: [PATCH 3/3] named export These all use named import (Except julia-client.coffee which is updated in other PR) File misc.js Found usages (14 usages found) Unclassified usage (5 usages found) lib\connection\process (2 usages found) basic.js (1 usage found) 6 import { paths, mutex } from '../../misc' remote.js (1 usage found) 5 import { paths, mutex } from '../../misc' lib\runtime (3 usages found) console.js (1 usage found) 6 import { paths } from '../misc' debugger.js (1 usage found) 8 import { blocks, cells, paths } from '../misc' frontend.js (1 usage found) 5 import { colors } from '../misc' Usage in string literals (9 usages found) lib (2 usages found) connection.coffee (1 usage found) 1 {time} = require './misc' julia-client.coffee (1 usage found) 19 misc: require './misc' lib\connection (3 usages found) ipc.coffee (1 usage found) 5 {bufferLines} = require '../misc' local.coffee (1 usage found) 1 {paths} = require '../misc' terminal.coffee (1 usage found) 6 {paths} = require '../misc' lib\connection\process (1 usage found) server.coffee (1 usage found) 7 {exclusive} = require '../../misc' lib\runtime (1 usage found) evaluation.coffee (1 usage found) 7 {paths, blocks, cells, words, weave} = require '../misc' lib\ui (2 usages found) progress.coffee (1 usage found) 3 {formatTimePeriod} = require '../misc' views.coffee (1 usage found) 4 {once} = require '../misc' --- lib/misc.js | 50 +++++++++++++++++++++++--------------------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/lib/misc.js b/lib/misc.js index 34f2f3b7..9ab55be2 100644 --- a/lib/misc.js +++ b/lib/misc.js @@ -11,18 +11,7 @@ import * as weave from './misc/weave' import * as colors from './misc/colors' import * as scopes from './misc/scopes' -export default { - // TODO remove these from the export default and export them directly (prevents expensive copy) - // TODO don't use this.message use message directly (prevents expensive copy) - paths: paths, - blocks: blocks, - cells: cells, - words: words, - weave: weave, - colors: colors, - scopes: scopes, - - bufferLines(t, f) { +export function bufferLines(t, f) { if (!f) { [t, f] = [null, t]; } const buffer = ['']; const flush = (t == null) ? ()=>{} : debounce(( () => { @@ -39,31 +28,31 @@ export default { } flush(); }; - }, +} - time(desc, p) { +export function time(desc, p) { const s = () => new Date().getTime()/1000; const t = s(); p.then(() => console.log(`${desc}: ${(s()-t).toFixed(2)}s`)) .catch(()=>{}); return p; - }, +} - hook(obj, method, f) { +export function hook(obj, method, f) { const souper = obj[method].bind(obj); return obj[method] = (...a) => f(souper, ...a); - }, +} - once(f) { +export function once(f) { let done = false; return function(...args) { if (done) { return; } done = true; return f.call(this, ...args); }; - }, +} - mutex() { +export function mutex() { let wait = Promise.resolve(); return function(f) { const current = wait; @@ -71,9 +60,9 @@ export default { wait = new Promise(resolve => release = resolve).catch(function() {}); return current.then(() => f.call(this, release)); }; - }, +} - exclusive(f) { +export function exclusive(f) { const lock = module.exports.mutex(); return function(...args) { return lock(release => { @@ -82,10 +71,10 @@ export default { return result; }); }; - }, +} - // takes a time period in seconds and formats it as hh:mm:ss - formatTimePeriod(dt) { +// takes a time period in seconds and formats it as hh:mm:ss +export function formatTimePeriod(dt) { if (dt <= 1) { return; } const h = Math.floor(dt/(60*60)); const m = Math.floor((dt -= h*60*60)/60); @@ -96,5 +85,12 @@ export default { parts[i] = dt < 10 ? `0${dt}` : `${dt}`; } return parts.join(':'); - } -}; +} + +exports.paths = paths +exports.blocks = blocks +exports.cells = cells +exports.words = words +exports.weave = weave +exports.colors = colors +exports.scopes = scopes