From a3d2467767245df0f3fb40f2ebfdbc421003b807 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Thu, 23 Mar 2017 16:13:22 -0700 Subject: [PATCH] Torrent viewer: Add 'Stop Download' button Fixes #6768 --- .../torrent/locales/en-US/app.properties | 1 + js/webtorrent/components/torrentViewer.js | 47 ++++++++++++------ js/webtorrent/entry.js | 48 ++++++++++++++----- 3 files changed, 69 insertions(+), 27 deletions(-) diff --git a/app/extensions/torrent/locales/en-US/app.properties b/app/extensions/torrent/locales/en-US/app.properties index c714874277a..c1ceab0fb0b 100644 --- a/app/extensions/torrent/locales/en-US/app.properties +++ b/app/extensions/torrent/locales/en-US/app.properties @@ -20,3 +20,4 @@ torrentLoadingInfo=Loading torrent info... torrentLoadingMedia=Loading... copyMagnetLink=Copy Magnet Link webtorrentPage=WebTorrent +stopDownload=Stop Download diff --git a/js/webtorrent/components/torrentViewer.js b/js/webtorrent/components/torrentViewer.js index bb829c365eb..a3e08baece0 100644 --- a/js/webtorrent/components/torrentViewer.js +++ b/js/webtorrent/components/torrentViewer.js @@ -22,7 +22,7 @@ class TorrentViewer extends React.Component { dispatch } = this.props - let titleElem, mainButtonId, saveButton + let titleElem, mainButton, saveButton, legalNotice if (torrent) { if (name) { @@ -30,9 +30,28 @@ class TorrentViewer extends React.Component { titleElem =
{name}
} else { // 'Loading torrent information...' - titleElem =
+ titleElem = ( +
+ ) } - mainButtonId = torrent.progress < 1 ? 'downloading' : 'seeding' + mainButton = ( +
@@ -90,6 +108,7 @@ class TorrentViewer extends React.Component { serverUrl={serverUrl} stateOwner={this} /> + {legalNotice} diff --git a/js/webtorrent/entry.js b/js/webtorrent/entry.js index 495062bc9dc..71ec371b2b3 100644 --- a/js/webtorrent/entry.js +++ b/js/webtorrent/entry.js @@ -54,16 +54,16 @@ function init () { return onError(new Error('Invalid torrent identifier')) } - // Create the client, set up IPC to the WebTorrentRemoteServer - client = new WebTorrentRemoteClient(send) - client.on('warning', onWarning) - client.on('error', onError) - + // Setup IPC to webtorrent-remote server ipc.on(messages.TORRENT_MESSAGE, function (e, msg) { - client.receive(msg) + if (client) client.receive(msg) }) - // Check whether we're already part of this swarm. If not, show a Start button. + // Create a webtorrent-remote client + initClient() + + // Check whether torrent is already added by another webtorrent-remote client. + // If it's not, a 'Start Download' button will be shown. client.get(store.torrentId, function (err, torrent) { if (!err) initTorrent(torrent) update() @@ -73,9 +73,7 @@ function init () { // before the page is closed. But that's okay; webtorrent-remote expects regular // heartbeats and assumes clients are dead after 30s without one. So basically, // this is an optimization to destroy the client sooner. - window.addEventListener('beforeunload', function () { - client.destroy() - }) + window.addEventListener('beforeunload', destroyClient) // Update the UI (to show download speed) every 1s. update() @@ -102,9 +100,20 @@ function update () { document.title = store.name || 'WebTorrent' } -function onServerListening () { - store.serverUrl = 'http://localhost:' + server.address().port - update() +function initClient () { + if (client) return + client = new WebTorrentRemoteClient(send) + client.on('warning', onWarning) + client.on('error', onError) +} + +function destroyClient () { + client.destroy() + client.removeListener('warning', onWarning) + client.removeListener('error', onError) + client = null + store.torrent = null + store.serverUrl = null } function initTorrent (torrent) { @@ -129,10 +138,17 @@ function initTorrent (torrent) { torrent.on('done', update) } +function onServerListening () { + store.serverUrl = 'http://localhost:' + server.address().port + update() +} + function dispatch (action) { switch (action) { case 'start': return start() + case 'stop': + return stop() case 'saveTorrentFile': return saveTorrentFile() case 'copyMagnetLink': @@ -143,6 +159,7 @@ function dispatch (action) { } function start () { + initClient() client.add(store.torrentId, function (err, torrent) { if (err) return onError(err) initTorrent(torrent) @@ -150,6 +167,11 @@ function start () { }) } +function stop () { + destroyClient() + update() +} + function saveTorrentFile () { let a = document.createElement('a') a.target = '_blank'