From e837a91a80c9df6c455231d808084210645c1a21 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Tue, 10 Aug 2021 12:29:18 -0700 Subject: [PATCH] Stop relying on QueryTargetWrapper in QueryTarget. --- src/matrix/storage/idb/QueryTarget.ts | 20 ++++++++++++++------ src/matrix/storage/idb/utils.ts | 10 ++++++---- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/matrix/storage/idb/QueryTarget.ts b/src/matrix/storage/idb/QueryTarget.ts index 4e8fa7f4..27baf233 100644 --- a/src/matrix/storage/idb/QueryTarget.ts +++ b/src/matrix/storage/idb/QueryTarget.ts @@ -15,14 +15,22 @@ limitations under the License. */ import {iterateCursor, reqAsPromise} from "./utils"; -import {QueryTargetWrapper} from "./Store.js" type Reducer = (acc: B, val: A) => B -export class QueryTarget { - private _target: QueryTargetWrapper +interface QueryTargetWrapper { + openCursor: (range?: IDBKeyRange | null , direction?: IDBCursorDirection) => IDBRequest + openKeyCursor: (range: IDBKeyRange, direction: IDBCursorDirection) => IDBRequest + supports: (method: string) => boolean + keyPath: string + get: (key: IDBValidKey) => IDBRequest + getKey: (key: IDBValidKey) => IDBRequest +} - constructor(target: QueryTargetWrapper) { +export class QueryTarget { + private _target: QueryTargetWrapper + + constructor(target: QueryTargetWrapper) { this._target = target; } @@ -111,7 +119,7 @@ export class QueryTarget { async findMaxKey(range: IDBKeyRange): Promise { const cursor = this._target.openKeyCursor(range, "prev"); let maxKey; - await iterateCursor(cursor, (_, key) => { + await iterateCursor(cursor, (value, key) => { maxKey = key; return {done: true}; }); @@ -126,7 +134,7 @@ export class QueryTarget { }); } - 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"); await iterateCursor(cursor, (_, key, cur) => { return {done: callback(key, cur)}; diff --git a/src/matrix/storage/idb/utils.ts b/src/matrix/storage/idb/utils.ts index 722c180d..53f391e8 100644 --- a/src/matrix/storage/idb/utils.ts +++ b/src/matrix/storage/idb/utils.ts @@ -124,9 +124,11 @@ export function txnAsPromise(txn): Promise { }); } -type CursorIterator = (value: T, key: IDBValidKey, cursor: IDBCursorWithValue) => { done: boolean, jumpTo?: IDBValidKey } +type CursorIterator = 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(cursorRequest: IDBRequest, processValue: CursorIterator): Promise { +export function iterateCursor(cursorRequest: IDBRequest, processValue: CursorIterator): Promise { // TODO: does cursor already have a value here?? return new Promise((resolve, reject) => { cursorRequest.onerror = () => { @@ -136,14 +138,14 @@ export function iterateCursor(cursorRequest: IDBRequest, }; // collect results cursorRequest.onsuccess = (event) => { - const cursor = (event.target as IDBRequest).result; + const cursor = (event.target as IDBRequest).result; if (!cursor) { resolve(false); // @ts-ignore needsSyncPromise && Promise._flush && Promise._flush(); 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 const done = result?.done; const jumpTo = result?.jumpTo;