Skip to content

Commit 01d938f

Browse files
author
Syed Adeel Hassan Rizvi
committed
Storing main window instance to reload later.
1 parent 00eb986 commit 01d938f

File tree

6 files changed

+220
-99
lines changed

6 files changed

+220
-99
lines changed

ElectronNET.Host/api/browserWindows.js

Lines changed: 14 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ElectronNET.Host/api/browserWindows.ts

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ const path = require('path');
33
const windows: Electron.BrowserWindow[] = [];
44
let readyToShowWindowsIds: number[] = [];
55
let window, lastOptions, electronSocket;
6-
let mainWindowId;
7-
6+
let mainWindowURL;
87
export = (socket: SocketIO.Socket, app: Electron.App) => {
98
electronSocket = socket;
109
socket.on('register-browserWindow-ready-to-show', (id) => {
@@ -200,24 +199,16 @@ export = (socket: SocketIO.Socket, app: Electron.App) => {
200199
options = { ...options, webPreferences: { nodeIntegration: true } };
201200
}
202201

203-
// lets not recreate the same window if its already open
204-
let nWindow = null;
205-
206-
if (app.commandLine.hasSwitch('watch')) {
207-
(app as any).on('hot-reload', (id) => {
208-
mainWindowId = id;
209-
nWindow = getWindowById(mainWindowId);
210-
if (nWindow) {
211-
nWindow.reload();
212-
if (loadUrl) {
213-
nWindow.loadURL(loadUrl);
214-
}
215-
return;
216-
}
217-
})
202+
// we dont want to recreate the window when watch is ready.
203+
if (app.commandLine.hasSwitch('watch') && app['mainWindowURL'] === loadUrl) {
204+
window = app['mainWindow'];
205+
if (window) {
206+
window.reload();
207+
}
208+
} else {
209+
window = new BrowserWindow(options);
218210
}
219211

220-
window = new BrowserWindow(options);
221212
window.on('ready-to-show', () => {
222213
if (readyToShowWindowsIds.includes(window.id)) {
223214
readyToShowWindowsIds = readyToShowWindowsIds.filter(value => value !== window.id);
@@ -263,10 +254,10 @@ export = (socket: SocketIO.Socket, app: Electron.App) => {
263254
console.log('auto clear-cache active for new window.');
264255
}
265256

266-
// set main window id
267-
if (mainWindowId == undefined) {
268-
mainWindowId = window.id;
269-
app.emit("mainWindow", mainWindowId);
257+
// set main window url
258+
if (app['mainWindowURL'] == undefined || app['mainWindowURL'] == "") {
259+
app['mainWindowURL'] = loadUrl;
260+
app['mainWindow'] = window;
270261
}
271262

272263
windows.push(window);
@@ -768,6 +759,7 @@ export = (socket: SocketIO.Socket, app: Electron.App) => {
768759
getWindowById(id).setBrowserView(browserView);
769760
});
770761

762+
771763
function getWindowById(id: number): Electron.BrowserWindow {
772764
for (let index = 0; index < windows.length; index++) {
773765
const element = windows[index];

ElectronNET.Host/main.js

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const path = require('path');
44
const cProcess = require('child_process').spawn;
55
const portscanner = require('portscanner');
66
const imageSize = require('image-size');
7+
const chalk = require('chalk');
78
let io, server, browserWindows, ipc, apiProcess, loadURL;
89
let appApi, menu, dialogApi, notification, tray, webContents;
910
let globalShortcut, shellApi, screen, clipboard, autoUpdater;
@@ -55,17 +56,12 @@ app.on('ready', () => {
5556

5657
// hostname needs to belocalhost, otherwise Windows Firewall will be triggered.
5758
portscanner.findAPortNotInUse(8000, 65535, 'localhost', function (error, port) {
58-
console.log('Electron Socket IO Port: ' + port);
59+
console.log(chalk.blue('Electron Socket IO Port: ' + port));
5960
startSocketApiBridge(port);
6061
});
6162

6263
});
6364

64-
app.on("mainWindow", id => {
65-
mainWindowId = id;
66-
console.log(` Main Window ID = ${id}`);
67-
})
68-
6965
function isSplashScreenEnabled() {
7066
if (manifestJsonFile.hasOwnProperty('splashscreen')) {
7167
if (manifestJsonFile.splashscreen.hasOwnProperty('imageFile')) {
@@ -80,8 +76,8 @@ function startSplashScreen() {
8076
let imageFile = path.join(currentBinPath, manifestJsonFile.splashscreen.imageFile);
8177
imageSize(imageFile, (error, dimensions) => {
8278
if (error) {
83-
console.log(`load splashscreen error:`);
84-
console.log(error);
79+
console.log(chalk.bold.red(`load splashscreen error:`));
80+
console.log(chalk.bold.red(error));
8581

8682
throw new Error(error.message);
8783
}
@@ -122,7 +118,7 @@ function startSocketApiBridge(port) {
122118

123119
server.listen(port, 'localhost');
124120
server.on('listening', function () {
125-
console.log('Electron Socket started on port %s at %s', server.address().port, server.address().address);
121+
console.log(chalk.bgGreenBright('Electron Socket started on port %s at %s', server.address().port, server.address().address));
126122
// Now that socket connection is established, we can guarantee port will not be open for portscanner
127123
if (watchable) {
128124
startAspCoreBackendWithWatch(port);
@@ -131,15 +127,37 @@ function startSocketApiBridge(port) {
131127
}
132128
});
133129

130+
// prototype
131+
app['mainWindowURL'] = "";
132+
app['mainWindow'] = null;
133+
134134
io.on('connection', (socket) => {
135+
136+
// we need to remove previously cache instances
137+
// otherwise it will fire the same event multiple depends how many time
138+
// live reload watch happen.
139+
socket.on('disconnect', function () {
140+
console.log(chalk.bold.red('Got disconnect!'));
141+
delete require.cache[require.resolve('./api/app')];
142+
delete require.cache[require.resolve('./api/browserWindows')];
143+
delete require.cache[require.resolve('./api/commandLine')];
144+
delete require.cache[require.resolve('./api/autoUpdater')];
145+
delete require.cache[require.resolve('./api/ipc')];
146+
delete require.cache[require.resolve('./api/menu')];
147+
delete require.cache[require.resolve('./api/dialog')];
148+
delete require.cache[require.resolve('./api/notification')];
149+
delete require.cache[require.resolve('./api/tray')];
150+
delete require.cache[require.resolve('./api/webContents')];
151+
delete require.cache[require.resolve('./api/globalShortcut')];
152+
delete require.cache[require.resolve('./api/shell')];
153+
delete require.cache[require.resolve('./api/screen')];
154+
delete require.cache[require.resolve('./api/clipboard')];
155+
delete require.cache[require.resolve('./api/browserView')];
156+
});
157+
135158
global['electronsocket'] = socket;
136159
global['electronsocket'].setMaxListeners(0);
137-
console.log('ASP.NET Core Application connected...', 'global.electronsocket', global['electronsocket'].id, new Date());
138-
139-
// send signal to reload
140-
if (mainWindowId != undefined) {
141-
app.emit("hot-reload", mainWindowId);
142-
}
160+
console.log(chalk.bold.bgCyan('ASP.NET Core Application connected...', 'global.electronsocket', global['electronsocket'].id, new Date()));
143161

144162
appApi = require('./api/app')(socket, app);
145163
browserWindows = require('./api/browserWindows')(socket, app);
@@ -157,6 +175,8 @@ function startSocketApiBridge(port) {
157175
clipboard = require('./api/clipboard')(socket);
158176
browserView = require('./api/browserView')(socket);
159177

178+
179+
160180
try {
161181
const hostHookScriptFilePath = path.join(__dirname, 'ElectronHostHook', 'index.js');
162182

@@ -166,7 +186,7 @@ function startSocketApiBridge(port) {
166186
hostHook.onHostReady();
167187
}
168188
} catch (error) {
169-
console.log(error.message);
189+
console.log(chalk.bold.red(error.message));
170190
}
171191
});
172192
}
@@ -225,15 +245,14 @@ function startAspCoreBackendWithWatch(electronPort) {
225245
loadURL = `http://localhost:${aspCoreBackendPort}`;
226246
const parameters = ['watch', 'run', `/electronPort=${electronPort}`, `/electronWebPort=${aspCoreBackendPort}`];
227247

228-
console.log(currentBinPath);
229248
var options = {
230249
cwd: currentBinPath,
231250
env: process.env,
232251
};
233252
apiProcess = cProcess('dotnet', parameters, options);
234253

235254
apiProcess.stdout.on('data', (data) => {
236-
console.log(`stdout: ${data.toString()}`);
255+
console.log(chalk.bold.blue(`${data.toString()}`));
237256
});
238257
}
239258
}

0 commit comments

Comments
 (0)