load() should be idempotent

This commit is contained in:
Andre Staltz 2024-01-02 11:43:42 +02:00
parent cb1027b8b2
commit 650c0110dd
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
2 changed files with 10 additions and 0 deletions

View File

@ -319,6 +319,15 @@ function initSet(peer, config) {
*/ */
function load(id, cb) { function load(id, cb) {
assertDBPlugin(peer) assertDBPlugin(peer)
if (accountID === id) {
loaded(cb)
return
}
if (accountID !== null) {
// prettier-ignore
cb(new Error(`Cannot load Set for account "${id}" because Set for account "${accountID}" is already loaded`))
return
}
accountID = id accountID = id
loadPromise = new Promise((resolve, reject) => { loadPromise = new Promise((resolve, reject) => {
for (const rec of peer.db.records()) { for (const rec of peer.db.records()) {

View File

@ -31,6 +31,7 @@ test('setup', async (t) => {
_nonce: 'alice', _nonce: 'alice',
}) })
await p(peer.set.load)(aliceID) await p(peer.set.load)(aliceID)
await p(peer.set.load)(aliceID) // on purpose, test that re-load is idempotent
peer.set.setGhostSpan(4) peer.set.setGhostSpan(4)
assert.equal(peer.set.getGhostSpan(), 4, 'getGhostSpan') assert.equal(peer.set.getGhostSpan(), 4, 'getGhostSpan')