Add type annotations to Storage

This commit is contained in:
Danila Fedorin 2021-08-12 13:10:13 -07:00
parent 31c951c68c
commit 287242deda

View file

@ -21,8 +21,13 @@ import { reqAsPromise } from "./utils";
const WEBKITEARLYCLOSETXNBUG_BOGUS_KEY = "782rh281re38-boguskey";
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;
// @ts-ignore
this._IDBKeyRange = IDBKeyRange;
this._hasWebkitEarlyCloseTxnBug = hasWebkitEarlyCloseTxnBug;
const nameMap = STORE_NAMES.reduce((nameMap, name) => {
@ -32,14 +37,14 @@ export class Storage {
this.storeNames = Object.freeze(nameMap);
}
_validateStoreNames(storeNames) {
_validateStoreNames(storeNames: string[]) {
const idx = storeNames.findIndex(name => !STORE_NAMES.includes(name));
if (idx !== -1) {
throw new StorageError(`Tried top, a transaction unknown store ${storeNames[idx]}`);
}
}
async readTxn(storeNames) {
async readTxn(storeNames: string[]): Promise<Transaction> {
this._validateStoreNames(storeNames);
try {
const txn = this._db.transaction(storeNames, "readonly");
@ -48,13 +53,14 @@ export class Storage {
if (this._hasWebkitEarlyCloseTxnBug) {
await reqAsPromise(txn.objectStore(storeNames[0]).get(WEBKITEARLYCLOSETXNBUG_BOGUS_KEY));
}
// @ts-ignore
return new Transaction(txn, storeNames, this._IDBKeyRange);
} catch(err) {
throw new StorageError("readTxn failed", err);
}
}
async readWriteTxn(storeNames) {
async readWriteTxn(storeNames: string[]): Promise<Transaction> {
this._validateStoreNames(storeNames);
try {
const txn = this._db.transaction(storeNames, "readwrite");
@ -63,6 +69,7 @@ export class Storage {
if (this._hasWebkitEarlyCloseTxnBug) {
await reqAsPromise(txn.objectStore(storeNames[0]).get(WEBKITEARLYCLOSETXNBUG_BOGUS_KEY));
}
// @ts-ignore
return new Transaction(txn, storeNames, this._IDBKeyRange);
} catch(err) {
throw new StorageError("readWriteTxn failed", err);