hypercore-storage 1.0.3 → 1.0.4
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 +15 -5
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -28,6 +28,7 @@ class Atom {
|
|
|
28
28
|
constructor (db) {
|
|
29
29
|
this.db = db
|
|
30
30
|
this.view = new View()
|
|
31
|
+
this.flushing = false
|
|
31
32
|
this.flushes = []
|
|
32
33
|
}
|
|
33
34
|
|
|
@@ -36,13 +37,21 @@ class Atom {
|
|
|
36
37
|
}
|
|
37
38
|
|
|
38
39
|
async flush () {
|
|
39
|
-
|
|
40
|
-
this.
|
|
40
|
+
if (this.flushing) throw new Error('Atom already flushing')
|
|
41
|
+
this.flushing = true
|
|
41
42
|
|
|
42
|
-
|
|
43
|
-
|
|
43
|
+
try {
|
|
44
|
+
await View.flush(this.view.changes, this.db)
|
|
45
|
+
this.view.reset()
|
|
46
|
+
|
|
47
|
+
const promises = []
|
|
48
|
+
const len = this.flushing.length // in case of reentry
|
|
49
|
+
for (let i = 0; i < len; i++) promises.push(this.flushes[i]())
|
|
44
50
|
|
|
45
|
-
|
|
51
|
+
await Promise.all(promises)
|
|
52
|
+
} finally {
|
|
53
|
+
this.flushing = false
|
|
54
|
+
}
|
|
46
55
|
}
|
|
47
56
|
}
|
|
48
57
|
|
|
@@ -99,6 +108,7 @@ class HypercoreStorage {
|
|
|
99
108
|
}
|
|
100
109
|
|
|
101
110
|
atomize (atom) {
|
|
111
|
+
if (this.atom && this.atom !== atom) throw new Error('Cannot atomize and atomized session with a new atom')
|
|
102
112
|
return new HypercoreStorage(this.store, this.db.session(), this.core, atom.view, atom)
|
|
103
113
|
}
|
|
104
114
|
|