2019-02-10 21:25:29 +01:00
|
|
|
import Room from "./room/room.js";
|
2019-02-26 20:49:45 +01:00
|
|
|
import { ObservableMap } from "../observable/index.js";
|
2019-07-26 22:33:33 +02:00
|
|
|
import { SendScheduler, RateLimitingBackoff } from "./SendScheduler.js";
|
2019-07-29 10:23:15 +02:00
|
|
|
import User from "./User.js";
|
2019-02-10 21:25:29 +01:00
|
|
|
|
2019-02-04 23:29:46 +01:00
|
|
|
export default class Session {
|
2019-03-08 20:03:18 +01:00
|
|
|
// sessionInfo contains deviceId, userId and homeServer
|
2019-05-12 20:26:46 +02:00
|
|
|
constructor({storage, hsApi, sessionInfo}) {
|
|
|
|
this._storage = storage;
|
2019-03-08 20:03:18 +01:00
|
|
|
this._hsApi = hsApi;
|
2019-05-12 20:26:46 +02:00
|
|
|
this._session = null;
|
2019-03-08 20:00:37 +01:00
|
|
|
this._sessionInfo = sessionInfo;
|
2019-05-12 20:26:46 +02:00
|
|
|
this._rooms = new ObservableMap();
|
2019-07-26 22:33:33 +02:00
|
|
|
this._sendScheduler = new SendScheduler({hsApi, backoff: new RateLimitingBackoff()});
|
2019-02-24 19:25:06 +01:00
|
|
|
this._roomUpdateCallback = (room, params) => this._rooms.update(room.id, params);
|
2019-07-29 10:23:15 +02:00
|
|
|
this._user = new User(sessionInfo.userId);
|
2019-05-12 20:26:46 +02:00
|
|
|
}
|
2018-12-21 14:35:24 +01:00
|
|
|
|
2019-05-12 20:26:46 +02:00
|
|
|
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-19 20:49:46 +02:00
|
|
|
this._storage.storeNames.timelineFragments,
|
2019-07-26 22:33:33 +02:00
|
|
|
this._storage.storeNames.pendingEvents,
|
2019-05-12 20:26:46 +02:00
|
|
|
]);
|
|
|
|
// restore session object
|
|
|
|
this._session = await txn.session.get();
|
|
|
|
if (!this._session) {
|
2019-03-08 20:00:37 +01:00
|
|
|
this._session = {};
|
2019-05-12 20:26:46 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-07-26 22:33:33 +02:00
|
|
|
const pendingEventsByRoomId = await this._getPendingEventsByRoom(txn);
|
2019-05-12 20:26:46 +02:00
|
|
|
// load rooms
|
|
|
|
const rooms = await txn.roomSummary.getAll();
|
|
|
|
await Promise.all(rooms.map(summary => {
|
2019-07-27 10:40:56 +02:00
|
|
|
const room = this.createRoom(summary.roomId, pendingEventsByRoomId.get(summary.roomId));
|
2019-05-12 20:26:46 +02:00
|
|
|
return room.load(summary, txn);
|
|
|
|
}));
|
|
|
|
}
|
2018-12-21 14:35:24 +01:00
|
|
|
|
2019-07-26 22:40:39 +02:00
|
|
|
notifyNetworkAvailable() {
|
2019-07-27 10:40:56 +02:00
|
|
|
for (const [, room] of this._rooms) {
|
2019-07-26 22:40:39 +02:00
|
|
|
room.resumeSending();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-26 22:33:33 +02:00
|
|
|
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-20 23:48:16 +01:00
|
|
|
get rooms() {
|
|
|
|
return this._rooms;
|
|
|
|
}
|
|
|
|
|
2019-07-26 22:33:33 +02:00
|
|
|
createRoom(roomId, pendingEvents) {
|
2019-05-12 20:26:46 +02:00
|
|
|
const room = new Room({
|
2019-03-08 20:03:18 +01:00
|
|
|
roomId,
|
|
|
|
storage: this._storage,
|
|
|
|
emitCollectionChange: this._roomUpdateCallback,
|
|
|
|
hsApi: this._hsApi,
|
2019-07-26 22:33:33 +02:00
|
|
|
sendScheduler: this._sendScheduler,
|
|
|
|
pendingEvents,
|
2019-07-29 10:23:15 +02:00
|
|
|
user: this._user,
|
2019-03-08 20:03:18 +01:00
|
|
|
});
|
2019-05-12 20:26:46 +02:00
|
|
|
this._rooms.add(roomId, room);
|
|
|
|
return room;
|
|
|
|
}
|
2018-12-21 14:35:24 +01:00
|
|
|
|
2020-03-14 20:45:36 +01:00
|
|
|
writeSync(syncToken, syncFilterId, accountData, txn) {
|
2019-05-12 20:26:46 +02:00
|
|
|
if (syncToken !== this._session.syncToken) {
|
2020-03-14 20:45:36 +01:00
|
|
|
// don't modify this._session because transaction might still fail
|
|
|
|
const newSessionData = Object.assign({}, this._session, {syncToken, syncFilterId});
|
|
|
|
txn.session.set(newSessionData);
|
|
|
|
return newSessionData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
afterSync(newSessionData) {
|
|
|
|
if (newSessionData) {
|
|
|
|
// sync transaction succeeded, modify object state now
|
|
|
|
this._session = newSessionData;
|
2019-05-12 20:26:46 +02:00
|
|
|
}
|
|
|
|
}
|
2019-02-07 01:20:27 +01:00
|
|
|
|
2019-05-12 20:26:46 +02:00
|
|
|
get syncToken() {
|
|
|
|
return this._session.syncToken;
|
|
|
|
}
|
2019-06-16 10:53:23 +02:00
|
|
|
|
2019-10-12 20:24:09 +02:00
|
|
|
get syncFilterId() {
|
|
|
|
return this._session.syncFilterId;
|
|
|
|
}
|
|
|
|
|
2019-07-29 10:23:15 +02:00
|
|
|
get user() {
|
|
|
|
return this._user;
|
2019-06-16 10:53:23 +02:00
|
|
|
}
|
2019-02-20 23:48:16 +01:00
|
|
|
}
|
2020-03-14 20:45:36 +01:00
|
|
|
|
|
|
|
export function tests() {
|
2020-03-14 21:38:37 +01:00
|
|
|
function createStorageMock(session, pendingEvents = []) {
|
2020-03-14 20:45:36 +01:00
|
|
|
return {
|
|
|
|
readTxn() {
|
|
|
|
return Promise.resolve({
|
|
|
|
session: {
|
|
|
|
get() {
|
|
|
|
return Promise.resolve(Object.assign({}, session));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
pendingEvents: {
|
|
|
|
getAll() {
|
|
|
|
return Promise.resolve(pendingEvents);
|
|
|
|
}
|
2020-03-14 21:38:37 +01:00
|
|
|
},
|
|
|
|
roomSummary: {
|
|
|
|
getAll() {
|
|
|
|
return Promise.resolve([]);
|
|
|
|
}
|
2020-03-14 20:45:36 +01:00
|
|
|
}
|
|
|
|
});
|
2020-03-14 21:38:37 +01:00
|
|
|
},
|
|
|
|
storeNames: {}
|
2020-03-14 20:45:36 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
"session data is not modified until after sync": async (assert) => {
|
|
|
|
const session = new Session({storage: createStorageMock({
|
|
|
|
syncToken: "a",
|
|
|
|
syncFilterId: 5,
|
2020-03-14 21:38:37 +01:00
|
|
|
}), sessionInfo: {userId: ""}});
|
2020-03-14 20:45:36 +01:00
|
|
|
await session.load();
|
|
|
|
let txnSetCalled = false;
|
|
|
|
const syncTxn = {
|
|
|
|
session: {
|
|
|
|
set({syncToken, syncFilterId}) {
|
|
|
|
txnSetCalled = true;
|
2020-03-14 21:38:37 +01:00
|
|
|
assert.equal(syncToken, "b");
|
|
|
|
assert.equal(syncFilterId, 6);
|
2020-03-14 20:45:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const newSessionData = session.writeSync("b", 6, {}, syncTxn);
|
|
|
|
assert(txnSetCalled);
|
2020-03-14 21:38:37 +01:00
|
|
|
assert.equal(session.syncToken, "a");
|
|
|
|
assert.equal(session.syncFilterId, 5);
|
2020-03-14 20:45:36 +01:00
|
|
|
session.afterSync(newSessionData);
|
2020-03-14 21:38:37 +01:00
|
|
|
assert.equal(session.syncToken, "b");
|
|
|
|
assert.equal(session.syncFilterId, 6);
|
2020-03-14 20:45:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|