UNPKG

@idxdb/promised

Version:

@idxdb/promised wraps the IndexedDB API. It allows you to easily store and retrieve data in an indexed db database using async/await syntax, making it easier to integrate with your existing codebase.

50 lines (49 loc) 1.25 kB
import { requestResolver } from '../shared/request-resolver.js'; export class KeyCursor { ctx; _cursor; _cursorPromise; constructor(ctx) { this.ctx = ctx; this._cursorPromise = requestResolver(ctx.request).then((cursor) => { this._cursor = cursor; return cursor; }); } get request() { return this._cursor?.request || this.ctx.request; } get key() { return this._cursor.key; } get primaryKey() { return this._cursor.primaryKey; } get direction() { return this.ctx.direction; } get source() { return this.ctx.source; } async end() { return this._cursorPromise.then((cursor) => !cursor); } continue(key) { this.stepUp().continue(key); } advance(count) { this.stepUp().advance(count); } continuePrimaryKey(key, primaryKey) { this.stepUp().continuePrimaryKey(key, primaryKey); } stepUp() { this._cursorPromise = requestResolver(this.ctx.request).then((cursor) => { this._cursor = cursor; return cursor; }); const cursor = this._cursor; this._cursor = null; return cursor; } }