hypercore-storage 1.3.0 → 1.3.2
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.
- package/index.js +16 -9
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -28,37 +28,44 @@ 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
|
+
if (this.flushedPromise === null) return this.flushedPromise.promise
|
|
43
|
+
this.flushedPromise = rrp()
|
|
44
|
+
return this.flushedPromise.promise
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
_resolve () {
|
|
48
|
+
const f = this.flushedPromise
|
|
49
|
+
this.flushedPromise = null
|
|
50
|
+
f.resolve()
|
|
51
|
+
}
|
|
52
|
+
|
|
44
53
|
async flush () {
|
|
45
54
|
if (this.flushing) throw new Error('Atom already flushing')
|
|
46
55
|
this.flushing = true
|
|
47
56
|
|
|
48
57
|
try {
|
|
49
|
-
let len = this.preflushes.length
|
|
50
|
-
for (let i = 0; i < len; i++) this.preflushes[i]()
|
|
51
|
-
|
|
52
58
|
await View.flush(this.view.changes, this.db)
|
|
53
59
|
this.view.reset()
|
|
54
60
|
|
|
55
61
|
const promises = []
|
|
56
|
-
len = this.flushes.length // in case of reentry
|
|
62
|
+
const len = this.flushes.length // in case of reentry
|
|
57
63
|
for (let i = 0; i < len; i++) promises.push(this.flushes[i]())
|
|
58
64
|
|
|
59
65
|
await Promise.all(promises)
|
|
60
66
|
} finally {
|
|
61
67
|
this.flushing = false
|
|
68
|
+
if (this.flushedPromise !== null) this._resolve()
|
|
62
69
|
}
|
|
63
70
|
}
|
|
64
71
|
}
|