forked from mystiq/hydrogen-web
Mock IDBKeyRange, too
This commit is contained in:
parent
71694787cd
commit
713f675f3a
3 changed files with 8 additions and 6 deletions
|
@ -32,10 +32,12 @@ interface QueryTargetInterface<T> {
|
||||||
export class QueryTarget<T> {
|
export class QueryTarget<T> {
|
||||||
protected _target: QueryTargetInterface<T>;
|
protected _target: QueryTargetInterface<T>;
|
||||||
protected _idbFactory: IDBFactory
|
protected _idbFactory: IDBFactory
|
||||||
|
protected _IDBKeyRange: typeof IDBKeyRange
|
||||||
|
|
||||||
constructor(target: QueryTargetInterface<T>, idbFactory: IDBFactory) {
|
constructor(target: QueryTargetInterface<T>, idbFactory: IDBFactory, _IDBKeyRange: typeof IDBKeyRange) {
|
||||||
this._target = target;
|
this._target = target;
|
||||||
this._idbFactory = idbFactory;
|
this._idbFactory = idbFactory;
|
||||||
|
this._IDBKeyRange = _IDBKeyRange;
|
||||||
}
|
}
|
||||||
|
|
||||||
_openCursor(range?: IDBQuery, direction?: IDBCursorDirection): IDBRequest<IDBCursorWithValue | null> {
|
_openCursor(range?: IDBQuery, direction?: IDBCursorDirection): IDBRequest<IDBCursorWithValue | null> {
|
||||||
|
@ -161,7 +163,7 @@ export class QueryTarget<T> {
|
||||||
const sortedKeys = keys.slice().sort(compareKeys);
|
const sortedKeys = keys.slice().sort(compareKeys);
|
||||||
const firstKey = backwards ? sortedKeys[sortedKeys.length - 1] : sortedKeys[0];
|
const firstKey = backwards ? sortedKeys[sortedKeys.length - 1] : sortedKeys[0];
|
||||||
const lastKey = backwards ? sortedKeys[0] : sortedKeys[sortedKeys.length - 1];
|
const lastKey = backwards ? sortedKeys[0] : sortedKeys[sortedKeys.length - 1];
|
||||||
const cursor = this._target.openKeyCursor(IDBKeyRange.bound(firstKey, lastKey), direction);
|
const cursor = this._target.openKeyCursor(this._IDBKeyRange.bound(firstKey, lastKey), direction);
|
||||||
let i = 0;
|
let i = 0;
|
||||||
let consumerDone = false;
|
let consumerDone = false;
|
||||||
await iterateCursor(cursor, (value, key) => {
|
await iterateCursor(cursor, (value, key) => {
|
||||||
|
|
|
@ -130,8 +130,8 @@ class QueryTargetWrapper<T> {
|
||||||
export class Store<T> extends QueryTarget<T> {
|
export class Store<T> extends QueryTarget<T> {
|
||||||
private _transaction: Transaction;
|
private _transaction: Transaction;
|
||||||
|
|
||||||
constructor(idbStore: IDBObjectStore, transaction: Transaction, idbFactory: IDBFactory) {
|
constructor(idbStore: IDBObjectStore, transaction: Transaction) {
|
||||||
super(new QueryTargetWrapper<T>(idbStore), idbFactory);
|
super(new QueryTargetWrapper<T>(idbStore), transaction.idbFactory, transaction.IDBKeyRange);
|
||||||
this._transaction = transaction;
|
this._transaction = transaction;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -145,7 +145,7 @@ export class Store<T> extends QueryTarget<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
index(indexName: string): QueryTarget<T> {
|
index(indexName: string): QueryTarget<T> {
|
||||||
return new QueryTarget<T>(new QueryTargetWrapper<T>(this._idbStore.index(indexName)), this._idbFactory);
|
return new QueryTarget<T>(new QueryTargetWrapper<T>(this._idbStore.index(indexName)), this._idbFactory, this._IDBKeyRange);
|
||||||
}
|
}
|
||||||
|
|
||||||
put(value: T): void {
|
put(value: T): void {
|
||||||
|
|
|
@ -56,7 +56,7 @@ export class Transaction {
|
||||||
// more specific error? this is a bug, so maybe not ...
|
// more specific error? this is a bug, so maybe not ...
|
||||||
throw new StorageError(`Invalid store for transaction: ${name}, only ${this._allowedStoreNames.join(", ")} are allowed.`);
|
throw new StorageError(`Invalid store for transaction: ${name}, only ${this._allowedStoreNames.join(", ")} are allowed.`);
|
||||||
}
|
}
|
||||||
return new Store(this._txn.objectStore(name), this, this.idbFactory);
|
return new Store(this._txn.objectStore(name), this);
|
||||||
}
|
}
|
||||||
|
|
||||||
_store<T>(name: StoreNames, mapStore: (idbStore: Store<any>) => T): T {
|
_store<T>(name: StoreNames, mapStore: (idbStore: Store<any>) => T): T {
|
||||||
|
|
Loading…
Reference in a new issue