diff --git a/src/matrix/storage/idb/QueryTarget.ts b/src/matrix/storage/idb/QueryTarget.ts index 8ed702da..3ab33a6b 100644 --- a/src/matrix/storage/idb/QueryTarget.ts +++ b/src/matrix/storage/idb/QueryTarget.ts @@ -15,6 +15,7 @@ limitations under the License. */ import {iterateCursor, DONE, NOT_DONE, reqAsPromise} from "./utils"; +import {Transaction} from "./Transaction"; type Reducer = (acc: B, val: A) => B @@ -31,13 +32,19 @@ interface QueryTargetInterface { export class QueryTarget { protected _target: QueryTargetInterface; - protected _idbFactory: IDBFactory - protected _IDBKeyRange: typeof IDBKeyRange + protected _transaction: Transaction; - constructor(target: QueryTargetInterface, idbFactory: IDBFactory, _IDBKeyRange: typeof IDBKeyRange) { + constructor(target: QueryTargetInterface, transaction: Transaction) { this._target = target; - this._idbFactory = idbFactory; - this._IDBKeyRange = _IDBKeyRange; + this._transaction = transaction; + } + + get idbFactory(): IDBFactory { + return this._transaction.idbFactory; + } + + get IDBKeyRange(): typeof IDBKeyRange { + return this._transaction.IDBKeyRange; } _openCursor(range?: IDBQuery, direction?: IDBCursorDirection): IDBRequest { @@ -159,11 +166,11 @@ export class QueryTarget { */ async findExistingKeys(keys: IDBValidKey[], backwards: boolean, callback: (key: IDBValidKey, found: boolean) => boolean): Promise { const direction = backwards ? "prev" : "next"; - const compareKeys = (a, b) => backwards ? -this._idbFactory.cmp(a, b) : this._idbFactory.cmp(a, b); + const compareKeys = (a, b) => backwards ? -this.idbFactory.cmp(a, b) : this.idbFactory.cmp(a, b); const sortedKeys = keys.slice().sort(compareKeys); const firstKey = backwards ? sortedKeys[sortedKeys.length - 1] : sortedKeys[0]; const lastKey = backwards ? sortedKeys[0] : sortedKeys[sortedKeys.length - 1]; - const cursor = this._target.openKeyCursor(this._IDBKeyRange.bound(firstKey, lastKey), direction); + const cursor = this._target.openKeyCursor(this.IDBKeyRange.bound(firstKey, lastKey), direction); let i = 0; let consumerDone = false; await iterateCursor(cursor, (value, key) => { diff --git a/src/matrix/storage/idb/Store.ts b/src/matrix/storage/idb/Store.ts index 1093e73d..0930888c 100644 --- a/src/matrix/storage/idb/Store.ts +++ b/src/matrix/storage/idb/Store.ts @@ -128,16 +128,8 @@ class QueryTargetWrapper { } export class Store extends QueryTarget { - private _transaction: Transaction; - constructor(idbStore: IDBObjectStore, transaction: Transaction) { - super(new QueryTargetWrapper(idbStore), transaction.idbFactory, transaction.IDBKeyRange); - this._transaction = transaction; - } - - get IDBKeyRange() { - // @ts-ignore - return this._transaction.IDBKeyRange; + super(new QueryTargetWrapper(idbStore), transaction); } get _idbStore(): QueryTargetWrapper { @@ -145,7 +137,7 @@ export class Store extends QueryTarget { } index(indexName: string): QueryTarget { - return new QueryTarget(new QueryTargetWrapper(this._idbStore.index(indexName)), this._idbFactory, this._IDBKeyRange); + return new QueryTarget(new QueryTargetWrapper(this._idbStore.index(indexName)), this._transaction); } put(value: T): void {