From f120ce50e6698736cbefc3238f6643faee403e00 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Mon, 22 Nov 2021 15:16:46 +0530 Subject: [PATCH] Convert Reconnector.js to ts --- src/domain/session/SessionStatusViewModel.js | 2 +- src/matrix/SessionContainer.js | 2 +- .../net/{Reconnector.js => Reconnector.ts} | 45 ++++++++++++------- src/matrix/net/types/response.ts | 5 +++ src/platform/web/dom/Clock.js | 1 - 5 files changed, 35 insertions(+), 20 deletions(-) rename src/matrix/net/{Reconnector.js => Reconnector.ts} (79%) diff --git a/src/domain/session/SessionStatusViewModel.js b/src/domain/session/SessionStatusViewModel.js index 27dad7cc..fcedb371 100644 --- a/src/domain/session/SessionStatusViewModel.js +++ b/src/domain/session/SessionStatusViewModel.js @@ -16,7 +16,7 @@ limitations under the License. import {ViewModel} from "../ViewModel.js"; import {createEnum} from "../../utils/enum"; -import {ConnectionStatus} from "../../matrix/net/Reconnector.js"; +import {ConnectionStatus} from "../../matrix/net/Reconnector"; import {SyncStatus} from "../../matrix/Sync.js"; const SessionStatus = createEnum( diff --git a/src/matrix/SessionContainer.js b/src/matrix/SessionContainer.js index 7b0dfa1e..9dad9333 100644 --- a/src/matrix/SessionContainer.js +++ b/src/matrix/SessionContainer.js @@ -20,7 +20,7 @@ import {lookupHomeserver} from "./well-known.js"; import {AbortableOperation} from "../utils/AbortableOperation"; import {ObservableValue} from "../observable/ObservableValue"; import {HomeServerApi} from "./net/HomeServerApi.js"; -import {Reconnector, ConnectionStatus} from "./net/Reconnector.js"; +import {Reconnector, ConnectionStatus} from "./net/Reconnector"; import {ExponentialRetryDelay} from "./net/ExponentialRetryDelay"; import {MediaRepository} from "./net/MediaRepository"; import {RequestScheduler} from "./net/RequestScheduler.js"; diff --git a/src/matrix/net/Reconnector.js b/src/matrix/net/Reconnector.ts similarity index 79% rename from src/matrix/net/Reconnector.js rename to src/matrix/net/Reconnector.ts index c4231c79..7f9c9660 100644 --- a/src/matrix/net/Reconnector.js +++ b/src/matrix/net/Reconnector.ts @@ -14,42 +14,53 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {createEnum} from "../../utils/enum"; import {ObservableValue} from "../../observable/ObservableValue"; +import type {ExponentialRetryDelay} from "./ExponentialRetryDelay"; +import type {TimeMeasure} from "../../platform/web/dom/Clock.js"; +import type {OnlineStatus} from "../../platform/web/dom/OnlineStatus.js"; +import type {IVersionResponse} from "./types/response"; +import type {HomeServerApi} from "./HomeServerApi.js"; -export const ConnectionStatus = createEnum( +export enum ConnectionStatus { "Waiting", "Reconnecting", "Online" -); +}; export class Reconnector { - constructor({retryDelay, createMeasure, onlineStatus}) { + private readonly _retryDelay: ExponentialRetryDelay; + private readonly _createTimeMeasure: () => TimeMeasure; + private readonly _onlineStatus: OnlineStatus; + private readonly _state: ObservableValue; + private _isReconnecting: boolean; + private _versionsResponse?: IVersionResponse = undefined; + private _stateSince: TimeMeasure; + + constructor({retryDelay, createMeasure, onlineStatus}: {retryDelay: ExponentialRetryDelay, createMeasure: () => TimeMeasure, onlineStatus: OnlineStatus}) { this._onlineStatus = onlineStatus; this._retryDelay = retryDelay; this._createTimeMeasure = createMeasure; // assume online, and do our thing when something fails this._state = new ObservableValue(ConnectionStatus.Online); this._isReconnecting = false; - this._versionsResponse = null; } - get lastVersionsResponse() { + get lastVersionsResponse(): IVersionResponse | undefined { return this._versionsResponse; } - get connectionStatus() { + get connectionStatus(): ObservableValue { return this._state; } - get retryIn() { + get retryIn(): number { if (this._state.get() === ConnectionStatus.Waiting) { return this._retryDelay.nextValue - this._stateSince.measure(); } return 0; } - async onRequestFailed(hsApi) { + async onRequestFailed(hsApi: HomeServerApi): Promise { if (!this._isReconnecting) { this._isReconnecting = true; @@ -75,14 +86,14 @@ export class Reconnector { } } - tryNow() { + tryNow(): void { if (this._retryDelay) { // this will interrupt this._retryDelay.waitForRetry() in _reconnectLoop this._retryDelay.abort(); } } - _setState(state) { + _setState(state: ConnectionStatus): void { if (state !== this._state.get()) { if (state === ConnectionStatus.Waiting) { this._stateSince = this._createTimeMeasure(); @@ -93,8 +104,8 @@ export class Reconnector { } } - async _reconnectLoop(hsApi) { - this._versionsResponse = null; + async _reconnectLoop(hsApi: HomeServerApi): Promise { + this._versionsResponse = undefined; this._retryDelay.reset(); while (!this._versionsResponse) { @@ -120,7 +131,7 @@ export class Reconnector { import {Clock as MockClock} from "../../mocks/Clock.js"; -import {ExponentialRetryDelay} from "./ExponentialRetryDelay"; +import {ExponentialRetryDelay as _ExponentialRetryDelay} from "./ExponentialRetryDelay"; import {ConnectionError} from "../error.js" export function tests() { @@ -146,10 +157,10 @@ export function tests() { const clock = new MockClock(); const {createMeasure} = clock; const onlineStatus = new ObservableValue(false); - const retryDelay = new ExponentialRetryDelay(clock.createTimeout); + const retryDelay = new _ExponentialRetryDelay(clock.createTimeout); const reconnector = new Reconnector({retryDelay, onlineStatus, createMeasure}); const {connectionStatus} = reconnector; - const statuses = []; + const statuses: ConnectionStatus[] = []; const subscription = reconnector.connectionStatus.subscribe(s => { statuses.push(s); }); @@ -170,7 +181,7 @@ export function tests() { const clock = new MockClock(); const {createMeasure} = clock; const onlineStatus = new ObservableValue(false); - const retryDelay = new ExponentialRetryDelay(clock.createTimeout); + const retryDelay = new _ExponentialRetryDelay(clock.createTimeout); const reconnector = new Reconnector({retryDelay, onlineStatus, createMeasure}); const {connectionStatus} = reconnector; reconnector.onRequestFailed(createHsApiMock(1)); diff --git a/src/matrix/net/types/response.ts b/src/matrix/net/types/response.ts index 762bd683..4c08adc0 100644 --- a/src/matrix/net/types/response.ts +++ b/src/matrix/net/types/response.ts @@ -52,3 +52,8 @@ interface IThumbnailInfo { mimetype: string; size: number; } + +export interface IVersionResponse { + versions: string[]; + unstable_features?: Record; +} diff --git a/src/platform/web/dom/Clock.js b/src/platform/web/dom/Clock.js index 855e925c..6691bc73 100644 --- a/src/platform/web/dom/Clock.js +++ b/src/platform/web/dom/Clock.js @@ -60,7 +60,6 @@ class Interval { } } - class TimeMeasure { constructor() { this._start = window.performance.now();