chore(web): inlined iterator
This commit is contained in:
parent
8df4c1a6b5
commit
1b5760b3fc
2 changed files with 8 additions and 14 deletions
|
@ -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)
|
||||||
|
|
|
@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue