Browse Source

exit from fuse gracefully

Kieran Gibb 3 years ago
parent
commit
ae7dacb3dc
No account linked to committer's email address
2 changed files with 25 additions and 30 deletions
  1. 7
    7
      example.js
  2. 18
    23
      index.js

+ 7
- 7
example.js View File

@@ -5,15 +5,15 @@ const mountdir = './mnt'
5 5
 const storage = './drive'
6 6
 const drive = KappaDrive(storage)
7 7
 
8
-const mounter = Mount()
8
+const mount = Mount()
9 9
 
10 10
 drive.ready(() => {
11
-  mounter.mount(drive, mountdir).then((destroy) => {
12
-    // Drive is ready and mounted...
11
+  mount(drive, mountdir, (err, done) => {
12
+    if (err) return console.error(err)
13
+
13 14
     setTimeout(() => {
14
-      // Waiting 30 seconds for demonstration purposes
15
-      // then terminate the mount
16
-      mounter.cleanup(destroy, mountdir)
17
-    }, 30000)
15
+      console.log("DONE", done)
16
+      if (done) done()
17
+    }, 3000)
18 18
   })
19 19
 })

+ 18
- 23
index.js View File

@@ -1,34 +1,29 @@
1 1
 const rimraf = require('rimraf')
2
-const hyperdriveFuse = require('hyperdrive-fuse')
2
+const fuse = require('hyperdrive-fuse')
3 3
 const mkdirp = require('mkdirp')
4 4
 
5
-module.exports = () => new MountDrive()
6
-class MountDrive {
7
-  async mount (drive, dest, opts) {
8
-    mkdirp.sync(dest)
5
+module.exports = function () {
6
+  return function mount (drive, dir, cb) {
7
+    mkdirp.sync(dir)
9 8
 
10
-    var { destroy } = await hyperdriveFuse.mount(drive, dest, opts)
11
-    this.destroy = destroy
12
-    this.dest = dest
9
+    var mounting = fuse.mount(drive, dir)
10
+    mounting.catch(cb)
13 11
 
14
-    process.once('SIGINT', this.callCleanup)
15
-    return destroy
16
-  }
12
+    mounting.then(({ destroy }) => {
13
+      process.on('SIGINT', done)
17 14
 
18
-  callCleanup () {
19
-    this.cleanup(this.destroy, this.dest)
20
-  }
15
+      return cb(null, done)
21 16
 
22
-  cleanup (destroy, dest) {
23
-    return new Promise((resolve, reject) => {
24
-      destroy(err => {
25
-        if (err) return reject(err)
26
-        process.removeListener('SIGINT', this.callCleanup)
27
-        rimraf(dest, err => {
28
-          if (err) return reject(err)
29
-          return resolve()
17
+      function done () {
18
+        destroy((err) => {
19
+          if (err) throw err
20
+          process.removeListener('SIGINT', done)
21
+          rimraf(dir, (err) => {
22
+            if (err) return cb(err)
23
+            return cb()
24
+          })
30 25
         })
31
-      })
26
+      }
32 27
     })
33 28
   }
34 29
 }