-
Notifications
You must be signed in to change notification settings - Fork 21
/
background_task.js
52 lines (43 loc) · 1.07 KB
/
background_task.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
const { BrowserWindow } = require('electron');
const url = require('url');
const { EventEmitter } = require('events');
class BackgroundTask extends EventEmitter {
constructor(path) {
super();
this.killed = false;
// Create the hidden browser window
this.window = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true
}
});
// Sends when the window is ready to start getting and sending data
this.window.webContents.on('did-finish-load', () => {
this.emit('ready');
});
// Bubbles any IPC message in the form of an EventEmitter event
this.window.webContents.on('ipc-message', (event, channel, data) => {
this.emit(channel, data);
});
// Load the task
this.window.loadURL(url.format({
pathname: path,
protocol: 'file:',
slashes: true
}));
this.window.on('closed', () => {
this.window = null;
});
}
// Send data to the task
send(name, data) {
this.window.webContents.send(name, data);
}
// Kill the task
kill() {
this.window.destroy();
this.killed = true;
}
}
module.exports = BackgroundTask;