Add type annotations to Storage
This commit is contained in:
parent
31c951c68c
commit
287242deda
1 changed files with 11 additions and 4 deletions
|
@ -21,8 +21,13 @@ import { reqAsPromise } from "./utils";
|
||||||
const WEBKITEARLYCLOSETXNBUG_BOGUS_KEY = "782rh281re38-boguskey";
|
const WEBKITEARLYCLOSETXNBUG_BOGUS_KEY = "782rh281re38-boguskey";
|
||||||
|
|
||||||
export class Storage {
|
export class Storage {
|
||||||
constructor(idbDatabase, IDBKeyRange, hasWebkitEarlyCloseTxnBug) {
|
private _db: IDBDatabase
|
||||||
|
private _hasWebkitEarlyCloseTxnBug: boolean
|
||||||
|
storeNames: { [ name : string] : string }
|
||||||
|
|
||||||
|
constructor(idbDatabase: IDBDatabase, IDBKeyRange, hasWebkitEarlyCloseTxnBug: boolean) {
|
||||||
this._db = idbDatabase;
|
this._db = idbDatabase;
|
||||||
|
// @ts-ignore
|
||||||
this._IDBKeyRange = IDBKeyRange;
|
this._IDBKeyRange = IDBKeyRange;
|
||||||
this._hasWebkitEarlyCloseTxnBug = hasWebkitEarlyCloseTxnBug;
|
this._hasWebkitEarlyCloseTxnBug = hasWebkitEarlyCloseTxnBug;
|
||||||
const nameMap = STORE_NAMES.reduce((nameMap, name) => {
|
const nameMap = STORE_NAMES.reduce((nameMap, name) => {
|
||||||
|
@ -32,14 +37,14 @@ export class Storage {
|
||||||
this.storeNames = Object.freeze(nameMap);
|
this.storeNames = Object.freeze(nameMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
_validateStoreNames(storeNames) {
|
_validateStoreNames(storeNames: string[]) {
|
||||||
const idx = storeNames.findIndex(name => !STORE_NAMES.includes(name));
|
const idx = storeNames.findIndex(name => !STORE_NAMES.includes(name));
|
||||||
if (idx !== -1) {
|
if (idx !== -1) {
|
||||||
throw new StorageError(`Tried top, a transaction unknown store ${storeNames[idx]}`);
|
throw new StorageError(`Tried top, a transaction unknown store ${storeNames[idx]}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async readTxn(storeNames) {
|
async readTxn(storeNames: string[]): Promise<Transaction> {
|
||||||
this._validateStoreNames(storeNames);
|
this._validateStoreNames(storeNames);
|
||||||
try {
|
try {
|
||||||
const txn = this._db.transaction(storeNames, "readonly");
|
const txn = this._db.transaction(storeNames, "readonly");
|
||||||
|
@ -48,13 +53,14 @@ export class Storage {
|
||||||
if (this._hasWebkitEarlyCloseTxnBug) {
|
if (this._hasWebkitEarlyCloseTxnBug) {
|
||||||
await reqAsPromise(txn.objectStore(storeNames[0]).get(WEBKITEARLYCLOSETXNBUG_BOGUS_KEY));
|
await reqAsPromise(txn.objectStore(storeNames[0]).get(WEBKITEARLYCLOSETXNBUG_BOGUS_KEY));
|
||||||
}
|
}
|
||||||
|
// @ts-ignore
|
||||||
return new Transaction(txn, storeNames, this._IDBKeyRange);
|
return new Transaction(txn, storeNames, this._IDBKeyRange);
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
throw new StorageError("readTxn failed", err);
|
throw new StorageError("readTxn failed", err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async readWriteTxn(storeNames) {
|
async readWriteTxn(storeNames: string[]): Promise<Transaction> {
|
||||||
this._validateStoreNames(storeNames);
|
this._validateStoreNames(storeNames);
|
||||||
try {
|
try {
|
||||||
const txn = this._db.transaction(storeNames, "readwrite");
|
const txn = this._db.transaction(storeNames, "readwrite");
|
||||||
|
@ -63,6 +69,7 @@ export class Storage {
|
||||||
if (this._hasWebkitEarlyCloseTxnBug) {
|
if (this._hasWebkitEarlyCloseTxnBug) {
|
||||||
await reqAsPromise(txn.objectStore(storeNames[0]).get(WEBKITEARLYCLOSETXNBUG_BOGUS_KEY));
|
await reqAsPromise(txn.objectStore(storeNames[0]).get(WEBKITEARLYCLOSETXNBUG_BOGUS_KEY));
|
||||||
}
|
}
|
||||||
|
// @ts-ignore
|
||||||
return new Transaction(txn, storeNames, this._IDBKeyRange);
|
return new Transaction(txn, storeNames, this._IDBKeyRange);
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
throw new StorageError("readWriteTxn failed", err);
|
throw new StorageError("readWriteTxn failed", err);
|
||||||
|
|
Reference in a new issue