Skip to content

Commit

Permalink
worker window incl. ipc - webpack changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Raul Rosenlöcher committed Jun 13, 2018
1 parent 3a9259f commit dced254
Show file tree
Hide file tree
Showing 14 changed files with 120 additions and 38 deletions.
3 changes: 2 additions & 1 deletion app/common/appConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ export const DEFCONF_LOGLEVEL_CONSOLE = 'warn';
export const DEFCONF_LOG_DELETE_ON_START = true;

export const DEBUG_ARGS = ""; // "-r -o fff -a 12 -t 12"
export const DEBUG_DEVTOOLS_PROD = false;
export const DEBUG_DEVTOOLS_PROD = true;
export const DEBUG_SHOW_WORKER = true;
4 changes: 2 additions & 2 deletions app/common/ipcKeys.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

// channels
export const IPC_TGT_MAIN = 'IPC_TGT_MAIN';
export const IPC_TGT_RENDERER = 'IPC_TGT_REND';
export const IPC_TGT_WORKER = 'IPC_TGT_WORK';
export const IPC_TGT_RENDERER = 'IPC_TGT_RENDERER';
export const IPC_TGT_WORKER = 'IPC_TGT_WORKER';


export const IPC_SRC_MAIN = 'IPC_SRC_MAIN';
Expand Down
15 changes: 10 additions & 5 deletions app/main/ipc/mainIpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ const logKey = "mainIpc-";
// ----------------------------------------------------------------------------------

export function registerListener() {
//log.debug(`${logKey}registerListener`);
ipcMain.on(ipcKeys.IPC_TGT_MAIN, listenMainChannel);
log.debug(`${logKey}registerListener`);

}

// ----------------------------------------------------------------------------------

export function unregisterListener() {
//log.debug(`${logKey}unregisterListener`);
ipcMain.removeAllListeners(ipcKeys.IPC_TGT_MAIN);
log.debug(`${logKey}unregisterListener`);
}

// ----------------------------------------------------------------------------------
Expand All @@ -28,8 +27,12 @@ function listenMainChannel(event, input, output) {
//log.debug("listenMainChannel: event=", event, "; input=", input, "; output=", output);
log.debug(`${logKey}listenMainChannel: input=`, input);

if (input.type === ipcKeys.IPC_STATE_READY)
if (input.type === ipcKeys.IPC_STATE_READY && input.payload === "from_renderer")
sendToRenderer(ipcKeys.IPC_STATE_READY, "from_main");

if (input.type === ipcKeys.IPC_STATE_READY && input.payload === "from_worker")
sendToWorker(ipcKeys.IPC_STATE_READY, "from_main");

}

// ----------------------------------------------------------------------------------
Expand All @@ -39,7 +42,9 @@ export function sendToWorker(ipcType, payload) {
type: ipcType,
payload: payload
}
ipcMain.send(ipcKeys.IPC_TGT_RENDERER, data);
const window = windows.getWorkerWindow();
if (window)
window.webContents.send(ipcKeys.IPC_TGT_WORKER, data);
}

// ----------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion app/main/main.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ if (!configMain.shouldExit()) {
await installExtensions();
}

//windows.createWorkerWindow();
windows.createWorkerWindow();

windows.createMainWindow();

Expand Down
9 changes: 5 additions & 4 deletions app/main/windows.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export function getMainWindow() {

function closeMainWindow() {
configMain.saveConfig();
log.debug("closeMainWindow");
}

// ----------------------------------------------------------------------------------
Expand Down Expand Up @@ -129,10 +130,10 @@ export function createWorkerWindow() {
if (!workerWindow)
throw new Error('"windows" is not defined');

log.debug("createWorkerWindow - did-finish-load");

workerWindow.webContents.openDevTools();
workerWindow.show();
if (appConstants.DEBUG_SHOW_WORKER) {
workerWindow.webContents.openDevTools();
workerWindow.show();
}

});

Expand Down
7 changes: 3 additions & 4 deletions app/renderer/ipc/rendererIpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@ const logKey = "rendererIpc-";
// ----------------------------------------------------------------------------------

export function registerListener() {
//log.debug(`${logKey}registerListener`);
ipcRenderer.on(ipcKeys.IPC_TGT_RENDERER, listenRendererChannel);
log.debug(`${logKey}registerListener`);

sendToMain(ipcKeys.IPC_STATE_READY, "registerListener");
sendToMain(ipcKeys.IPC_STATE_READY, "from_renderer");
}

// ----------------------------------------------------------------------------------

export function unregisterListener() {
//log.debug(`${logKey}unregisterListener`);
ipcRenderer.removeAllListeners(ipcKeys.IPC_TGT_MAIN);
log.debug("unregisterListener");
log.debug(`${logKey}registerListener`);
}

// ----------------------------------------------------------------------------------
Expand Down
10 changes: 3 additions & 7 deletions app/worker/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import log from 'electron-log';
import * as workerIpc from './workerIpc';

