Skip to content

Commit 06755b2

Browse files
committed
Merge branch 'orbitdb-master'
2 parents 6bdd32e + 90088a5 commit 06755b2

File tree

3 files changed

+30
-48
lines changed

3 files changed

+30
-48
lines changed

package-lock.json

Lines changed: 8 additions & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"orbit-db-io": "~0.2.0",
2929
"p-each-series": "^1.0.0",
3030
"p-map": "^3.0.0",
31+
"p-queue": "^6.2.1",
3132
"readable-stream": "~2.3.5"
3233
},
3334
"standard": {

src/Store.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const EventEmitter = require('events').EventEmitter
55
const Readable = require('readable-stream')
66
const toStream = require('it-to-stream')
77
const mapSeries = require('p-each-series')
8+
const { default: PQueue } = require('p-queue')
89
const Log = require('ipfs-log')
910
const Entry = Log.Entry
1011
const Index = require('./Index')
@@ -67,6 +68,9 @@ class Store {
6768
// Create the operations log
6869
this._oplog = new Log(this._ipfs, this.identity, { logId: this.id, access: this.access, sortFn: this.options.sortFn })
6970

71+
// _addOperation queue
72+
this._opqueue = new PQueue({ concurrency: 1 })
73+
7074
// Create the index
7175
this._index = new this.options.Index(this.address.root)
7276

@@ -173,6 +177,8 @@ class Store {
173177
await this.options.onClose(this)
174178
}
175179

180+
await this._opqueue.onIdle()
181+
176182
// Replicator teardown logic
177183
this._replicator.stop()
178184

@@ -501,20 +507,23 @@ class Store {
501507
}
502508

503509
async _addOperation (data, { onProgressCallback, pin = false } = {}) {
504-
if (this._oplog) {
505-
// check local cache?
506-
if (this.options.syncLocal) {
507-
await this.syncLocal()
508-
}
510+
async function addOperation () {
511+
if (this._oplog) {
512+
// check local cache?
513+
if (this.options.syncLocal) {
514+
await this.syncLocal()
515+
}
509516

510-
const entry = await this._oplog.append(data, this.options.referenceCount, pin)
511-
this._recalculateReplicationStatus(this.replicationStatus.progress + 1, entry.clock.time)
512-
await this._cache.set(this.localHeadsPath, [entry])
513-
await this._updateIndex()
514-
this.events.emit('write', this.address.toString(), entry, this._oplog.heads)
515-
if (onProgressCallback) onProgressCallback(entry)
516-
return entry.hash
517+
const entry = await this._oplog.append(data, this.options.referenceCount, pin)
518+
this._recalculateReplicationStatus(this.replicationStatus.progress + 1, entry.clock.time)
519+
await this._cache.set(this.localHeadsPath, [entry])
520+
await this._updateIndex()
521+
this.events.emit('write', this.address.toString(), entry, this._oplog.heads)
522+
if (onProgressCallback) onProgressCallback(entry)
523+
return entry.hash
524+
}
517525
}
526+
return this._opqueue.add(addOperation.bind(this))
518527
}
519528

520529
_addOperationBatch (data, batchOperation, lastOperation, onProgressCallback) {

0 commit comments

Comments
 (0)