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/session.js

55 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-02-11 01:55:29 +05:30
import Room from "./room/room.js";
2019-02-27 01:19:45 +05:30
import { ObservableMap } from "../observable/index.js";
2019-02-11 01:55:29 +05:30
export default class Session {
constructor({storage, sessionInfo}) {
this._storage = storage;
2019-02-07 05:50:27 +05:30
this._session = null;
this._sessionInfo = sessionInfo;
2019-02-21 04:18:16 +05:30
this._rooms = new ObservableMap();
2019-02-24 23:55:06 +05:30
this._roomUpdateCallback = (room, params) => this._rooms.update(room.id, params);
2019-02-07 05:50:27 +05:30
}
2018-12-21 19:05:24 +05:30
2019-02-07 05:50:27 +05:30
async load() {
const txn = await this._storage.readTxn([
2019-02-07 06:21:48 +05:30
this._storage.storeNames.session,
this._storage.storeNames.roomSummary,
2019-02-11 01:55:29 +05:30
this._storage.storeNames.roomState,
this._storage.storeNames.roomTimeline,
2019-02-07 06:21:48 +05:30
]);
// restore session object
2019-02-07 05:50:27 +05:30
this._session = await txn.session.get();
if (!this._session) {
this._session = {};
return;
2019-02-07 05:50:27 +05:30
}
// load rooms
2019-02-07 06:21:48 +05:30
const rooms = await txn.roomSummary.getAll();
2019-02-11 01:55:29 +05:30
await Promise.all(rooms.map(summary => {
const room = this.createRoom(summary.roomId);
return room.load(summary, txn);
2019-02-07 06:21:48 +05:30
}));
2018-12-21 19:05:24 +05:30
}
2019-02-21 04:18:16 +05:30
get rooms() {
return this._rooms;
}
createRoom(roomId) {
2019-02-24 23:55:06 +05:30
const room = new Room(roomId, this._storage, this._roomUpdateCallback);
2019-02-21 04:18:16 +05:30
this._rooms.add(roomId, room);
return room;
}
2018-12-21 19:05:24 +05:30
2019-03-09 00:31:28 +05:30
persistSync(syncToken, accountData, txn) {
2019-02-16 07:29:10 +05:30
if (syncToken !== this._session.syncToken) {
this._session.syncToken = syncToken;
txn.session.set(this._session);
}
2019-02-07 05:50:27 +05:30
}
get syncToken() {
return this._session.syncToken;
}
2019-02-21 04:18:16 +05:30
}