-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
170 lines (142 loc) · 4.7 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { app, BrowserWindow } from 'electron';
import path from 'path';
import Datastore from "nedb";
import crawler from './server/crawler';
import {ipcMain} from 'electron';
const CL = new crawler();
/*
nedb need to be setup in main
DB: 文章、无聊图、妹子图
db与render,要么通过koa走一个本地server,做一个restAPI通信,
要么直接用electron内置的ipc通信
*/
const db = {};
db.page = new Datastore({filename: getDatabasePath('page'), autoload: true});
db.pic = new Datastore({filename: getDatabasePath('pic'), autoload: true});
db.ooxx = new Datastore({filename: getDatabasePath('ooxx'), autoload: true});
/*
ensureIndex 有一定的性能提升
*/
db.page.ensureIndex({ fieldName: '_id', unique: true }, function (err) {
if(err) console.log(err)
});
db.pic.ensureIndex({ fieldName: '_id', unique: true }, function (err) {
if(err) console.log(err)
});
db.ooxx.ensureIndex({ fieldName: '_id', unique: true }, function (err) {
if(err) console.log(err)
});
function getDatabasePath(name){
return path.join(__dirname, 'data', name + '.db');
}
// not palnning to build on win32, it's 2017 ....
// if (handleSquirrelCommand()) return;
let win = null;
process.env.NODE_ENV = process.env.NODE_ENV || 'production';
console.log(process.env.NODE_ENV);
const shouldQuit = app.makeSingleInstance(function(otherInstArgv, otherInstWorkingDir){
if(win != null){
if(win.isMinimized()){
win.restore();
}
win.focus();
}
});
if(shouldQuit) app.quit();
function createWindow(){
console.log('create Electron Window ...');
// app.server = require('./server/app.js');
CL.init(db);
/*
all the custom below is targeted on MAC, other platform never tested
*/
win = new BrowserWindow(
{
useContentSize: true, width:1000, height: 680, backgroundColor: '#2e2c29', resizable: false, title: "JdView",
frame: false, darkTheme: true, vibrancy: 'dark', scrollBounce: true, type: 'textured', fullscreen: false,
}
);
win.loadURL('file://' + __dirname + '/index.html');
// win.webContents.openDevTools();
win.on('closed', () => { win = null;});
/*
set state through ipcMain
*/
ipcMain.on('synchronous-message', (event, arg) => {
// console.log(arg);
// arg[0] is string, define type of action here, arg[1] is params
switch (arg[0]) {
case 'page':
db.page.find({}).sort({date_group:-1, index:-1}).skip(arg[1]).limit(arg[2]).exec((err, doc) => {
event.returnValue = doc;
});
// checking if need to update
db.page.find({}).sort({date_group:-1, index:-1}).exec((err, doc) => {
if(doc.length - arg[2] < 1.5 * (arg[2] - arg[1])){
CL.crawlMore('page', db)
}
});
break;
case 'pic':
db.pic.find({}).sort({_id:-1}).skip(arg[1]).limit(arg[2]).exec((err, doc) => {
event.returnValue = doc;
});
db.pic.find({}).sort({_id:-1}).exec((err, doc) => {
if(doc.length - arg[2] < 1.5 * (arg[2] - arg[1])){
CL.crawlMore('pic', db)
}
});
break;
case 'ooxx':
db.ooxx.find({}).sort({_id:-1}).skip(arg[1]).limit(arg[2]).exec((err, doc) => {
event.returnValue = doc;
});
db.ooxx.find({}).sort({_id:-1}).exec((err, doc) => {
if(doc.length - arg[2] < 1.5 * (arg[2] - arg[1])){
CL.crawlMore('ooxx', db)
}
});
break;
case 'detailPage':
CL.detailPage(arg[1]).then(res => {
// console.log('res:', res[0]);
event.returnValue = res;
}).catch(e => {
console.log(e);
});
break;
}
})
}
app.on('ready', createWindow);
app.on('window-all-closed', () => {
if(process.platform != 'darwin') app.quit();
});
app.on('active', () =>{
if(win == null) createWindow();
})
// function handleSquirrelCommand() {
// if (process.platform != 'win32') return false; // only applies to Windows (win32 is both 32- & 64-bit)
//
// const command = process.argv[1];
// const target = path.basename(process.execPath);
//
// switch (command) {
// case '--squirrel-install':
// case '--squirrel-updated':
// update(['--createShortcut=' + target + ''], app.quit);
// return true;
// case '--squirrel-uninstall':
// update(['--removeShortcut=' + target + ''], app.quit);
// return true;
// case '--squirrel-obsolete':
// app.quit();
// return true;
// }
//
// return false;
// }
// function update(argsm, done){
// const updateExe = path.resolve(path.dirname(process.execPath), '..', 'Update.exe');
// spawn(updateExe, args, { detached: true}).on('close', done);
// }