Remove unnecessary cast and restrict constructor parameter type

This commit is contained in:
Danila Fedorin 2021-08-26 16:56:03 -07:00
parent 1707df71df
commit b7d232d56d
2 changed files with 23 additions and 5 deletions

View file

@ -36,7 +36,7 @@ class QueryTargetWrapper<T> {
get keyPath(): string | string[] {
if ("objectStore" in this._qt) {
return (this._qt as IDBIndex).objectStore.keyPath;
return this._qt.objectStore.keyPath;
} else {
return this._qt.keyPath;
}

View file

@ -17,13 +17,31 @@ limitations under the License.
import { StorageError } from "../common";
function _sourceName(source: IDBIndex | IDBObjectStore): string {
return "objectStore" in source ?
`${source.objectStore.name}.${source.name}` :
source.name;
}
function _sourceDatabase(source: IDBIndex | IDBObjectStore): string {
return "objectStore" in source ?
source.objectStore.transaction.db.name :
source.transaction.db.name;
}
export class IDBError extends StorageError {
storeName: string;
databaseName: string;
constructor(message: string, source, cause: DOMException | null = null) {
const storeName = source?.name || "<unknown store>";
const databaseName = source?.transaction?.db?.name || "<unknown db>";
constructor(message: string, source: IDBIndex | IDBCursor | IDBObjectStore, cause: DOMException | null = null) {
let storeName: string, databaseName: string;
if (source instanceof IDBCursor) {
storeName = _sourceName(source.source);
databaseName = _sourceDatabase(source.source);
} else {
storeName = _sourceName(source);
databaseName = _sourceDatabase(source);
}
let fullMessage = `${message} on ${databaseName}.${storeName}`;
if (cause) {
fullMessage += ": ";
@ -52,7 +70,7 @@ export class IDBRequestError extends IDBError {
}
export class IDBRequestAttemptError extends IDBError {
constructor(method: string, source, cause: DOMException, params: any[]) {
constructor(method: string, source: IDBIndex | IDBObjectStore, cause: DOMException, params: any[]) {
super(`${method}(${params.map(p => JSON.stringify(p)).join(", ")}) failed`, source, cause);
}
}