chore(web): inlined iterator

This commit is contained in:
alina 🌸 2024-06-28 14:08:45 +03:00
parent 8df4c1a6b5
commit 1b5760b3fc
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI
2 changed files with 8 additions and 14 deletions

View file

@ -1,7 +1,7 @@
import { IReferenceMessagesRepository } from '@mtcute/core' import { IReferenceMessagesRepository } from '@mtcute/core'
import { IdbStorageDriver } from '../driver.js' import { IdbStorageDriver } from '../driver.js'
import { cursorToIterator, reqToPromise, txToPromise } from '../utils.js' import { reqToPromise, txToPromise } from '../utils.js'
const TABLE = 'messageRefs' const TABLE = 'messageRefs'
@ -62,8 +62,14 @@ export class IdbRefMsgRepository implements IReferenceMessagesRepository {
const os = tx.objectStore(TABLE) const os = tx.objectStore(TABLE)
const index = os.index('by_peer') const index = os.index('by_peer')
for await (const cursor of cursorToIterator(index.openCursor(peerId))) { const req = index.openCursor(peerId)
let cursor = await reqToPromise(req)
while (cursor) {
cursor.delete() cursor.delete()
cursor.continue()
cursor = await reqToPromise(req)
} }
return txToPromise(tx) return txToPromise(tx)

View file

@ -11,15 +11,3 @@ export function reqToPromise<T>(req: IDBRequest<T>): Promise<T> {
req.onerror = () => reject(req.error) req.onerror = () => reject(req.error)
}) })
} }
export async function* cursorToIterator<T extends IDBCursor>(
req: IDBRequest<T | null>,
): AsyncIterableIterator<T> {
let cursor = await reqToPromise(req)
while (cursor) {
yield cursor
cursor.continue()
cursor = await reqToPromise(req)
}
}