2020-08-05 18:38:55 +02:00
|
|
|
/*
|
|
|
|
Copyright 2020 Bruno Windels <bruno@windels.cloud>
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2019-02-07 01:20:27 +01:00
|
|
|
import {txnAsPromise} from "./utils.js";
|
2019-06-26 22:00:50 +02:00
|
|
|
import {StorageError} from "../common.js";
|
2020-04-20 21:41:10 +02:00
|
|
|
import {Store} from "./Store.js";
|
2020-04-20 21:26:39 +02:00
|
|
|
import {SessionStore} from "./stores/SessionStore.js";
|
|
|
|
import {RoomSummaryStore} from "./stores/RoomSummaryStore.js";
|
|
|
|
import {TimelineEventStore} from "./stores/TimelineEventStore.js";
|
|
|
|
import {RoomStateStore} from "./stores/RoomStateStore.js";
|
2020-06-26 23:26:24 +02:00
|
|
|
import {RoomMemberStore} from "./stores/RoomMemberStore.js";
|
2020-04-20 21:26:39 +02:00
|
|
|
import {TimelineFragmentStore} from "./stores/TimelineFragmentStore.js";
|
|
|
|
import {PendingEventStore} from "./stores/PendingEventStore.js";
|
2019-02-05 00:21:50 +01:00
|
|
|
|
2020-04-20 21:26:39 +02:00
|
|
|
export class Transaction {
|
2019-06-26 22:31:36 +02:00
|
|
|
constructor(txn, allowedStoreNames) {
|
|
|
|
this._txn = txn;
|
|
|
|
this._allowedStoreNames = allowedStoreNames;
|
|
|
|
this._stores = {
|
|
|
|
session: null,
|
|
|
|
roomSummary: null,
|
|
|
|
roomTimeline: null,
|
|
|
|
roomState: null,
|
|
|
|
};
|
|
|
|
}
|
2019-02-05 00:21:50 +01:00
|
|
|
|
2019-06-26 22:31:36 +02:00
|
|
|
_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 00:21:50 +01:00
|
|
|
|
2019-06-26 22:31:36 +02:00
|
|
|
_store(name, mapStore) {
|
|
|
|
if (!this._stores[name]) {
|
|
|
|
const idbStore = this._idbStore(name);
|
|
|
|
this._stores[name] = mapStore(idbStore);
|
|
|
|
}
|
|
|
|
return this._stores[name];
|
|
|
|
}
|
2019-02-07 01:20:27 +01:00
|
|
|
|
2019-06-26 22:31:36 +02:00
|
|
|
get session() {
|
|
|
|
return this._store("session", idbStore => new SessionStore(idbStore));
|
|
|
|
}
|
2019-02-05 00:21:50 +01:00
|
|
|
|
2019-06-26 22:31:36 +02:00
|
|
|
get roomSummary() {
|
|
|
|
return this._store("roomSummary", idbStore => new RoomSummaryStore(idbStore));
|
|
|
|
}
|
2019-02-10 21:25:29 +01:00
|
|
|
|
2019-05-12 20:24:06 +02:00
|
|
|
get timelineFragments() {
|
|
|
|
return this._store("timelineFragments", idbStore => new TimelineFragmentStore(idbStore));
|
|
|
|
}
|
|
|
|
|
2019-06-26 22:31:36 +02:00
|
|
|
get timelineEvents() {
|
|
|
|
return this._store("timelineEvents", idbStore => new TimelineEventStore(idbStore));
|
|
|
|
}
|
2019-02-10 21:25:29 +01:00
|
|
|
|
2019-06-26 22:31:36 +02:00
|
|
|
get roomState() {
|
|
|
|
return this._store("roomState", idbStore => new RoomStateStore(idbStore));
|
|
|
|
}
|
2019-02-10 21:25:29 +01:00
|
|
|
|
2020-06-26 23:26:24 +02:00
|
|
|
get roomMembers() {
|
|
|
|
return this._store("roomMembers", idbStore => new RoomMemberStore(idbStore));
|
|
|
|
}
|
|
|
|
|
2019-07-27 10:40:56 +02:00
|
|
|
get pendingEvents() {
|
|
|
|
return this._store("pendingEvents", idbStore => new PendingEventStore(idbStore));
|
|
|
|
}
|
|
|
|
|
2019-06-26 22:31:36 +02:00
|
|
|
complete() {
|
|
|
|
return txnAsPromise(this._txn);
|
|
|
|
}
|
2019-02-05 00:21:50 +01:00
|
|
|
|
2019-06-26 22:31:36 +02:00
|
|
|
abort() {
|
|
|
|
this._txn.abort();
|
|
|
|
}
|
2019-05-12 20:24:06 +02:00
|
|
|
}
|