Stop relying on QueryTargetWrapper in QueryTarget.

This commit is contained in:
Danila Fedorin 2021-08-10 12:29:18 -07:00
parent 1d5b105c34
commit e837a91a80
2 changed files with 20 additions and 10 deletions

View file

@ -15,14 +15,22 @@ limitations under the License.
*/ */
import {iterateCursor, reqAsPromise} from "./utils"; import {iterateCursor, reqAsPromise} from "./utils";
import {QueryTargetWrapper} from "./Store.js"
type Reducer<A,B> = (acc: B, val: A) => B type Reducer<A,B> = (acc: B, val: A) => B
export class QueryTarget<T> { interface QueryTargetWrapper<T> {
private _target: QueryTargetWrapper openCursor: (range?: IDBKeyRange | null , direction?: IDBCursorDirection) => IDBRequest<IDBCursorWithValue>
openKeyCursor: (range: IDBKeyRange, direction: IDBCursorDirection) => IDBRequest<IDBCursor>
supports: (method: string) => boolean
keyPath: string
get: (key: IDBValidKey) => IDBRequest<T>
getKey: (key: IDBValidKey) => IDBRequest<IDBValidKey>
}
constructor(target: QueryTargetWrapper) { export class QueryTarget<T> {
private _target: QueryTargetWrapper<T>
constructor(target: QueryTargetWrapper<T>) {
this._target = target; this._target = target;
} }
@ -111,7 +119,7 @@ export class QueryTarget<T> {
async findMaxKey(range: IDBKeyRange): Promise<IDBValidKey | undefined> { async findMaxKey(range: IDBKeyRange): Promise<IDBValidKey | undefined> {
const cursor = this._target.openKeyCursor(range, "prev"); const cursor = this._target.openKeyCursor(range, "prev");
let maxKey; let maxKey;
await iterateCursor(cursor, (_, key) => { await iterateCursor(cursor, (value, key) => {
maxKey = key; maxKey = key;
return {done: true}; return {done: true};
}); });
@ -126,7 +134,7 @@ export class QueryTarget<T> {
}); });
} }
async iterateKeys(range: IDBKeyRange, callback: (key: IDBValidKey, cur: IDBCursorWithValue) => boolean) { async iterateKeys(range: IDBKeyRange, callback: (key: IDBValidKey, cur: IDBCursor) => boolean) {
const cursor = this._target.openKeyCursor(range, "next"); const cursor = this._target.openKeyCursor(range, "next");
await iterateCursor(cursor, (_, key, cur) => { await iterateCursor(cursor, (_, key, cur) => {
return {done: callback(key, cur)}; return {done: callback(key, cur)};

View file

@ -124,9 +124,11 @@ export function txnAsPromise(txn): Promise<void> {
}); });
} }
type CursorIterator<T> = (value: T, key: IDBValidKey, cursor: IDBCursorWithValue) => { done: boolean, jumpTo?: IDBValidKey } type CursorIterator<T, I extends IDBCursor> = I extends IDBCursorWithValue ?
(value: T, key: IDBValidKey, cursor: IDBCursorWithValue) => { done: boolean, jumpTo?: IDBValidKey } :
(value: undefined, key: IDBValidKey, cursor: IDBCursor) => { done: boolean, jumpTo?: IDBValidKey }
export function iterateCursor<T>(cursorRequest: IDBRequest<IDBCursorWithValue>, processValue: CursorIterator<T>): Promise<boolean> { export function iterateCursor<T, I extends IDBCursor = IDBCursorWithValue>(cursorRequest: IDBRequest<I>, processValue: CursorIterator<T, I>): Promise<boolean> {
// TODO: does cursor already have a value here?? // TODO: does cursor already have a value here??
return new Promise<boolean>((resolve, reject) => { return new Promise<boolean>((resolve, reject) => {
cursorRequest.onerror = () => { cursorRequest.onerror = () => {
@ -136,14 +138,14 @@ export function iterateCursor<T>(cursorRequest: IDBRequest<IDBCursorWithValue>,
}; };
// collect results // collect results
cursorRequest.onsuccess = (event) => { cursorRequest.onsuccess = (event) => {
const cursor = (event.target as IDBRequest<IDBCursorWithValue>).result; const cursor = (event.target as IDBRequest<I>).result;
if (!cursor) { if (!cursor) {
resolve(false); resolve(false);
// @ts-ignore // @ts-ignore
needsSyncPromise && Promise._flush && Promise._flush(); needsSyncPromise && Promise._flush && Promise._flush();
return; // end of results return; // end of results
} }
const result = processValue(cursor.value, cursor.key, cursor); const result = processValue(cursor["value"], cursor.key, cursor);
// TODO: don't use object for result and assume it's jumpTo when not === true/false or undefined // TODO: don't use object for result and assume it's jumpTo when not === true/false or undefined
const done = result?.done; const done = result?.done;
const jumpTo = result?.jumpTo; const jumpTo = result?.jumpTo;