WebTorrent process

* Separate hidden window, with its own renderer process, for WebTorrent
  (Must be a window. You cannot run WebRTC at all in a Web Worker, and you can't
   run it well in a node process like the electron main process.)

* Disabled the create-torrent-modal for now. That gives us a consistent UX
  regardless of whether the user dragged files or folders onto the app or opened
  the Create New Torrent menu item.

* Main process routes all messages between the main and webtorrent windows.

* The renderer index.js is smaller now (but still too big), with the WebTorrent
  interface moved to webtorrent.js / it's own process.

* The UI should be faster now, and should not lag under load.
This commit is contained in:
DC
2016-04-04 08:43:27 -07:00
committed by Feross Aboukhadijeh
parent 38ce25592f
commit db9e3e90c5
12 changed files with 726 additions and 427 deletions

View File

@@ -54,6 +54,7 @@ function init () {
app.on('ready', function () {
menu.init()
windows.createMainWindow()
windows.createWebTorrentHiddenWindow()
shortcuts.init()
tray.init()
handlers.install()

View File

@@ -69,6 +69,28 @@ function init () {
menu.onPlayerClose()
shortcuts.unregisterPlayerShortcuts()
})
// Capture all events
var oldEmit = ipcMain.emit
ipcMain.emit = function (name, e, ...args) {
// Relay messages between the main window and the WebTorrent hidden window
if (name.startsWith('wt-')) {
var recipient, recipientStr
if (e.sender.browserWindowOptions.title === 'webtorrent-hidden-window') {
recipient = windows.main
recipientStr = 'main'
} else {
recipient = windows.webtorrent
recipientStr = 'webtorrent'
}
console.log('sending %s to %s', name, recipientStr)
recipient.send(name, ...args)
return
}
// Emit all other events normally
oldEmit.call(ipcMain, name, e, ...args)
}
}
function setBounds (bounds, maximize) {

View File

@@ -124,7 +124,10 @@ function showCreateTorrent () {
properties: [ 'openFile', 'openDirectory' ]
}, function (filenames) {
if (!Array.isArray(filenames)) return
windows.main.send('dispatch', 'seed', filenames[0])
var options = {
files: filenames[0]
}
windows.main.send('dispatch', 'createTorrent', options)
})
}

View File

@@ -1,9 +1,10 @@
var windows = module.exports = {
about: null,
main: null,
createAboutWindow: createAboutWindow,
createMainWindow: createMainWindow,
focusWindow: focusWindow
createAboutWindow,
createWebTorrentHiddenWindow,
createMainWindow,
focusWindow
}
var electron = require('electron')
@@ -46,6 +47,25 @@ function createAboutWindow () {
})
}
function createWebTorrentHiddenWindow () {
var win = windows.webtorrent = new electron.BrowserWindow({
backgroundColor: '#ECECEC',
show: false,
center: true,
title: 'webtorrent-hidden-window',
width: 500,
height: 500,
minimizable: false,
maximizable: false,
fullscreen: false,
skipTaskbar: true
})
win.loadURL(config.WINDOW_WEBTORRENT)
// To debug the WebTorrent process, set `show` to true above and uncomment:
// win.webContents.openDevTools({detached: false})
}
function createMainWindow () {
if (windows.main) {
return focusWindow(windows.main)
@@ -56,7 +76,7 @@ function createMainWindow () {
icon: config.APP_ICON + '.png',
minWidth: 375,
minHeight: 38 + (120 * 2), // header height + 2 torrents
show: false, // Hide window until DOM finishes loading
show: true, // Hide window until DOM finishes loading
title: config.APP_WINDOW_TITLE,
titleBarStyle: 'hidden-inset', // Hide OS chrome, except traffic light buttons (OS X)
useContentSize: true, // Specify web page size without OS chrome