Browse Source

make it a class and add removeListener

ameba23 3 years ago
parent
commit
5f1c7d2acc
1 changed files with 29 additions and 23 deletions
  1. 29
    23
      index.js

+ 29
- 23
index.js View File

@@ -1,35 +1,41 @@
1 1
 const rimraf = require('rimraf')
2
-const { mount, unmount } = require('hyperdrive-fuse')
2
+const hyperdriveFuse = require('hyperdrive-fuse')
3 3
 const mkdirp = require('mkdirp')
4 4
 
5
-module.exports = Mount
6
-module.exports.cleanup = cleanup
7
-module.exports.unmount = Unmount
5
+// module.exports = Mount
6
+// module.exports.cleanup = cleanup
7
+// module.exports.unmount = Unmount
8
+module.exports = () => new MountDrive()
9
+class MountDrive {
10
+  async mount (drive, dest) {
11
+    mkdirp.sync(dest)
12
+    var { destroy } = await hyperdriveFuse.mount(drive, dest)
13
+    this.destroy = destroy
14
+    this.dest = dest
8 15
 
9
-async function Mount (drive, dest) {
10
-  mkdirp.sync(dest)
11
-  var { destroy } = await mount(drive, dest)
16
+    process.once('SIGINT', this.callCleanup)
17
+    return destroy
18
+  }
12 19
 
13
-  process.once('SIGINT', () => (
14
-    cleanup(destroy, dest)
15
-  ))
20
+  callCleanup () {
21
+    this.cleanup(this.destroy, this.dest)
22
+  }
16 23
 
17
-  return destroy
18
-}
19
-
20
-function cleanup (destroy, dest) {
21
-  return new Promise((resolve, reject) => {
22
-    destroy(err => {
23
-      if (err) return reject(err)
24
-      rimraf(dest, err => {
24
+  cleanup (destroy, dest) {
25
+    return new Promise((resolve, reject) => {
26
+      destroy(err => {
25 27
         if (err) return reject(err)
26
-        return resolve()
28
+        process.removeListener('SIGINT', this.callCleanup)
29
+        rimraf(dest, err => {
30
+          if (err) return reject(err)
31
+          return resolve()
32
+        })
27 33
       })
28 34
     })
29
-  })
35
+  }
30 36
 }
31 37
 
32 38
 // TODO this should include cleanup
33
-function Unmount (dest, cb) {
34
-  return unmount(dest, cb)
35
-}
39
+// function Unmount (dest, cb) {
40
+//   return unmount(dest, cb)
41
+// }