From c01fb66baecc62b28f7c8a7cf901c8ce61b32dc6 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Sat, 5 Mar 2016 19:52:16 -0800 Subject: [PATCH 1/6] move renderer state into new file --- renderer/index.js | 61 +++++------------------------------------------ renderer/state.js | 53 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 55 deletions(-) create mode 100644 renderer/state.js diff --git a/renderer/index.js b/renderer/index.js index 0a692ed2..1e789ca0 100644 --- a/renderer/index.js +++ b/renderer/index.js @@ -9,6 +9,7 @@ var EventEmitter = require('events') var mainLoop = require('main-loop') var networkAddress = require('network-address') var path = require('path') +var state = require('./state') var throttle = require('throttleit') var torrentPoster = require('./lib/torrent-poster') var WebTorrent = require('webtorrent') @@ -30,68 +31,18 @@ global.WEBTORRENT_ANNOUNCE = createTorrent.announceList return url.indexOf('wss://') === 0 || url.indexOf('ws://') === 0 }) -var state = global.state = { - /* Temporary state disappears once the program exits. - * It can contain complex objects like open connections, etc. - */ - url: '/', - client: null, /* the WebTorrent client */ - server: null, /* local WebTorrent-to-HTTP server */ - dock: { - badge: 0, - progress: 0 - }, - devices: { - airplay: null, /* airplay client. finds and manages AppleTVs */ - chromecast: null /* chromecast client. finds and manages Chromecasts */ - }, - torrentPlaying: null, /* the torrent we're streaming. see client.torrents */ - // history: [], /* track how we got to the current view. enables Back button */ - // historyIndex: 0, - isFocused: true, - isFullScreen: false, - mainWindowBounds: null, /* x y width height */ - title: 'WebTorrent', /* current window title */ - video: { - isPaused: false, - currentTime: 0, /* seconds */ - duration: 1, /* seconds */ - mouseStationarySince: 0 /* Unix time in ms */ - }, - - /* Saved state is read from and written to ~/.webtorrent/state.json - * It should be simple and minimal and must be JSONifiable - */ - saved: { - torrents: [ - { - name: 'Elephants Dream', - torrentFile: 'resources/ElephantsDream_archive.torrent' - }, - { - name: 'Big Buck Bunny', - torrentFile: 'resources/BigBuckBunny_archive.torrent' - }, - { - name: 'Sintel', - torrentFile: 'resources/Sintel_archive.torrent' - }, - { - name: 'Tears of Steel', - torrentFile: 'resources/TearsOfSteel_archive.torrent' - } - ] - } -} - var client, vdomLoop, updateThrottled function init () { - client = global.client = new WebTorrent() + client = new WebTorrent() client.on('warning', onWarning) client.on('error', onError) state.client = client + // For easy debugging in Developer Tools + global.client = client + global.state = state + vdomLoop = mainLoop(state, render, { create: createElement, diff: diff, diff --git a/renderer/state.js b/renderer/state.js new file mode 100644 index 00000000..1715d8c9 --- /dev/null +++ b/renderer/state.js @@ -0,0 +1,53 @@ +module.exports = { + /* Temporary state disappears once the program exits. + * It can contain complex objects like open connections, etc. + */ + url: '/', + client: null, /* the WebTorrent client */ + server: null, /* local WebTorrent-to-HTTP server */ + dock: { + badge: 0, + progress: 0 + }, + devices: { + airplay: null, /* airplay client. finds and manages AppleTVs */ + chromecast: null /* chromecast client. finds and manages Chromecasts */ + }, + torrentPlaying: null, /* the torrent we're streaming. see client.torrents */ + // history: [], /* track how we got to the current view. enables Back button */ + // historyIndex: 0, + isFocused: true, + isFullScreen: false, + mainWindowBounds: null, /* x y width height */ + title: 'WebTorrent', /* current window title */ + video: { + isPaused: false, + currentTime: 0, /* seconds */ + duration: 1, /* seconds */ + mouseStationarySince: 0 /* Unix time in ms */ + }, + + /* Saved state is read from and written to ~/.webtorrent/state.json + * It should be simple and minimal and must be JSONifiable + */ + saved: { + torrents: [ + { + name: 'Elephants Dream', + torrentFile: 'resources/ElephantsDream_archive.torrent' + }, + { + name: 'Big Buck Bunny', + torrentFile: 'resources/BigBuckBunny_archive.torrent' + }, + { + name: 'Sintel', + torrentFile: 'resources/Sintel_archive.torrent' + }, + { + name: 'Tears of Steel', + torrentFile: 'resources/TearsOfSteel_archive.torrent' + } + ] + } +} From c7b49ae45b85f8b77dd440b23fa55de13df53a4e Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Sat, 5 Mar 2016 19:52:31 -0800 Subject: [PATCH 2/6] renderer: Use dispatch() code path even for ipc messages --- renderer/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/renderer/index.js b/renderer/index.js index 1e789ca0..691e23da 100644 --- a/renderer/index.js +++ b/renderer/index.js @@ -167,11 +167,11 @@ function dispatch (action, ...args) { } electron.ipcRenderer.on('addTorrent', function (e, torrentId) { - addTorrent(torrentId) + dispatch('addTorrent', torrentId) }) electron.ipcRenderer.on('seed', function (e, files) { - seed(files) + dispatch('seed', files) }) electron.ipcRenderer.on('fullscreenChanged', function (e, isFullScreen) { From 617f0bcf43a0d1e3831ad854bee214cbe1292606 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Sat, 5 Mar 2016 20:04:59 -0800 Subject: [PATCH 3/6] export object directly --- main/menu.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/main/menu.js b/main/menu.js index 3b4dd8e7..617ba158 100644 --- a/main/menu.js +++ b/main/menu.js @@ -1,5 +1,5 @@ -var electron = require('electron') var debug = require('debug')('webtorrent-app:menu') +var electron = require('electron') var windows = require('./windows') var app = electron.app @@ -285,12 +285,10 @@ function getMenuTemplate () { var appMenu = electron.Menu.buildFromTemplate(getMenuTemplate()) -var menu = { +module.exports = { appMenu: appMenu, onToggleFullScreen: onToggleFullScreen, onWindowHide: onWindowHide, onWindowShow: onWindowShow, toggleFullScreen: toggleFullScreen } - -module.exports = menu From db5de7d3ad9787cd073b06a42d53dd398cc37268 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Sat, 5 Mar 2016 20:07:51 -0800 Subject: [PATCH 4/6] one less global --- renderer/index.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/renderer/index.js b/renderer/index.js index 691e23da..f2709dc1 100644 --- a/renderer/index.js +++ b/renderer/index.js @@ -31,16 +31,14 @@ global.WEBTORRENT_ANNOUNCE = createTorrent.announceList return url.indexOf('wss://') === 0 || url.indexOf('ws://') === 0 }) -var client, vdomLoop, updateThrottled +var vdomLoop, updateThrottled function init () { - client = new WebTorrent() - client.on('warning', onWarning) - client.on('error', onError) - state.client = client + state.client = new WebTorrent() + state.client.on('warning', onWarning) + state.client.on('error', onError) // For easy debugging in Developer Tools - global.client = client global.state = state vdomLoop = mainLoop(state, render, { @@ -207,13 +205,13 @@ function isNotTorrentFile (file) { function addTorrent (torrentId) { if (!torrentId) torrentId = 'magnet:?xt=urn:btih:6a9759bffd5c0af65319979fb7832189f4f3c35d&dn=sintel.mp4' - var torrent = client.add(torrentId) + var torrent = state.client.add(torrentId) addTorrentEvents(torrent) } function seed (files) { if (files.length === 0) return - var torrent = client.seed(files) + var torrent = state.client.seed(files) addTorrentEvents(torrent) } From edf3d83e2e2d3a8e50d198067ebbd107ee3c40c7 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Sat, 5 Mar 2016 20:07:55 -0800 Subject: [PATCH 5/6] TODO --- renderer/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/renderer/index.js b/renderer/index.js index f2709dc1..54d777aa 100644 --- a/renderer/index.js +++ b/renderer/index.js @@ -302,6 +302,7 @@ function openAirplay (torrent) { } function setDimensions (dimensions) { + // TODO: eliminate blocking remote call state.mainWindowBounds = electron.remote.getCurrentWindow().getBounds() // Limit window size to screen size From 6664c92ea60dda9dfee41f77166d74a3fefd7937 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Sat, 5 Mar 2016 20:08:12 -0800 Subject: [PATCH 6/6] 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) {