From: moshee Date: Tue, 18 Nov 2014 23:34:46 +0000 (-0800) Subject: Make use of editor transactions. Resolve #5. X-Git-Tag: v0.3.2~1 X-Git-Url: https://arthur.ath.cx/gitweb/?a=commitdiff_plain;h=f312e36dccb4e9e0544a9d6ad5270bbaad76be7f;p=atom-ax-pipe.git Make use of editor transactions. Resolve #5. --- diff --git a/lib/pipe.coffee b/lib/pipe.coffee index 5294840..3781a56 100644 --- a/lib/pipe.coffee +++ b/lib/pipe.coffee @@ -26,13 +26,19 @@ module.exports = commandString = "cd '#{atom.project.rootDirectory.path}' && #{commandString}" properties = { reversed: true, invalidate: 'never' } - for range in editor.getSelectedBufferRanges() - marker = editor.markBufferRange range, properties - processRange marker, editor, commandString + ranges = editor.getSelectedBufferRanges() + wg = new WaitGroup -> + editor.commitTransaction() + view.focus() - view.focus() + wg.add(ranges.length) + + editor.beginTransaction() + for range, i in ranges + marker = editor.markBufferRange range, properties + processRange marker, editor, commandString, wg -processRange = (marker, editor, commandString) -> +processRange = (marker, editor, commandString, wg) -> stdout = '' stderr = '' @@ -47,6 +53,20 @@ processRange = (marker, editor, commandString) -> proc.on 'close', (code) -> text = stderr || stdout editor.setTextInBufferRange(marker.getBufferRange(), text) + wg.done() proc.stdin.write(editor.getTextInBufferRange(marker.getBufferRange())) proc.stdin.end() + +class WaitGroup + constructor: (cb) -> + @n = 0 + @cb = cb + + add: (n) -> + @n += n + + done: -> + @n -= 1 + if @n <= 0 + @cb()