2019-02-04 23:29:46 +01:00
|
|
|
export default class Session {
|
2019-02-07 01:20:27 +01:00
|
|
|
// loginData has device_id, user_id, home_server, access_token
|
|
|
|
constructor(storage) {
|
2019-02-04 23:29:46 +01:00
|
|
|
this._storage = storage;
|
2019-02-07 01:20:27 +01:00
|
|
|
this._session = null;
|
|
|
|
this._rooms = null;
|
|
|
|
}
|
|
|
|
// should be called before load
|
|
|
|
async setLoginData(loginData) {
|
|
|
|
const txn = this._storage.readWriteTxn([this._storage.storeNames.session]);
|
|
|
|
const session = {loginData};
|
|
|
|
txn.session.set(session);
|
|
|
|
await txn.complete();
|
2018-12-21 14:35:24 +01:00
|
|
|
}
|
|
|
|
|
2019-02-07 01:20:27 +01:00
|
|
|
async load() {
|
|
|
|
const txn = this._storage.readTxn([this._storage.storeNames.session]);
|
|
|
|
this._session = await txn.session.get();
|
|
|
|
if (!this._session) {
|
|
|
|
throw new Error("session store is empty");
|
|
|
|
}
|
|
|
|
// load rooms
|
2018-12-21 14:35:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getRoom(roomId) {
|
|
|
|
return this._rooms[roomId];
|
|
|
|
}
|
|
|
|
|
2019-02-04 23:29:46 +01:00
|
|
|
createRoom(roomId) {
|
|
|
|
const room = new Room(roomId, this._storage);
|
|
|
|
this._rooms[roomId] = room;
|
|
|
|
return room;
|
|
|
|
}
|
2018-12-21 14:35:24 +01:00
|
|
|
|
2019-02-04 23:29:46 +01:00
|
|
|
applySync(syncToken, accountData, txn) {
|
2019-02-07 01:20:27 +01:00
|
|
|
this._session.syncToken = syncToken;
|
|
|
|
txn.session.setSession(this._session);
|
|
|
|
}
|
|
|
|
|
|
|
|
get syncToken() {
|
|
|
|
return this._session.syncToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
get accessToken() {
|
|
|
|
return this._session.loginData.access_token;
|
2018-12-21 14:35:24 +01:00
|
|
|
}
|
|
|
|
}
|