diff --git a/src/matrix/storage/idb/QueryTarget.ts b/src/matrix/storage/idb/QueryTarget.ts index e3b77810..c8fc8df1 100644 --- a/src/matrix/storage/idb/QueryTarget.ts +++ b/src/matrix/storage/idb/QueryTarget.ts @@ -31,9 +31,11 @@ interface QueryTargetInterface { export class QueryTarget { protected _target: QueryTargetInterface; + protected _idbFactory: IDBFactory - constructor(target: QueryTargetInterface) { + constructor(target: QueryTargetInterface, idbFactory: IDBFactory) { this._target = target; + this._idbFactory = idbFactory; } _openCursor(range?: IDBQuery, direction?: IDBCursorDirection): IDBRequest { @@ -155,7 +157,7 @@ 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 ? -indexedDB.cmp(a, b) : indexedDB.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]; diff --git a/src/matrix/storage/idb/Storage.ts b/src/matrix/storage/idb/Storage.ts index bffdb642..7e02c111 100644 --- a/src/matrix/storage/idb/Storage.ts +++ b/src/matrix/storage/idb/Storage.ts @@ -23,11 +23,13 @@ const WEBKITEARLYCLOSETXNBUG_BOGUS_KEY = "782rh281re38-boguskey"; export class Storage { private _db: IDBDatabase; private _hasWebkitEarlyCloseTxnBug: boolean; + private _idbFactory: IDBFactory private _IDBKeyRange: typeof IDBKeyRange storeNames: typeof StoreNames; - constructor(idbDatabase: IDBDatabase, _IDBKeyRange: typeof IDBKeyRange, hasWebkitEarlyCloseTxnBug: boolean) { + constructor(idbDatabase: IDBDatabase, idbFactory: IDBFactory, _IDBKeyRange: typeof IDBKeyRange, hasWebkitEarlyCloseTxnBug: boolean) { this._db = idbDatabase; + this._idbFactory = idbFactory; this._IDBKeyRange = _IDBKeyRange; this._hasWebkitEarlyCloseTxnBug = hasWebkitEarlyCloseTxnBug; this.storeNames = StoreNames; @@ -49,7 +51,7 @@ export class Storage { if (this._hasWebkitEarlyCloseTxnBug) { await reqAsPromise(txn.objectStore(storeNames[0]).get(WEBKITEARLYCLOSETXNBUG_BOGUS_KEY)); } - return new Transaction(txn, storeNames, this._IDBKeyRange); + return new Transaction(txn, storeNames, this._idbFactory, this._IDBKeyRange); } catch(err) { throw new StorageError("readTxn failed", err); } @@ -64,7 +66,7 @@ export class Storage { if (this._hasWebkitEarlyCloseTxnBug) { await reqAsPromise(txn.objectStore(storeNames[0]).get(WEBKITEARLYCLOSETXNBUG_BOGUS_KEY)); } - return new Transaction(txn, storeNames, this._IDBKeyRange); + return new Transaction(txn, storeNames, this._idbFactory, this._IDBKeyRange); } catch(err) { throw new StorageError("readWriteTxn failed", err); } diff --git a/src/matrix/storage/idb/StorageFactory.ts b/src/matrix/storage/idb/StorageFactory.ts index 6db78c36..55e6f604 100644 --- a/src/matrix/storage/idb/StorageFactory.ts +++ b/src/matrix/storage/idb/StorageFactory.ts @@ -70,7 +70,7 @@ export class StorageFactory { const hasWebkitEarlyCloseTxnBug = await detectWebkitEarlyCloseTxnBug(this._idbFactory); const db = await openDatabaseWithSessionId(sessionId, this._idbFactory, log); - return new Storage(db, this._IDBKeyRange, hasWebkitEarlyCloseTxnBug); + return new Storage(db, this._idbFactory, this._IDBKeyRange, hasWebkitEarlyCloseTxnBug); } delete(sessionId: string): Promise { diff --git a/src/matrix/storage/idb/Store.ts b/src/matrix/storage/idb/Store.ts index e2da0707..6dafd90e 100644 --- a/src/matrix/storage/idb/Store.ts +++ b/src/matrix/storage/idb/Store.ts @@ -130,8 +130,8 @@ class QueryTargetWrapper { export class Store extends QueryTarget { private _transaction: Transaction; - constructor(idbStore: IDBObjectStore, transaction: Transaction) { - super(new QueryTargetWrapper(idbStore)); + constructor(idbStore: IDBObjectStore, transaction: Transaction, idbFactory: IDBFactory) { + super(new QueryTargetWrapper(idbStore), idbFactory); this._transaction = transaction; } @@ -145,7 +145,7 @@ export class Store extends QueryTarget { } index(indexName: string): QueryTarget { - return new QueryTarget(new QueryTargetWrapper(this._idbStore.index(indexName))); + return new QueryTarget(new QueryTargetWrapper(this._idbStore.index(indexName)), this._idbFactory); } put(value: T): void { diff --git a/src/matrix/storage/idb/Transaction.ts b/src/matrix/storage/idb/Transaction.ts index ea21b745..1eaf0caf 100644 --- a/src/matrix/storage/idb/Transaction.ts +++ b/src/matrix/storage/idb/Transaction.ts @@ -40,13 +40,15 @@ export class Transaction { private _txn: IDBTransaction; private _allowedStoreNames: StoreNames[]; private _stores: { [storeName in StoreNames]?: any }; + idbFactory: IDBFactory + IDBKeyRange: typeof IDBKeyRange - constructor(txn: IDBTransaction, allowedStoreNames: StoreNames[], IDBKeyRange) { + constructor(txn: IDBTransaction, allowedStoreNames: StoreNames[], _idbFactory: IDBFactory, _IDBKeyRange: typeof IDBKeyRange) { this._txn = txn; this._allowedStoreNames = allowedStoreNames; this._stores = {}; - // @ts-ignore - this.IDBKeyRange = IDBKeyRange; + this.idbFactory = _idbFactory; + this.IDBKeyRange = _IDBKeyRange; } _idbStore(name: StoreNames): Store { @@ -54,7 +56,7 @@ export class Transaction { // more specific error? this is a bug, so maybe not ... throw new StorageError(`Invalid store for transaction: ${name}, only ${this._allowedStoreNames.join(", ")} are allowed.`); } - return new Store(this._txn.objectStore(name), this); + return new Store(this._txn.objectStore(name), this, this.idbFactory); } _store(name: StoreNames, mapStore: (idbStore: Store) => T): T {