Skip to content
This repository was archived by the owner on Sep 30, 2023. It is now read-only.

Commit f54360a

Browse files
committed
Make updateIndex() calls asynchronous
1 parent 5188f66 commit f54360a

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

src/Index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class Index {
4747
@param oplog - the source operations log that called updateIndex
4848
@param entries - operations that were added to the log
4949
*/
50-
updateIndex (oplog, entries) {
50+
async updateIndex (oplog, entries) {
5151
this._index = oplog.values
5252
}
5353
}

src/Store.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,7 @@ class Store {
102102
}
103103
this._replicationStatus.queued -= logs.length
104104
this._replicationStatus.buffered = this._replicator._buffer.length
105-
this._recalculateReplicationMax()
106-
this._index.updateIndex(this._oplog)
107-
this._recalculateReplicationProgress()
105+
await this._updateIndex()
108106
// logger.debug(`<replicated>`)
109107
this.events.emit('replicated', this.address.toString(), logs.length)
110108
} catch (e) {
@@ -200,12 +198,11 @@ class Store {
200198
this._recalculateReplicationMax(head.clock.time)
201199
let log = await Log.fromEntryHash(this._ipfs, head.hash, this._oplog.id, amount, this._oplog.values, this.key, this.access.write, this._onLoadProgress.bind(this))
202200
await this._oplog.join(log, amount, this._oplog.id)
203-
this._recalculateReplicationProgress()
204201
})
205202

206203
// Update the index
207204
if (heads.length > 0)
208-
this._index.updateIndex(this._oplog)
205+
await this._updateIndex()
209206

210207
this.events.emit('ready', this.address.toString(), this._oplog.heads)
211208
}
@@ -392,9 +389,7 @@ class Store {
392389
if (snapshotData) {
393390
const log = await Log.fromJSON(this._ipfs, snapshotData, -1, this._key, this.access.write, 1000, onProgress)
394391
await this._oplog.join(log, -1, this._oplog.id)
395-
this._recalculateReplicationMax()
396-
this._index.updateIndex(this._oplog)
397-
this._recalculateReplicationProgress()
392+
await this._updateIndex()
398393
this.events.emit('replicated', this.address.toString())
399394
}
400395
this.events.emit('ready', this.address.toString(), this._oplog.heads)
@@ -405,12 +400,18 @@ class Store {
405400
return this
406401
}
407402

403+
async _updateIndex () {
404+
this._recalculateReplicationMax()
405+
await this._index.updateIndex(this._oplog)
406+
this._recalculateReplicationProgress()
407+
}
408+
408409
async _addOperation (data, batchOperation, lastOperation, onProgressCallback) {
409410
if (this._oplog) {
410411
const entry = await this._oplog.append(data, this.options.referenceCount)
411412
this._recalculateReplicationStatus(this.replicationStatus.progress + 1, entry.clock.time)
412413
await this._cache.set('_localHeads', [entry])
413-
this._index.updateIndex(this._oplog)
414+
await this._updateIndex()
414415
this.events.emit('write', this.address.toString(), entry, this._oplog.heads)
415416
if (onProgressCallback) onProgressCallback(entry)
416417
return entry.hash

0 commit comments

Comments
 (0)