2019-02-07 01:20:27 +01:00
|
|
|
import Transaction from "./transaction.js";
|
|
|
|
|
2019-02-07 00:19:14 +01:00
|
|
|
export const STORE_NAMES = ["session", "roomState", "roomSummary", "roomTimeline"];
|
2019-02-05 00:21:50 +01:00
|
|
|
|
|
|
|
export default class Storage {
|
|
|
|
constructor(idbDatabase) {
|
|
|
|
this._db = idbDatabase;
|
|
|
|
const nameMap = STORE_NAMES.reduce((nameMap, name) => {
|
|
|
|
nameMap[name] = name;
|
|
|
|
return nameMap;
|
|
|
|
}, {});
|
|
|
|
this.storeNames = Object.freeze(nameMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
_validateStoreNames(storeNames) {
|
|
|
|
const unknownStoreName = storeNames.find(name => !STORE_NAMES.includes(name));
|
|
|
|
if (unknownStoreName) {
|
|
|
|
throw new Error(`Tried to open a transaction for unknown store ${unknownStoreName}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 01:20:27 +01:00
|
|
|
readTxn(storeNames) {
|
2019-02-05 00:21:50 +01:00
|
|
|
this._validateStoreNames(storeNames);
|
|
|
|
const txn = this._db.transaction(storeNames, "readonly");
|
|
|
|
return new Transaction(txn, storeNames);
|
|
|
|
}
|
|
|
|
|
2019-02-07 01:20:27 +01:00
|
|
|
readWriteTxn(storeNames) {
|
2019-02-05 00:21:50 +01:00
|
|
|
this._validateStoreNames(storeNames);
|
|
|
|
const txn = this._db.transaction(storeNames, "readwrite");
|
|
|
|
return new Transaction(txn, storeNames);
|
|
|
|
}
|
|
|
|
}
|