Skip to content

Commit

Permalink
Indexed DB .next update
Browse files Browse the repository at this point in the history
  • Loading branch information
arietrouw committed Dec 31, 2024
1 parent 61a0564 commit a16833c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,23 +196,23 @@ export class IndexedDbArchivist<
cursor?: Hex,
): Promise<WithStorageMeta[]> {
// TODO: We have to handle the case where the cursor is not found, and then find the correct cursor to start with (thunked cursor)

return await useReadOnlyStore(db, storeName, async (store) => {
const sequenceIndex = assertEx(store.index(IndexedDbArchivist.sequenceIndexName), () => 'Failed to get sequence index')
let sequenceCursor: IDBPCursorWithValue<PayloadStore, [string]> | null | undefined
const parsedCursor = cursor === SequenceConstants.minLocalSequence ? null : cursor
sequenceCursor = assertEx(await sequenceIndex.openCursor(
null,
const parsedCursor = cursor
? order === 'asc'
? IDBKeyRange.lowerBound(cursor, false)
: IDBKeyRange.upperBound(cursor, false)
: null

sequenceCursor = await sequenceIndex.openCursor(
parsedCursor,
order === 'desc' ? 'prev' : 'next',
), () => `Failed to get cursor [${parsedCursor}, ${cursor}]`)
if (!sequenceCursor?.value) return []
try {
sequenceCursor = parsedCursor
? sequenceCursor.value._sequence === parsedCursor
? await sequenceCursor?.advance(1)
: await (await sequenceCursor?.continue(parsedCursor))?.advance(1)
: sequenceCursor // advance to skip the initial value
} catch {
return []
)

if (cursor) {
sequenceCursor = await sequenceCursor?.advance(1)
}

let remaining = limit
Expand All @@ -221,14 +221,14 @@ export class IndexedDbArchivist<
const value = sequenceCursor?.value
if (value) {
result.push(value)
try {
sequenceCursor = await sequenceCursor?.advance(1)
} catch {
break
}
if (sequenceCursor === null) {
break
}
}
try {
sequenceCursor = await sequenceCursor?.advance(1)
} catch {
break
}
if (sequenceCursor === null) {
break
}
remaining--
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ describe('IndexedDbArchivist', () => {

const batch2 = await archivist.next?.({ limit: 2, cursor: batch1?.[1]._sequence })
expect(batch2.length).toBe(2)
expect(await PayloadBuilder.dataHash(batch2?.[0])).toEqual(await PayloadBuilder.dataHash(payloads3[0]))
expect(await PayloadBuilder.dataHash(batch2?.[1])).toEqual(await PayloadBuilder.dataHash(payloads4[0]))

const batch3 = await archivist.next?.({ limit: 20 })
expect(batch3.length).toBe(4)
Expand Down

0 comments on commit a16833c

Please sign in to comment.