-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
82 lines (67 loc) · 1.75 KB
/
Copy pathapp.js
File metadata and controls
82 lines (67 loc) · 1.75 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
79
80
81
82
const FramedStream = require('framed-stream')
const PearRuntime = require('pear-runtime')
const ReadyResource = require('ready-resource')
module.exports = class App extends ReadyResource {
constructor({ dir, app, updates, version, upgrade, name }) {
super()
this.dir = dir
this.app = app
this.updates = updates
this.version = version
this.upgrade = upgrade
this.name = name
this.IPC = null
this.pipe = null
}
_open() {
this.IPC = PearRuntime.run(require.resolve('./workers/main.js'), [
String(this.updates),
this.version,
this.upgrade,
this.name,
this.dir,
this.app || ''
])
this.pipe = new FramedStream(this.IPC)
this.pipe.on('data', (data) => this._onmessage(data))
this.pipe.on('error', (err) => this.emit('error', err))
this.IPC.on('error', (err) => this.emit('error', err))
this.IPC.on('exit', (code) => {
if (code === 0 || this.closing !== null || this.closed) return
this.emit('error', new Error(`Updates worker exited with code ${code}`))
})
}
_close() {
const pipe = this.pipe
const IPC = this.IPC
this.pipe = null
this.IPC = null
pipe?.destroy()
IPC?.destroy()
}
_onmessage(data) {
const message = data.toString()
if (message === 'updating') {
this.emit('updating')
return
}
if (message === 'updated') {
this.emit('updated')
this._send('pear:applyUpdate')
return
}
if (message === 'pear:updateApplied') {
this.emit('update-applied')
return
}
this.emit('message', message)
}
_send(message) {
if (this.pipe === null) return
this.pipe.write(message)
}
async exit(code = 0) {
Bare.exitCode = code
await this.close()
}
}