Merge pull request #596 from vector-im/ts-conversion-matrix-sessioninfo

Convert matrix/sessioninfo to typescript
This commit is contained in:
Bruno Windels 2021-12-02 09:29:11 +01:00 committed by GitHub
commit d91aaabeb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 8 deletions

View File

@ -14,12 +14,33 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
export class SessionInfoStorage { interface ISessionInfo {
constructor(name) { id: string;
deviceId: string;
userId: string;
homeserver: string;
homeServer: string; // deprecate this over time
accessToken: string;
lastUsed: number;
}
// todo: this should probably be in platform/types?
interface ISessionInfoStorage {
getAll(): Promise<ISessionInfo[]>;
updateLastUsed(id: string, timestamp: number): Promise<void>;
get(id: string): Promise<ISessionInfo | undefined>;
add(sessionInfo: ISessionInfo): Promise<void>;
delete(sessionId: string): Promise<void>;
}
export class SessionInfoStorage implements ISessionInfoStorage {
private readonly _name: string;
constructor(name: string) {
this._name = name; this._name = name;
} }
getAll() { getAll(): Promise<ISessionInfo[]> {
const sessionsJson = localStorage.getItem(this._name); const sessionsJson = localStorage.getItem(this._name);
if (sessionsJson) { if (sessionsJson) {
const sessions = JSON.parse(sessionsJson); const sessions = JSON.parse(sessionsJson);
@ -30,7 +51,7 @@ export class SessionInfoStorage {
return Promise.resolve([]); return Promise.resolve([]);
} }
async updateLastUsed(id, timestamp) { async updateLastUsed(id: string, timestamp: number): Promise<void> {
const sessions = await this.getAll(); const sessions = await this.getAll();
if (sessions) { if (sessions) {
const session = sessions.find(session => session.id === id); const session = sessions.find(session => session.id === id);
@ -41,20 +62,20 @@ export class SessionInfoStorage {
} }
} }
async get(id) { async get(id: string): Promise<ISessionInfo | undefined> {
const sessions = await this.getAll(); const sessions = await this.getAll();
if (sessions) { if (sessions) {
return sessions.find(session => session.id === id); return sessions.find(session => session.id === id);
} }
} }
async add(sessionInfo) { async add(sessionInfo: ISessionInfo): Promise<void> {
const sessions = await this.getAll(); const sessions = await this.getAll();
sessions.push(sessionInfo); sessions.push(sessionInfo);
localStorage.setItem(this._name, JSON.stringify(sessions)); localStorage.setItem(this._name, JSON.stringify(sessions));
} }
async delete(sessionId) { async delete(sessionId: string): Promise<void> {
let sessions = await this.getAll(); let sessions = await this.getAll();
sessions = sessions.filter(s => s.id !== sessionId); sessions = sessions.filter(s => s.id !== sessionId);
localStorage.setItem(this._name, JSON.stringify(sessions)); localStorage.setItem(this._name, JSON.stringify(sessions));

View File

@ -17,7 +17,7 @@ limitations under the License.
import {createFetchRequest} from "./dom/request/fetch.js"; import {createFetchRequest} from "./dom/request/fetch.js";
import {xhrRequest} from "./dom/request/xhr.js"; import {xhrRequest} from "./dom/request/xhr.js";
import {StorageFactory} from "../../matrix/storage/idb/StorageFactory"; 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 {SettingsStorage} from "./dom/SettingsStorage.js";
import {Encoding} from "./utils/Encoding.js"; import {Encoding} from "./utils/Encoding.js";
import {OlmWorker} from "../../matrix/e2ee/OlmWorker.js"; import {OlmWorker} from "../../matrix/e2ee/OlmWorker.js";