forked from mystiq/hydrogen-web
Migrate Storage to TypeScript
This commit is contained in:
parent
5f3c9cda97
commit
eae820f91b
2 changed files with 13 additions and 6 deletions
|
@ -21,21 +21,26 @@ import { reqAsPromise } from "./utils";
|
|||
const WEBKITEARLYCLOSETXNBUG_BOGUS_KEY = "782rh281re38-boguskey";
|
||||
|
||||
export class Storage {
|
||||
constructor(idbDatabase, IDBKeyRange, hasWebkitEarlyCloseTxnBug) {
|
||||
private _db: IDBDatabase;
|
||||
private _hasWebkitEarlyCloseTxnBug: boolean;
|
||||
storeNames: typeof StoreNames;
|
||||
|
||||
constructor(idbDatabase: IDBDatabase, IDBKeyRange, hasWebkitEarlyCloseTxnBug: boolean) {
|
||||
this._db = idbDatabase;
|
||||
// @ts-ignore
|
||||
this._IDBKeyRange = IDBKeyRange;
|
||||
this._hasWebkitEarlyCloseTxnBug = hasWebkitEarlyCloseTxnBug;
|
||||
this.storeNames = StoreNames;
|
||||
}
|
||||
|
||||
_validateStoreNames(storeNames) {
|
||||
_validateStoreNames(storeNames: StoreNames[]): void {
|
||||
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: StoreNames[]): Promise<Transaction> {
|
||||
this._validateStoreNames(storeNames);
|
||||
try {
|
||||
const txn = this._db.transaction(storeNames, "readonly");
|
||||
|
@ -44,13 +49,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: StoreNames[]): Promise<Transaction> {
|
||||
this._validateStoreNames(storeNames);
|
||||
try {
|
||||
const txn = this._db.transaction(storeNames, "readwrite");
|
||||
|
@ -59,13 +65,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("readWriteTxn failed", err);
|
||||
}
|
||||
}
|
||||
|
||||
close() {
|
||||
close(): void {
|
||||
this._db.close();
|
||||
}
|
||||
}
|
|
@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
|||
limitations under the License.
|
||||
*/
|
||||
|
||||
import {Storage} from "./Storage.js";
|
||||
import {Storage} from "./Storage";
|
||||
import { openDatabase, reqAsPromise } from "./utils";
|
||||
import { exportSession, importSession } from "./export.js";
|
||||
import { schema } from "./schema.js";
|
||||
|
|
Loading…
Reference in a new issue