forked from pubkey/rxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
73 lines (59 loc) · 1.75 KB
/
main.js
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
const electron = require('electron');
const path = require('path');
const { addRxPlugin } = require('rxdb');
const { RxDBServerCouchDBPlugin } = require('rxdb/plugins/server-couchdb');
addRxPlugin(RxDBServerCouchDBPlugin);
const database = require('./database');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
const windows = [];
function createWindow() {
const width = 300;
const height = 600;
const w = new BrowserWindow({
width,
height,
webPreferences: {
contextIsolation: false,
nodeIntegration: true,
preload: path.join(__dirname, 'preload.js')
}
});
w.loadFile('index.html');
const x = windows.length * width;
const y = 0;
w.setPosition(x, y);
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
electron.ipcMain.handle('getDBSuffix', () => dbSuffix);
const db = await database.createDatabase(
'heroesdb' + dbSuffix,
'memory'
);
/**
* spawn a server
* which is used as sync-goal by page.js
*/
console.log('start server');
await db.serverCouchDB({
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();
createWindow();
});
app.on('window-all-closed', function () {
if (process.platform !== 'darwin')
app.quit();
});