From 73eebb1e34d6819afca170894cf24d5290c2ecd2 Mon Sep 17 00:00:00 2001 From: steveetm Date: Tue, 12 May 2020 07:43:50 +0200 Subject: [PATCH] Fix timing issue with electron@8 (#65) * Fix timing issue with electron@8 * Compatibilty with electron<5 * Lint Co-authored-by: Andrey Belym --- package.json | 2 +- src/injectable/electron-mocks.js | 25 ++++++++++++++++++------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 51c2216..81d996f 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,7 @@ "babel-plugin-transform-runtime": "^6.23.0", "babel-preset-env": "^1.6.0", "del": "^2.0.0", - "electron": "^7.1.2", + "electron": "^8.2.3", "gulp": "^4.0.0", "gulp-babel": "^7.0.0", "gulp-eslint": "^3.0.1", diff --git a/src/injectable/electron-mocks.js b/src/injectable/electron-mocks.js index 87f3735..4d7a7ba 100644 --- a/src/injectable/electron-mocks.js +++ b/src/injectable/electron-mocks.js @@ -2,6 +2,10 @@ import { Client } from '../ipc'; import resolveFileUrl from '../utils/resolve-file-url'; import CONSTANTS from '../constants'; +const ELECTRON_VERSION = process.versions.electron && Number(process.versions.electron.split('.')[0]); + +const ELECTRON_VERSION_WITH_ASYNC_LOAD_URL = 5; + const URL_QUERY_RE = /\?.*$/; const NAVIGATION_EVENTS = ['will-navigate', 'did-navigate']; @@ -54,8 +58,8 @@ function handleDialog (type, args) { module.exports = function install (config, testPageUrl) { ipc = new Client(config, { dialogHandler, contextMenuHandler, windowHandler }); - ipc.connect(); - + const ipcConnectionPromise = ipc.connect(); + var { Menu, dialog, app } = require('electron'); var WebContents; @@ -82,9 +86,7 @@ module.exports = function install (config, testPageUrl) { return url.indexOf('file:') === 0; } - WebContents.prototype.loadURL = function (url, options) { - startLoadingTimeout(config.mainWindowUrl); - + function loadUrl (webContext, url, options) { let testUrl = stripQuery(url); if (isFileProtocol(url)) @@ -104,10 +106,19 @@ module.exports = function install (config, testPageUrl) { windowHandler.window = this; if (config.openDevTools) - this.openDevTools(); + webContext.openDevTools(); } - return origLoadURL.call(this, url, options); + return origLoadURL.call(webContext, url, options); + } + + WebContents.prototype.loadURL = function (url, options) { + startLoadingTimeout(config.mainWindowUrl); + + if (ELECTRON_VERSION >= ELECTRON_VERSION_WITH_ASYNC_LOAD_URL) + return ipcConnectionPromise.then(() => loadUrl(this, url, options)); + + return loadUrl(this, url, options); }; app.getAppPath = function () {