This repository has been archived on 2022-08-19. You can view files and clone it, but cannot push or open issues or pull requests.
hydrogen-web/src/matrix/storage/idb/transaction.js

71 lines
2.2 KiB
JavaScript
Raw Normal View History

2019-02-07 05:50:27 +05:30
import {txnAsPromise} from "./utils.js";
import {StorageError} from "../common.js";
import {Store} from "./store.js";
import {SessionStore} from "./stores/SessionStore.js";
import {RoomSummaryStore} from "./stores/RoomSummaryStore.js";
import {TimelineEventStore} from "./stores/TimelineEventStore.js";
import {RoomStateStore} from "./stores/RoomStateStore.js";
import {TimelineFragmentStore} from "./stores/TimelineFragmentStore.js";
import {PendingEventStore} from "./stores/PendingEventStore.js";
2019-02-05 04:51:50 +05:30
export class Transaction {
2019-06-27 02:01:36 +05:30
constructor(txn, allowedStoreNames) {
this._txn = txn;
this._allowedStoreNames = allowedStoreNames;
this._stores = {
session: null,
roomSummary: null,
roomTimeline: null,
roomState: null,
};
}
2019-02-05 04:51:50 +05:30
2019-06-27 02:01:36 +05:30
_idbStore(name) {
if (!this._allowedStoreNames.includes(name)) {
// more specific error? this is a bug, so maybe not ...
throw new StorageError(`Invalid store for transaction: ${name}, only ${this._allowedStoreNames.join(", ")} are allowed.`);
}
return new Store(this._txn.objectStore(name));
}
2019-02-05 04:51:50 +05:30
2019-06-27 02:01:36 +05:30
_store(name, mapStore) {
if (!this._stores[name]) {
const idbStore = this._idbStore(name);
this._stores[name] = mapStore(idbStore);
}
return this._stores[name];
}
2019-02-07 05:50:27 +05:30
2019-06-27 02:01:36 +05:30
get session() {
return this._store("session", idbStore => new SessionStore(idbStore));
}
2019-02-05 04:51:50 +05:30
2019-06-27 02:01:36 +05:30
get roomSummary() {
return this._store("roomSummary", idbStore => new RoomSummaryStore(idbStore));
}
2019-02-11 01:55:29 +05:30
get timelineFragments() {
return this._store("timelineFragments", idbStore => new TimelineFragmentStore(idbStore));
}
2019-06-27 02:01:36 +05:30
get timelineEvents() {
return this._store("timelineEvents", idbStore => new TimelineEventStore(idbStore));
}
2019-02-11 01:55:29 +05:30
2019-06-27 02:01:36 +05:30
get roomState() {
return this._store("roomState", idbStore => new RoomStateStore(idbStore));
}
2019-02-11 01:55:29 +05:30
get pendingEvents() {
return this._store("pendingEvents", idbStore => new PendingEventStore(idbStore));
}
2019-06-27 02:01:36 +05:30
complete() {
return txnAsPromise(this._txn);
}
2019-02-05 04:51:50 +05:30
2019-06-27 02:01:36 +05:30
abort() {
this._txn.abort();
}
}