hydrogen-web/src/matrix/storage/memory/Transaction.js
2019-04-04 09:27:31 +02:00

57 lines
1.8 KiB
JavaScript

import RoomTimelineStore from "./stores/RoomTimelineStore.js";
export default class Transaction {
constructor(storeValues, writable) {
this._storeValues = storeValues;
this._txnStoreValues = {};
this._writable = writable;
}
_store(name, mapper) {
if (!this._txnStoreValues.hasOwnProperty(name)) {
if (!this._storeValues.hasOwnProperty(name)) {
throw new Error(`Transaction wasn't opened for store ${name}`);
}
const store = mapper(this._storeValues[name]);
const clone = store.cloneStoreValue();
// extra prevention for writing
if (!this._writable) {
Object.freeze(clone);
}
this._txnStoreValues[name] = clone;
}
return mapper(this._txnStoreValues[name]);
}
get session() {
throw new Error("not yet implemented");
// return this._store("session", storeValue => new SessionStore(storeValue));
}
get roomSummary() {
throw new Error("not yet implemented");
// return this._store("roomSummary", storeValue => new RoomSummaryStore(storeValue));
}
get roomTimeline() {
return this._store("roomTimeline", storeValue => new RoomTimelineStore(storeValue));
}
get roomState() {
throw new Error("not yet implemented");
// return this._store("roomState", storeValue => new RoomStateStore(storeValue));
}
complete() {
for(let name of Object.keys(this._txnStoreValues)) {
this._storeValues[name] = this._txnStoreValues[name];
}
this._txnStoreValues = null;
return Promise.resolve();
}
abort() {
this._txnStoreValues = null;
return Promise.resolve();
}
}