diff options
author | Valentin Popov <info@valentineus.link> | 2019-08-20 13:51:06 +0300 |
---|---|---|
committer | Valentin Popov <info@valentineus.link> | 2019-08-20 13:51:06 +0300 |
commit | 9c57d101f0181aa134190edb0b2e7aabdba1151f (patch) | |
tree | 41e2405204d001490ba76e272bc5d11020a2c357 /example/application/src | |
parent | b3461bdcc0462bda7fad46b02f8307a0bc1166cd (diff) | |
download | electron-hot-reload-9c57d101f0181aa134190edb0b2e7aabdba1151f.tar.xz electron-hot-reload-9c57d101f0181aa134190edb0b2e7aabdba1151f.zip |
Added example application
Signed-off-by: Valentin Popov <info@valentineus.link>
Diffstat (limited to 'example/application/src')
-rw-r--r-- | example/application/src/index.html | 45 | ||||
-rw-r--r-- | example/application/src/main.js | 63 | ||||
-rw-r--r-- | example/application/src/renderer.js | 28 | ||||
-rw-r--r-- | example/application/src/utils/get-json.js | 12 | ||||
-rw-r--r-- | example/application/src/utils/reloader.js | 20 |
5 files changed, 168 insertions, 0 deletions
diff --git a/example/application/src/index.html b/example/application/src/index.html new file mode 100644 index 0000000..9b8aba9 --- /dev/null +++ b/example/application/src/index.html @@ -0,0 +1,45 @@ +<!DOCTYPE html> +<html lang="en"> + + <head> + <meta name="viewport" content="initial-scale=1.0, maximum-scale=1, shrink-to-fit=no, width=device-width" /> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> + <meta http-equiv="X-UA-Compatible" content="IE=edge" /> + <meta charset="utf-8" /> + <title><%= htmlWebpackPlugin.options.title %></title> + + <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" /> + </head> + + <body> + <div class="container-fluid"> + <div class="container text-center"> + <h1>Last reload</h1> + </div> + + <div class="container text-center"> + <div> + Main Process: + <span id="main"></span> + </div> + + <div> + Renderer Process: + <span id="renderer"></span> + </div> + </div> + + <div class="container text-center pt-3"> + <div class="btn-group" role="group" aria-label="Basic example"> + <button type="button" class="btn btn-secondary" id="btnMain">Reload Main</button> + <button type="button" class="btn btn-secondary" id="btnRend">Reload Renderer</button> + </div> + </div> + </div> + + <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script> + <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script> + <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script> + </body> + +</html> diff --git a/example/application/src/main.js b/example/application/src/main.js new file mode 100644 index 0000000..162768b --- /dev/null +++ b/example/application/src/main.js @@ -0,0 +1,63 @@ +// Modules to control application life and create native browser window +import { app, BrowserWindow } from 'electron' + +import './utils/reloader' +import './utils/get-json' + +// Keep a global reference of the window object, if you don't, the window will +// be closed automatically when the JavaScript object is garbage collected. +let mainWindow + +function createWindow() { + // Create the browser window. + mainWindow = new BrowserWindow({ + width: 800, + height: 600, + show: false, + webPreferences: { + nodeIntegration: true + } + }) + + // and load the index.html of the app. + mainWindow.loadFile('dist/index.html') + + // Open the DevTools. + // mainWindow.webContents.openDevTools() + + // Emitted when the window is closed. + mainWindow.on('closed', function () { + // Dereference the window object, usually you would store windows + // in an array if your app supports multi windows, this is the time + // when you should delete the corresponding element. + mainWindow = null + }) + + // While loading the page, the ready - to - show event will be emitted + // when the renderer process has rendered the page for the first time + // if the window has not been shown yet. + mainWindow.on('ready-to-show', () => { + mainWindow.show(); + }); +} + +// This method will be called when Electron has finished +// initialization and is ready to create browser windows. +// Some APIs can only be used after this event occurs. +app.on('ready', createWindow) + +// Quit when all windows are closed. +app.on('window-all-closed', function () { + // On macOS it is common for applications and their menu bar + // to stay active until the user quits explicitly with Cmd + Q + if (process.platform !== 'darwin') app.quit() +}) + +app.on('activate', function () { + // On macOS it's common to re-create a window in the app when the + // dock icon is clicked and there are no other windows open. + if (mainWindow === null) createWindow() +}) + +// In this file you can include the rest of your app's specific main process +// code. You can also put them in separate files and require them here. diff --git a/example/application/src/renderer.js b/example/application/src/renderer.js new file mode 100644 index 0000000..f4376e9 --- /dev/null +++ b/example/application/src/renderer.js @@ -0,0 +1,28 @@ +// This file is required by the index.html file and will +// be executed in the renderer process for that window. +// All of the Node.js APIs are available in this process. + +import { ipcRenderer } from 'electron' +import moment from 'moment' + +ipcRenderer.on('get:json:result', (event, { main, renderer }) => { + const mainTime = moment(main.time || 0).startOf('minute').fromNow() + const rendererTime = moment(renderer.time || 0).startOf('minute').fromNow() + + document.getElementById("main").innerHTML = mainTime; + document.getElementById("renderer").innerHTML = rendererTime; +}) + +ipcRenderer.send('get:json') + +setTimeout(() => { + ipcRenderer.send('get:json') +}, 3000); + +document.querySelector('#btnMain').addEventListener('click', () => { + ipcRenderer.send('reload:main') +}) + +document.querySelector('#btnRend').addEventListener('click', () => { + ipcRenderer.send('reload:renderer') +}) diff --git a/example/application/src/utils/get-json.js b/example/application/src/utils/get-json.js new file mode 100644 index 0000000..18f4403 --- /dev/null +++ b/example/application/src/utils/get-json.js @@ -0,0 +1,12 @@ +import { app, ipcMain } from 'electron' +import jsonfile from 'jsonfile' +import path from 'path' + +ipcMain.on('get:json', async (event) => { + const tempDir = path.join(app.getAppPath(), 'temp') + + const main = await jsonfile.readFile(path.join(tempDir, 'main.json')) + const renderer = await jsonfile.readFile(path.join(tempDir, 'renderer.json')) + + event.sender.send('get:json:result', { main, renderer }) +}) diff --git a/example/application/src/utils/reloader.js b/example/application/src/utils/reloader.js new file mode 100644 index 0000000..ad78496 --- /dev/null +++ b/example/application/src/utils/reloader.js @@ -0,0 +1,20 @@ +import { app, ipcMain } from 'electron' +import jsonfile from 'jsonfile' +import path from 'path' + +import { mainReloader, rendererReloader } from '../../../../dist' + +const tempDir = path.join(app.getAppPath(), 'temp') +const tempMain = path.join(tempDir, 'main.json') +const tempRend = path.join(tempDir, 'renderer.json') + +mainReloader(tempMain) +rendererReloader(tempRend) + +ipcMain.on('reload:main', async () => { + await jsonfile.writeFile(tempMain, { time: Date.now() }) +}) + +ipcMain.on('reload:renderer', async () => { + await jsonfile.writeFile(tempRend, { time: Date.now() }) +}) |