Convert Reconnector.js to ts

This commit is contained in:
RMidhunSuresh 2021-11-22 15:16:46 +05:30
parent 90e3fde35d
commit f120ce50e6
5 changed files with 35 additions and 20 deletions

View file

@ -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(

View file

@ -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";

View file

@ -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<ConnectionStatus>;
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<ConnectionStatus> {
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<void> {
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<void> {
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));

View file

@ -52,3 +52,8 @@ interface IThumbnailInfo {
mimetype: string;
size: number;
}
export interface IVersionResponse {
versions: string[];
unstable_features?: Record<string, boolean>;
}

View file

@ -60,7 +60,6 @@ class Interval {
}
}
class TimeMeasure {
constructor() {
this._start = window.performance.now();