Skip to content

Commit

Permalink
fix: fix IndexedDB reset function (#1199)
Browse files Browse the repository at this point in the history
Thanks to @zhanleewo for the patch! I've added a test to verify this
fixes the issue.

---------

Co-authored-by: Edward <[email protected]>
  • Loading branch information
travis and zhanleewo authored Nov 29, 2023
1 parent f51b066 commit 48cf555
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/access-client/src/drivers/indexeddb.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export class IndexedDBDriver {
async reset() {
const db = await this.#getOpenDB()

withObjectStore(db, 'readwrite', this.#dbStoreName, (s) => {
const clear = withObjectStore(db, 'readwrite', this.#dbStoreName, (s) => {
/** @type {import('p-defer').DeferredPromise<void>} */
const { resolve, reject, promise } = defer()
const req = s.clear()
Expand All @@ -156,6 +156,8 @@ export class IndexedDBDriver {

return promise
})

await clear()
}
}

Expand Down
16 changes: 16 additions & 0 deletions packages/access-client/test/stores/store-indexeddb.browser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,20 @@ describe('IndexedDB store', () => {
assert.equal(del1.capabilities[0].can, del0.capabilities[0].can)
assert.equal(del1.capabilities[0].with, del0.capabilities[0].with)
})

it('should be resettable', async () => {
const principal = await RSASigner.generate({ extractable: false })
const data = await AgentData.create({ principal })

const store = new StoreIndexedDB('test-access-db-' + Date.now())
await store.open()
await store.save(data.export())

const exportData = await store.load()
assert.equal(exportData?.principal.id, principal.did())

await store.reset()
const resetExportData = await store.load()
assert.equal(resetExportData?.principal.id, undefined)
})
})

0 comments on commit 48cf555

Please sign in to comment.