Browse Source

Reused ready.js to enqueue multifeed#close() calls

Tony Ivanov 2 years ago
parent
commit
84e1af6fd7
2 changed files with 23 additions and 7 deletions
  1. 6
    1
      index.js
  2. 17
    6
      ready.js

+ 6
- 1
index.js View File

@@ -30,6 +30,7 @@ function Multifeed (hypercore, storage, opts) {
30 30
 
31 31
   this.writerLock = mutexify()
32 32
 
33
+  this._close = readyify(_close.bind(this), true)
33 34
   this.closed = false
34 35
 
35 36
   // random-access-storage wrapper that wraps all hypercores in a directory
@@ -93,9 +94,13 @@ Multifeed.prototype.ready = function (cb) {
93 94
 }
94 95
 
95 96
 Multifeed.prototype.close = function (cb) {
96
-  var self = this
97 97
   if (typeof cb !== 'function') cb = function noop () {}
98
+  return this._close(cb)
99
+}
98 100
 
101
+function _close (cb) {
102
+  var self = this
103
+  console.log('CLOSE CALLDED')
99 104
   this.writerLock(function (release) {
100 105
     function done (err) {
101 106
       release(function () {

+ 17
- 6
ready.js View File

@@ -1,13 +1,24 @@
1
-module.exports = function (work) {
1
+module.exports = function (work, noauto) {
2 2
   var ready = false
3 3
   var fns = []
4
-  process.nextTick(function () {
5
-    work(function () {
6
-      ready = true
7
-      fns.forEach(process.nextTick)
4
+
5
+  var startWork = function () {
6
+    process.nextTick(function () {
7
+      work(function () {
8
+        ready = true
9
+        fns.forEach(process.nextTick)
10
+      })
8 11
     })
9
-  })
12
+  }
13
+
14
+  if (!noauto) startWork() // default behaviour
15
+
10 16
   return function (fn) {
17
+    if (noauto) { // start on first invocation
18
+      noauto = false
19
+      startWork()
20
+    }
21
+
11 22
     if (!ready) fns.push(fn)
12 23
     else process.nextTick(fn)
13 24
   }