hydrogen-web/src/matrix/storage/idb/storage.js

33 lines
945 B
JavaScript
Raw Normal View History

2019-02-07 05:50:27 +05:30
import Transaction from "./transaction.js";
2019-02-07 04:49:14 +05:30
export const STORE_NAMES = ["session", "roomState", "roomSummary", "roomTimeline"];
2019-02-05 04:51:50 +05:30
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) {
2019-02-11 01:55:29 +05:30
const idx = storeNames.findIndex(name => !STORE_NAMES.includes(name));
if (idx !== -1) {
throw new Error(`Tried to open a transaction for unknown store ${storeNames[idx]}`);
2019-02-05 04:51:50 +05:30
}
}
2019-02-07 05:50:27 +05:30
readTxn(storeNames) {
2019-02-05 04:51:50 +05:30
this._validateStoreNames(storeNames);
const txn = this._db.transaction(storeNames, "readonly");
return new Transaction(txn, storeNames);
}
2019-02-07 05:50:27 +05:30
readWriteTxn(storeNames) {
2019-02-05 04:51:50 +05:30
this._validateStoreNames(storeNames);
const txn = this._db.transaction(storeNames, "readwrite");
return new Transaction(txn, storeNames);
}
}