Lift transaction property to QueryTarget

This commit is contained in:
Danila Fedorin 2021-09-14 11:18:24 -07:00
parent 41e568f783
commit f8117b6f98
2 changed files with 16 additions and 17 deletions

View file

@ -15,6 +15,7 @@ limitations under the License.
*/
import {iterateCursor, DONE, NOT_DONE, reqAsPromise} from "./utils";
import {Transaction} from "./Transaction";
type Reducer<A,B> = (acc: B, val: A) => B
@ -31,13 +32,19 @@ interface QueryTargetInterface<T> {
export class QueryTarget<T> {
protected _target: QueryTargetInterface<T>;
protected _idbFactory: IDBFactory
protected _IDBKeyRange: typeof IDBKeyRange
protected _transaction: Transaction;
constructor(target: QueryTargetInterface<T>, idbFactory: IDBFactory, _IDBKeyRange: typeof IDBKeyRange) {
constructor(target: QueryTargetInterface<T>, 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<IDBCursorWithValue | null> {
@ -159,11 +166,11 @@ export class QueryTarget<T> {
*/
async findExistingKeys(keys: IDBValidKey[], backwards: boolean, callback: (key: IDBValidKey, found: boolean) => boolean): Promise<void> {
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) => {

View file

@ -128,16 +128,8 @@ class QueryTargetWrapper<T> {
}
export class Store<T> extends QueryTarget<T> {
private _transaction: Transaction;
constructor(idbStore: IDBObjectStore, transaction: Transaction) {
super(new QueryTargetWrapper<T>(idbStore), transaction.idbFactory, transaction.IDBKeyRange);
this._transaction = transaction;
}
get IDBKeyRange() {
// @ts-ignore
return this._transaction.IDBKeyRange;
super(new QueryTargetWrapper<T>(idbStore), transaction);
}
get _idbStore(): QueryTargetWrapper<T> {
@ -145,7 +137,7 @@ export class Store<T> extends QueryTarget<T> {
}
index(indexName: string): QueryTarget<T> {
return new QueryTarget<T>(new QueryTargetWrapper<T>(this._idbStore.index(indexName)), this._idbFactory, this._IDBKeyRange);
return new QueryTarget<T>(new QueryTargetWrapper<T>(this._idbStore.index(indexName)), this._transaction);
}
put(value: T): void {