From 9d4c65d1b106926905f59f6afa592a67157148ba Mon Sep 17 00:00:00 2001 From: DC Date: Wed, 6 Apr 2016 03:22:29 -0700 Subject: [PATCH] Queue messages for WebTorrent window until it's ready --- main/ipc.js | 22 +++++++++++++++++++++- renderer/webtorrent.js | 2 ++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/main/ipc.js b/main/ipc.js index af7c13ba..28f9c391 100644 --- a/main/ipc.js +++ b/main/ipc.js @@ -26,6 +26,17 @@ function init () { }, 50) }) + var messageQueueMainToWebTorrent = [] + ipcMain.on('ipcReadyWebTorrent', function (e) { + app.ipcReadyWebTorrent = true + log('sending %d queued messages from the main win to the webtorrent window', + messageQueueMainToWebTorrent.length) + messageQueueMainToWebTorrent.forEach(function (message) { + windows.webtorrent.send(message.name, ...message.args) + log('webtorrent: sent queued %s', message.name) + }) + }) + ipcMain.on('showOpenTorrentFile', menu.showOpenTorrentFile) ipcMain.on('showCreateTorrent', menu.showCreateTorrent) @@ -80,11 +91,20 @@ function init () { // Relay messages between the main window and the WebTorrent hidden window if (name.startsWith('wt-')) { if (e.sender.browserWindowOptions.title === 'webtorrent-hidden-window') { + // Send message to main window windows.main.send(name, ...args) log('webtorrent: got %s', name) - } else { + } else if (app.ipcReadyWebTorrent) { + // Send message to webtorrent window windows.webtorrent.send(name, ...args) log('webtorrent: sent %s', name) + } else { + // Queue message for webtorrent window, it hasn't finished loading yet + messageQueueMainToWebTorrent.push({ + name: name, + args: args + }) + log('webtorrent: queueing %s', name) } return } diff --git a/renderer/webtorrent.js b/renderer/webtorrent.js index 6b44a748..21975a84 100644 --- a/renderer/webtorrent.js +++ b/renderer/webtorrent.js @@ -55,6 +55,8 @@ function init () { ipc.on('wt-stop-server', (e) => stopServer()) + ipc.send('ipcReadyWebTorrent') + setInterval(updateTorrentProgress, 1000) }