From f771ac68c4cacec484c3598c1fbaa0c1a3b07851 Mon Sep 17 00:00:00 2001 From: Christophe Hamerling Date: Tue, 23 May 2017 11:13:06 +0200 Subject: [PATCH] Add flag to avoid weird errors when browser window has been already closed --- index.js | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index e403699..b14a399 100644 --- a/index.js +++ b/index.js @@ -34,6 +34,7 @@ module.exports = function (config, windowParams) { return new Promise(function (resolve, reject) { const authWindow = new BrowserWindow(windowParams || {'use-content-size': true}); + let done = false; authWindow.loadURL(url); authWindow.show(); @@ -42,7 +43,20 @@ module.exports = function (config, windowParams) { reject(new Error('window was closed by user')); }); + function close() { + authWindow.removeAllListeners('closed'); + + setImmediate(function() { + authWindow.close(); + }); + } + function onCallback(url) { + if (done) { + return; + } + done = true; + var url_parts = nodeUrl.parse(url, true); var query = url_parts.query; var code = query.code; @@ -50,16 +64,10 @@ module.exports = function (config, windowParams) { if (error !== undefined) { reject(error); - authWindow.removeAllListeners('closed'); - setImmediate(function () { - authWindow.close(); - }); + close(); } else if (code) { resolve(code); - authWindow.removeAllListeners('closed'); - setImmediate(function () { - authWindow.close(); - }); + close(); } }