-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.js
48 lines (42 loc) · 1.04 KB
/
api.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
'use strict'
const fs = require('fs')
const fsext = require('fs-native-extensions')
class Internal {
_pinging = true
_ping (method) { return () => this._pinging && method.request({ beat: 'ping' }) }
}
class API extends Internal {
#ipc = null
constructor (ipc) {
super()
this.#ipc = ipc
}
wakeup (method) {
return (link, storage, appdev) => method.request({ args: [link, storage, appdev] })
}
shutdown (method) {
return async () => {
if (this.#ipc.closed || this.#ipc.closing) return
this._pinging = false
method.send()
const fd = await new Promise((resolve, reject) => fs.open(this.#ipc._lock, 'r+', (err, fd) => {
if (err) {
reject(err)
return
}
resolve(fd)
}))
await fsext.waitForLock(fd)
fsext.unlock(fd)
await new Promise((resolve, reject) => fs.close(fd, (err) => {
if (err) {
reject(err)
return
}
resolve()
}))
await this.#ipc.close()
}
}
}
module.exports = API