-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
62 lines (51 loc) · 1.59 KB
/
client.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
const getSocketPath = require('./lib/socket-path')
const HRPC = require('./lib/rpc')
const { EventEmitter } = require('events')
module.exports = class UpdaterClient extends EventEmitter {
constructor (name) {
super()
this.socketPath = getSocketPath(name)
this.client = HRPC.connect(this.socketPath)
this.latestRelease = null
this.version = null
this.updateAvailable = false
this.updateDownloaded = false
this.updateDownloading = false
this.client.updater.onRequest({ onUpdateStatus: (s) => this._update(s) })
this.ready().catch((err) => this.emit('error', err))
}
_update (s) {
const wasDownloaded = this.updateDownloaded
const wasAvailable = this.updateAvailable
const wasDownloading = this.updateDownloading
this.version = s.version
this.latestRelease = s.latestRelease
this.updateAvailable = s.updateAvailable
this.updateDownloaded = s.updateDownloaded
this.updateDownloading = s.updateDownloading
if (!wasAvailable && this.updateAvailable) {
this.emit('update-available')
}
if (!wasDownloading && this.updateDownloading) {
this.emit('update-downloading')
}
if (!wasDownloaded && this.updateDownloaded) {
this.emit('update-downloaded')
}
}
async ready () {
if (!this.version) {
this._update(await this.client.updater.status())
this.emit('ready')
}
}
updateAndRelaunch () {
return this.client.updater.updateAndRelaunch()
}
downloadUpdate () {
return this.client.updater.downloadUpdate()
}
nextUpdate () {
return this.client.updater.nextUpdate()
}
}