hypercore-storage 1.3.0 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +15 -9
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -28,37 +28,43 @@ class Atom {
28
28
  constructor (db) {
29
29
  this.db = db
30
30
  this.view = new View()
31
+ this.flushedPromise = null
31
32
  this.flushing = false
32
- this.preflushes = []
33
33
  this.flushes = []
34
34
  }
35
35
 
36
- onpreflush (fn) {
37
- this.preflushes.push(fn)
38
- }
39
-
40
36
  onflush (fn) {
41
37
  this.flushes.push(fn)
42
38
  }
43
39
 
40
+ flushed () {
41
+ if (!this.flushing) return Promise.resolve()
42
+ this.flushedPromise = rrp()
43
+ return this.flushedPromise.promise
44
+ }
45
+
46
+ _resolve () {
47
+ const f = this.flushedPromise
48
+ this.flushedPromise = null
49
+ f.resolve()
50
+ }
51
+
44
52
  async flush () {
45
53
  if (this.flushing) throw new Error('Atom already flushing')
46
54
  this.flushing = true
47
55
 
48
56
  try {
49
- let len = this.preflushes.length
50
- for (let i = 0; i < len; i++) this.preflushes[i]()
51
-
52
57
  await View.flush(this.view.changes, this.db)
53
58
  this.view.reset()
54
59
 
55
60
  const promises = []
56
- len = this.flushes.length // in case of reentry
61
+ const len = this.flushes.length // in case of reentry
57
62
  for (let i = 0; i < len; i++) promises.push(this.flushes[i]())
58
63
 
59
64
  await Promise.all(promises)
60
65
  } finally {
61
66
  this.flushing = false
67
+ if (this.flushedPromise !== null) this._resolve()
62
68
  }
63
69
  }
64
70
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore-storage",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "main": "index.js",
5
5
  "files": [
6
6
  "index.js",