hydrogen-web/src/matrix/session.js

92 lines
2.8 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";
import { SendScheduler, RateLimitingBackoff } from "./SendScheduler.js";
2019-02-11 01:55:29 +05:30
export default class Session {
// sessionInfo contains deviceId, userId and homeServer
2019-05-12 23:56:46 +05:30
constructor({storage, hsApi, sessionInfo}) {
this._storage = storage;
this._hsApi = hsApi;
2019-05-12 23:56:46 +05:30
this._session = null;
this._sessionInfo = sessionInfo;
2019-05-12 23:56:46 +05:30
this._rooms = new ObservableMap();
this._sendScheduler = new SendScheduler({hsApi, backoff: new RateLimitingBackoff()});
2019-02-24 23:55:06 +05:30
this._roomUpdateCallback = (room, params) => this._rooms.update(room.id, params);
2019-05-12 23:56:46 +05:30
}
2018-12-21 19:05:24 +05:30
2019-05-12 23:56:46 +05:30
async load() {
const txn = await this._storage.readTxn([
this._storage.storeNames.session,
this._storage.storeNames.roomSummary,
this._storage.storeNames.roomState,
this._storage.storeNames.timelineEvents,
2019-05-20 00:19:46 +05:30
this._storage.storeNames.timelineFragments,
this._storage.storeNames.pendingEvents,
2019-05-12 23:56:46 +05:30
]);
// restore session object
this._session = await txn.session.get();
if (!this._session) {
this._session = {};
2019-05-12 23:56:46 +05:30
return;
}
const pendingEventsByRoomId = await this._getPendingEventsByRoom(txn);
2019-05-12 23:56:46 +05:30
// load rooms
const rooms = await txn.roomSummary.getAll();
await Promise.all(rooms.map(summary => {
const room = this.createRoom(summary.roomId, pendingEventsByRoomId.get(summary.roomId));
2019-05-12 23:56:46 +05:30
return room.load(summary, txn);
}));
}
2018-12-21 19:05:24 +05:30
2019-07-27 02:10:39 +05:30
notifyNetworkAvailable() {
for (const [, room] of this._rooms) {
2019-07-27 02:10:39 +05:30
room.resumeSending();
}
}
async _getPendingEventsByRoom(txn) {
const pendingEvents = await txn.pendingEvents.getAll();
return pendingEvents.reduce((groups, pe) => {
const group = groups.get(pe.roomId);
if (group) {
group.push(pe);
} else {
groups.set(pe.roomId, [pe]);
}
return groups;
}, new Map());
}
2019-02-21 04:18:16 +05:30
get rooms() {
return this._rooms;
}
createRoom(roomId, pendingEvents) {
2019-05-12 23:56:46 +05:30
const room = new Room({
roomId,
storage: this._storage,
emitCollectionChange: this._roomUpdateCallback,
hsApi: this._hsApi,
sendScheduler: this._sendScheduler,
pendingEvents,
});
2019-05-12 23:56:46 +05:30
this._rooms.add(roomId, room);
return room;
}
2018-12-21 19:05:24 +05:30
2019-05-12 23:56:46 +05:30
persistSync(syncToken, accountData, txn) {
if (syncToken !== this._session.syncToken) {
this._session.syncToken = syncToken;
txn.session.set(this._session);
}
}
2019-02-07 05:50:27 +05:30
2019-05-12 23:56:46 +05:30
get syncToken() {
return this._session.syncToken;
}
2019-06-16 14:23:23 +05:30
get userId() {
return this._sessionInfo.userId;
}
2019-02-21 04:18:16 +05:30
}