log.info("worker/index.js - loaded");

console.log("con - worker.js - loaded");
//alert("Hello! I am an alert box!!");
workerIpc.registerListener();

//const log = require('electron-log');
log.info("log - worker.js - loaded");

//import log from 'electron-log';
//log.info("worker.js - loaded");


28 changes: 26 additions & 2 deletions app/worker/worker.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,32 @@
<title>invisible-worker-window!</title>
</head>
<body>
<div id="app"></div>
<script src="./worker.js"></script>
<div id="worker"><h2>invisible-worker-window!</h2></div>
<script>
{
const scripts = [];

// Dynamically insert the DLL script in development env in the
// renderer process
if (process.env.NODE_ENV === 'development') {
scripts.push('../../dll/worker.dev.dll.js');
}

// Dynamically insert the bundled app script in the renderer process
const port = process.env.PORT || 1212;
scripts.push(
(process.env.HOT)
? 'http://localhost:' + port + '/dist/worker.dev.js'
: '../dist/worker.prod.js'
);

document.write(
scripts
.map(script => `<script defer src="${script}"><\/script>`)
.join('')
);
}
</script>
</body>
</html>

Expand Down
44 changes: 44 additions & 0 deletions app/worker/workerIpc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import log from 'electron-log';
import {ipcRenderer} from 'electron';
import * as ipcKeys from "../common/ipcKeys";

// ----------------------------------------------------------------------------------

const logKey = "workerIpc-";

// ----------------------------------------------------------------------------------

export function registerListener() {
//log.debug(`${logKey}registerListener`);
ipcRenderer.on(ipcKeys.IPC_TGT_WORKER, listenWorkerChannel);

sendToMain(ipcKeys.IPC_STATE_READY, "from_worker");
}

// ----------------------------------------------------------------------------------

export function unregisterListener() {
//log.debug(`${logKey}unregisterListener`);
ipcRenderer.removeAllListeners(ipcKeys.IPC_TGT_MAIN);
}

// ----------------------------------------------------------------------------------

function listenWorkerChannel(event, input, output) {
//log.debug("listenRendererChannel: event=", event, "; input=", input, "; output=", output);
log.debug(`${logKey}listenWorkerChannel: input=`, input);

}

// ----------------------------------------------------------------------------------

export function sendToMain(ipcType, payload) {
const data = {
type: ipcType,
payload: payload
}
ipcRenderer.send(ipcKeys.IPC_TGT_MAIN, data);
}

// ----------------------------------------------------------------------------------

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"dist/",
"node_modules/",
"renderer/app.html",
"worker/worker.html",
"main/main.prod.js",
"main/main.prod.js.map",
"package.json"
Expand Down
1 change: 1 addition & 0 deletions webpack.config.base.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export default {

output: {
path: path.join(__dirname, 'app'),
filename: '[name].prod.js',
// https://github.com/webpack/webpack/issues/1114
libraryTarget: 'commonjs2'
},
Expand Down
5 changes: 2 additions & 3 deletions webpack.config.renderer.dev.dll.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ export default merge.smart(baseConfig, {
module: require('./webpack.config.renderer.dev').module,

entry: {
renderer: Object.keys(dependencies || {}).filter(
dependency => dependency !== 'font-awesome'
)
renderer: Object.keys(dependencies || {}).filter(dependency => dependency !== 'font-awesome'),
worker: Object.keys(dependencies || {}).filter(dependency => dependency !== 'font-awesome')
},

output: {
Expand Down
22 changes: 15 additions & 7 deletions webpack.config.renderer.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,24 @@ export default merge.smart(baseConfig, {

target: 'electron-renderer',

entry: [
'react-hot-loader/patch',
`webpack-dev-server/client?http://localhost:${port}/`,
'webpack/hot/only-dev-server',
path.join(__dirname, 'app', 'renderer', 'index.js')
],
entry: {
renderer: [
'react-hot-loader/patch',
`webpack-dev-server/client?http://localhost:${port}/`,
'webpack/hot/only-dev-server',
path.join(__dirname, 'app', 'renderer', 'index.js')
],
worker: [
'react-hot-loader/patch',
`webpack-dev-server/client?http://localhost:${port}/`,
'webpack/hot/only-dev-server',
path.join(__dirname, 'app', 'worker', 'index.js')
]
},

output: {
publicPath: `http://localhost:${port}/dist/`,
filename: 'renderer.dev.js'
filename: '[name].dev.js'
},

module: {
Expand Down
7 changes: 5 additions & 2 deletions webpack.config.renderer.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ export default merge.smart(baseConfig, {

target: 'electron-renderer',

entry: './app/renderer/index',
entry: {
renderer: './app/renderer/index',
worker: './app/worker/index'
},

output: {
path: path.join(__dirname, 'app/dist'),
publicPath: './dist/',
filename: 'renderer.prod.js'
filename: '[name].prod.js'
},

module: {
Expand Down

0 comments on commit dced254

Please sign in to comment.