|
| 1 | +const remote = require('@electron/remote/main'); |
1 | 2 | const electron = require('electron');
|
2 |
| -require('electron-window-manager'); |
3 |
| -const path = require('path'); |
4 |
| -const url = require('url'); |
5 | 3 | const database = require('./database');
|
6 | 4 |
|
| 5 | +remote.initialize(); |
| 6 | + |
7 | 7 | const app = electron.app;
|
8 | 8 | const BrowserWindow = electron.BrowserWindow;
|
9 | 9 |
|
10 | 10 | const windows = [];
|
11 | 11 |
|
12 | 12 | global.db; // define the global db object
|
13 | 13 |
|
14 |
| -function createWindow(dbSuffix) { |
| 14 | +function createWindow() { |
15 | 15 | const width = 300;
|
16 | 16 | const height = 600;
|
17 | 17 | const w = new BrowserWindow({
|
18 | 18 | width,
|
19 | 19 | height,
|
20 | 20 | webPreferences: {
|
21 |
| - nodeIntegration: true |
| 21 | + contextIsolation: false, |
| 22 | + nodeIntegration: true, |
22 | 23 | }
|
23 | 24 | });
|
24 | 25 |
|
25 |
| - w.loadURL(url.format({ |
26 |
| - pathname: path.join(__dirname, 'index.html'), |
27 |
| - protocol: 'file:', |
28 |
| - slashes: true |
29 |
| - })); |
| 26 | + remote.enable(w.webContents); |
| 27 | + |
| 28 | + w.loadFile('index.html'); |
30 | 29 |
|
31 | 30 | const x = windows.length * width;
|
32 | 31 | const y = 0;
|
33 | 32 | w.setPosition(x, y);
|
34 |
| - w.custom = { |
35 |
| - dbSuffix |
36 |
| - }; |
37 | 33 | windows.push(w);
|
38 | 34 | }
|
39 | 35 |
|
40 | 36 |
|
41 |
| -app.on('ready', async function() { |
| 37 | +app.on('ready', async function () { |
42 | 38 | const dbSuffix = new Date().getTime(); // we add a random timestamp in dev-mode to reset the database on each start
|
43 | 39 |
|
44 |
| - global.db = await database.getDatabase( |
| 40 | + electron.ipcMain.handle('getDBSuffix', () => dbSuffix); |
| 41 | + |
| 42 | + const db = await database.createDatabase( |
45 | 43 | 'heroesdb' + dbSuffix,
|
46 | 44 | 'memory'
|
47 | 45 | );
|
48 | 46 |
|
| 47 | + global.db = db; |
| 48 | + |
49 | 49 | // show heroes table in console
|
50 |
| - global.db.heroes.find().sort('name').$.subscribe(heroDocs => { |
| 50 | + db.heroes.find().sort('name').$.subscribe(heroDocs => { |
51 | 51 | console.log('### got heroes(' + heroDocs.length + '):');
|
52 | 52 | heroDocs.forEach(doc => console.log(
|
53 | 53 | doc.name + ' | ' + doc.color
|
54 | 54 | ));
|
55 | 55 | });
|
56 | 56 |
|
57 |
| - createWindow(dbSuffix); |
58 |
| - createWindow(dbSuffix); |
| 57 | + createWindow(); |
| 58 | + // FIXME: if remove the next line, replication between windows will not work |
| 59 | + // await new Promise(resolve => setTimeout(resolve, 2000)); |
| 60 | + createWindow(); |
59 | 61 | });
|
60 | 62 |
|
61 |
| -app.on('window-all-closed', function() { |
| 63 | +app.on('window-all-closed', function () { |
62 | 64 | if (process.platform !== 'darwin')
|
63 | 65 | app.quit();
|
64 | 66 | });
|
0 commit comments