Merge pull request #1099 from orbitdb/fix/stop

Close databases when orbitdb is stopped
This commit is contained in:
Hayden Young 2023-09-13 21:29:48 +08:00 committed by GitHub
commit 739103d66d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 4 deletions

View File

@ -174,6 +174,9 @@ const OrbitDB = async ({ ipfs, id, identity, identities, directory } = {}) => {
* @async * @async
*/ */
const stop = async () => { const stop = async () => {
for (const db of Object.values(databases)) {
await db.close()
}
if (keystore) { if (keystore) {
await keystore.close() await keystore.close()
} }

View File

@ -529,15 +529,18 @@ describe('Open databases', function () {
}) })
describe('opening same database', () => { describe('opening same database', () => {
let db let db1, db2
before(async () => { before(async () => {
orbitdb1 = await createOrbitDB({ ipfs: ipfs1, id: 'user1' }) orbitdb1 = await createOrbitDB({ ipfs: ipfs1, id: 'user1' })
}) })
after(async () => { after(async () => {
if (db) { if (db1) {
await db.close() await db1.close()
}
if (db2) {
await db2.close()
} }
if (orbitdb1) { if (orbitdb1) {
await orbitdb1.stop() await orbitdb1.stop()
@ -547,7 +550,6 @@ describe('Open databases', function () {
it('returns the database instance when opened with a name multiple times', async () => { it('returns the database instance when opened with a name multiple times', async () => {
let err let err
let db1, db2
try { try {
db1 = await orbitdb1.open('helloworld1') db1 = await orbitdb1.open('helloworld1')
@ -562,4 +564,36 @@ describe('Open databases', function () {
strictEqual(db1.address, db2.address) strictEqual(db1.address, db2.address)
}) })
}) })
describe('opening same database after stopping OrbitDB', () => {
let orbitdb
let db
after(async () => {
if (db) {
await db.close()
}
if (orbitdb) {
await orbitdb.stop()
}
await rmrf('./orbitdb')
})
it('returns the database instance', async () => {
let err
try {
orbitdb = await createOrbitDB({ ipfs: ipfs1, id: 'user1' })
db = await orbitdb.open('helloworld1')
await orbitdb.stop()
orbitdb = await createOrbitDB({ ipfs: ipfs1, id: 'user1' })
db = await orbitdb.open('helloworld1')
} catch (e) {
err = e
}
strictEqual(err, undefined)
strictEqual(db.name, 'helloworld1')
})
})
}) })