From bb18af414b9ef60f7a232ee9f83f9faeeb4ebdac Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Thu, 25 Nov 2021 15:18:03 +0530 Subject: [PATCH] Convert SessionInfoStorage.js to ts --- ...onInfoStorage.js => SessionInfoStorage.ts} | 35 +++++++++++++++---- src/platform/web/Platform.js | 2 +- 2 files changed, 29 insertions(+), 8 deletions(-) rename src/matrix/sessioninfo/localstorage/{SessionInfoStorage.js => SessionInfoStorage.ts} (64%) diff --git a/src/matrix/sessioninfo/localstorage/SessionInfoStorage.js b/src/matrix/sessioninfo/localstorage/SessionInfoStorage.ts similarity index 64% rename from src/matrix/sessioninfo/localstorage/SessionInfoStorage.js rename to src/matrix/sessioninfo/localstorage/SessionInfoStorage.ts index ce795916..fd407c6a 100644 --- a/src/matrix/sessioninfo/localstorage/SessionInfoStorage.js +++ b/src/matrix/sessioninfo/localstorage/SessionInfoStorage.ts @@ -14,12 +14,33 @@ See the License for the specific language governing permissions and limitations under the License. */ -export class SessionInfoStorage { - constructor(name) { +interface ISessionInfo { + id: string; + deviceId: string; + userId: string; + homeserver: string; + homeServer: string; + accessToken: string; + lastUsed: number; +} + +// todo: this should probably be in platform/types? +interface ISessionInfoStorage { + getAll(): Promise; + updateLastUsed(id: string, timestamp: number): Promise; + get(id: string): Promise; + add(sessionInfo: ISessionInfo): Promise; + delete(sessionId: string): Promise; +} + +export class SessionInfoStorage implements ISessionInfoStorage { + private readonly _name: string; + + constructor(name: string) { this._name = name; } - getAll() { + getAll(): Promise { const sessionsJson = localStorage.getItem(this._name); if (sessionsJson) { const sessions = JSON.parse(sessionsJson); @@ -30,7 +51,7 @@ export class SessionInfoStorage { return Promise.resolve([]); } - async updateLastUsed(id, timestamp) { + async updateLastUsed(id: string, timestamp: number): Promise { const sessions = await this.getAll(); if (sessions) { const session = sessions.find(session => session.id === id); @@ -41,20 +62,20 @@ export class SessionInfoStorage { } } - async get(id) { + async get(id: string): Promise { const sessions = await this.getAll(); if (sessions) { return sessions.find(session => session.id === id); } } - async add(sessionInfo) { + async add(sessionInfo: ISessionInfo): Promise { const sessions = await this.getAll(); sessions.push(sessionInfo); localStorage.setItem(this._name, JSON.stringify(sessions)); } - async delete(sessionId) { + async delete(sessionId: string): Promise { let sessions = await this.getAll(); sessions = sessions.filter(s => s.id !== sessionId); localStorage.setItem(this._name, JSON.stringify(sessions)); diff --git a/src/platform/web/Platform.js b/src/platform/web/Platform.js index f6cd2895..67f747ab 100644 --- a/src/platform/web/Platform.js +++ b/src/platform/web/Platform.js @@ -17,7 +17,7 @@ limitations under the License. import {createFetchRequest} from "./dom/request/fetch.js"; import {xhrRequest} from "./dom/request/xhr.js"; import {StorageFactory} from "../../matrix/storage/idb/StorageFactory"; -import {SessionInfoStorage} from "../../matrix/sessioninfo/localstorage/SessionInfoStorage.js"; +import {SessionInfoStorage} from "../../matrix/sessioninfo/localstorage/SessionInfoStorage"; import {SettingsStorage} from "./dom/SettingsStorage.js"; import {Encoding} from "./utils/Encoding.js"; import {OlmWorker} from "../../matrix/e2ee/OlmWorker.js";