From 6664c92ea60dda9dfee41f77166d74a3fefd7937 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Sat, 5 Mar 2016 20:08:12 -0800 Subject: [PATCH] refactor torrent event handling --- renderer/index.js | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/renderer/index.js b/renderer/index.js index 54d777aa..e3d3838b 100644 --- a/renderer/index.js +++ b/renderer/index.js @@ -217,28 +217,30 @@ function seed (files) { function addTorrentEvents (torrent) { torrent.on('infoHash', update) - torrent.on('done', function () { + torrent.on('download', updateThrottled) + torrent.on('upload', updateThrottled) + + torrent.on('ready', torrentReady) + torrent.on('done', torrentDone) + + update() + + function torrentReady () { + torrentPoster(torrent, function (err, buf) { + if (err) return onWarning(err) + torrent.posterURL = URL.createObjectURL(new Blob([ buf ], { type: 'image/png' })) + update() + }) + update() + } + + function torrentDone () { if (!state.isFocused) { state.dock.badge += 1 electron.ipcRenderer.send('setBadge', state.dock.badge) } update() - }) - torrent.on('download', updateThrottled) - torrent.on('upload', updateThrottled) - torrent.on('ready', function () { - torrentReady(torrent) - }) - update() -} - -function torrentReady (torrent) { - torrentPoster(torrent, function (err, buf) { - if (err) return onWarning(err) - torrent.posterURL = URL.createObjectURL(new Blob([ buf ], { type: 'image/png' })) - update() - }) - update() + } } function startServer (torrent, cb) {