support deleting the session from the container

This commit is contained in:
Bruno Windels 2020-04-20 22:26:04 +02:00
parent de7dcf6a40
commit bb7fca0592
2 changed files with 25 additions and 3 deletions

View file

@ -39,6 +39,8 @@ export class SessionContainer {
this._reconnector = null;
this._session = null;
this._sync = null;
this._sessionId = null;
this._storage = null;
}
_createNewSessionId() {
@ -119,17 +121,18 @@ export class SessionContainer {
request: this._request,
reconnector: this._reconnector,
});
const storage = await this._storageFactory.create(sessionInfo.id);
this._sessionId = sessionInfo.id;
this._storage = await this._storageFactory.create(sessionInfo.id);
// no need to pass access token to session
const filteredSessionInfo = {
deviceId: sessionInfo.deviceId,
userId: sessionInfo.userId,
homeServer: sessionInfo.homeServer,
};
this._session = new Session({storage, sessionInfo: filteredSessionInfo, hsApi});
this._session = new Session({storage: this._storage, sessionInfo: filteredSessionInfo, hsApi});
await this._session.load();
this._sync = new Sync({hsApi, storage, session: this._session});
this._sync = new Sync({hsApi, storage: this._storage, session: this._session});
// notify sync and session when back online
this._reconnectSubscription = this._reconnector.connectionStatus.subscribe(state => {
if (state === ConnectionStatus.Online) {
@ -206,6 +209,21 @@ export class SessionContainer {
this._waitForFirstSyncHandle.dispose();
this._waitForFirstSyncHandle = null;
}
if (this._storage) {
this._storage.close();
}
}
async deleteSession() {
if (this._sessionId) {
// if one fails, don't block the other from trying
// also, run in parallel
await Promise.all([
this._storageFactory.delete(this._sessionId),
this._sessionInfoStorage.delete(this._sessionId),
]);
this._sessionId = null;
}
}
}

View file

@ -37,4 +37,8 @@ export class Storage {
throw new StorageError("readWriteTxn failed", err);
}
}
close() {
this._db.close();
}
}