Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
78 lines (65 loc) · 1.82 KB
/
Copy pathmain.js
File metadata and controls
78 lines (65 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const electron = require('electron');
require('electron-window-manager');
const path = require('path');
const url = require('url');
const database = require('./database');
const { addRxPlugin } = require('rxdb');
addRxPlugin(require('rxdb/plugins/server').RxDBServerPlugin);
addRxPlugin(require('pouchdb-adapter-memory'));
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
const windows = [];
function createWindow(dbSuffix) {
const width = 300;
const height = 600;
const w = new BrowserWindow({
width,
height,
webPreferences: {
nodeIntegration: true
}
});
w.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}));
const x = windows.length * width;
const y = 0;
w.setPosition(x, y);
w.custom = {
dbSuffix
};
windows.push(w);
}
app.on('ready', async function () {
const dbSuffix = new Date().getTime(); // we add a random timestamp in dev-mode to reset the database on each start
const db = await database.getDatabase(
'heroesdb' + dbSuffix,
'memory'
);
/**
* spawn a server
* which is used as sync-goal by page.js
*/
console.log('start server');
await db.server({
path: '/db',
port: 10102,
cors: true
});
console.log('started server');
// show heroes table in console
db.heroes.find().sort('name').$.subscribe(heroDocs => {
console.log('### got heroes(' + heroDocs.length + '):');
heroDocs.forEach(doc => console.log(
doc.name + ' | ' + doc.color
));
});
createWindow(dbSuffix);
createWindow(dbSuffix);
});
app.on('window-all-closed', function () {
if (process.platform !== 'darwin')
app.quit();
});