From a0b98cbbeb1a48e6383512474e37f8e3a61ae8e9 Mon Sep 17 00:00:00 2001
From: Midhun Suresh <midhunr@element.io>
Date: Fri, 31 Dec 2021 13:30:33 +0530
Subject: [PATCH] Implement interface and refactor code

---
 src/Hydrogen.ts            | 18 +++++++++---------
 src/types/IMatrixClient.ts |  6 ++++++
 2 files changed, 15 insertions(+), 9 deletions(-)
 create mode 100644 src/types/IMatrixClient.ts

diff --git a/src/Hydrogen.ts b/src/Hydrogen.ts
index d597955..0065db0 100644
--- a/src/Hydrogen.ts
+++ b/src/Hydrogen.ts
@@ -1,13 +1,14 @@
 import { Platform, Client, LoadStatus, createNavigation, createRouter, RoomViewModel, TimelineView, } from "hydrogen-view-sdk";
 import assetPaths from "hydrogen-view-sdk/paths/vite";
 import "hydrogen-view-sdk/style.css";
+import { IMatrixClient } from "./types/IMatrixClient";
 
-export class Hydrogen {
+export class Hydrogen implements IMatrixClient {
     private readonly _homeserver: string;
-    private _platform: Record<string, any>;
-    private _client: Record<string, any>;
-    private _urlRouter: Record<string, any>;
-    private _navigation: Record<string, any>;
+    private _platform: Platform;
+    private _client: Client;
+    private _urlRouter: ReturnType<createRouter>;
+    private _navigation: ReturnType<createNavigation>;
     private _container: HTMLDivElement;
 
     constructor(homeserver: string, container: HTMLDivElement) {
@@ -21,15 +22,14 @@ export class Hydrogen {
         this._client = new Client(this._platform);
     }
 
-    async register(username: string, password: string, initialDeviceDisplayName: string) {
+    async register(username: string, password: string, initialDeviceDisplayName: string): Promise<void> {
         let stage = await this._client.startRegistration(this._homeserver, username, password, initialDeviceDisplayName);
         while (stage !== true) {
             stage = await stage.complete();
         }
-        return stage;
     }
 
-    async login(username: string, password: string) {
+    async login(username: string, password: string): Promise<void> {
         const loginOptions = await this._client.queryLogin(this._homeserver).result;
         this._client.startWithLogin(loginOptions.password(username, password));
 
@@ -46,7 +46,7 @@ export class Hydrogen {
         }
     }
 
-    async showRoom(roomId: string) {
+    async showRoom(roomId: string): Promise<void> {
         const room = this._session.rooms.get(roomId) ?? await this._joinRoom(roomId);
         const vm = new RoomViewModel({
             room,
diff --git a/src/types/IMatrixClient.ts b/src/types/IMatrixClient.ts
new file mode 100644
index 0000000..36923dd
--- /dev/null
+++ b/src/types/IMatrixClient.ts
@@ -0,0 +1,6 @@
+export interface IMatrixClient {
+    register(username: string, password: string, initialDeviceDisplayName: string): Promise<void>;
+    login(username: string, password: string): Promise<void>;
+    showRoom(roomId: string): Promise<void>;
+    attemptStartWithExistingSession(): Promise<boolean>;
+}
-- 
GitLab