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 {ViewModel} from "../ViewModel.js";
import {createEnum} from "../../utils/enum"; import {createEnum} from "../../utils/enum";
import {ConnectionStatus} from "../../matrix/net/Reconnector.js"; import {ConnectionStatus} from "../../matrix/net/Reconnector";
import {SyncStatus} from "../../matrix/Sync.js"; import {SyncStatus} from "../../matrix/Sync.js";
const SessionStatus = createEnum( const SessionStatus = createEnum(

View file

@ -20,7 +20,7 @@ import {lookupHomeserver} from "./well-known.js";
import {AbortableOperation} from "../utils/AbortableOperation"; import {AbortableOperation} from "../utils/AbortableOperation";
import {ObservableValue} from "../observable/ObservableValue"; import {ObservableValue} from "../observable/ObservableValue";
import {HomeServerApi} from "./net/HomeServerApi.js"; 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 {ExponentialRetryDelay} from "./net/ExponentialRetryDelay";
import {MediaRepository} from "./net/MediaRepository"; import {MediaRepository} from "./net/MediaRepository";
import {RequestScheduler} from "./net/RequestScheduler.js"; 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. limitations under the License.
*/ */
import {createEnum} from "../../utils/enum";
import {ObservableValue} from "../../observable/ObservableValue"; 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", "Waiting",
"Reconnecting", "Reconnecting",
"Online" "Online"
); };
export class Reconnector { 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._onlineStatus = onlineStatus;
this._retryDelay = retryDelay; this._retryDelay = retryDelay;
this._createTimeMeasure = createMeasure; this._createTimeMeasure = createMeasure;
// assume online, and do our thing when something fails // assume online, and do our thing when something fails
this._state = new ObservableValue(ConnectionStatus.Online); this._state = new ObservableValue(ConnectionStatus.Online);
this._isReconnecting = false; this._isReconnecting = false;
this._versionsResponse = null;
} }
get lastVersionsResponse() { get lastVersionsResponse(): IVersionResponse | undefined {
return this._versionsResponse; return this._versionsResponse;
} }
get connectionStatus() { get connectionStatus(): ObservableValue<ConnectionStatus> {
return this._state; return this._state;
} }
get retryIn() { get retryIn(): number {
if (this._state.get() === ConnectionStatus.Waiting) { if (this._state.get() === ConnectionStatus.Waiting) {
return this._retryDelay.nextValue - this._stateSince.measure(); return this._retryDelay.nextValue - this._stateSince.measure();
} }
return 0; return 0;
} }
async onRequestFailed(hsApi) { async onRequestFailed(hsApi: HomeServerApi): Promise<void> {
if (!this._isReconnecting) { if (!this._isReconnecting) {
this._isReconnecting = true; this._isReconnecting = true;
@ -75,14 +86,14 @@ export class Reconnector {
} }
} }
tryNow() { tryNow(): void {
if (this._retryDelay) { if (this._retryDelay) {
// this will interrupt this._retryDelay.waitForRetry() in _reconnectLoop // this will interrupt this._retryDelay.waitForRetry() in _reconnectLoop
this._retryDelay.abort(); this._retryDelay.abort();
} }
} }
_setState(state) { _setState(state: ConnectionStatus): void {
if (state !== this._state.get()) { if (state !== this._state.get()) {
if (state === ConnectionStatus.Waiting) { if (state === ConnectionStatus.Waiting) {
this._stateSince = this._createTimeMeasure(); this._stateSince = this._createTimeMeasure();
@ -93,8 +104,8 @@ export class Reconnector {
} }
} }
async _reconnectLoop(hsApi) { async _reconnectLoop(hsApi: HomeServerApi): Promise<void> {
this._versionsResponse = null; this._versionsResponse = undefined;
this._retryDelay.reset(); this._retryDelay.reset();
while (!this._versionsResponse) { while (!this._versionsResponse) {
@ -120,7 +131,7 @@ export class Reconnector {
import {Clock as MockClock} from "../../mocks/Clock.js"; import {Clock as MockClock} from "../../mocks/Clock.js";
import {ExponentialRetryDelay} from "./ExponentialRetryDelay"; import {ExponentialRetryDelay as _ExponentialRetryDelay} from "./ExponentialRetryDelay";
import {ConnectionError} from "../error.js" import {ConnectionError} from "../error.js"
export function tests() { export function tests() {
@ -146,10 +157,10 @@ export function tests() {
const clock = new MockClock(); const clock = new MockClock();
const {createMeasure} = clock; const {createMeasure} = clock;
const onlineStatus = new ObservableValue(false); 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 reconnector = new Reconnector({retryDelay, onlineStatus, createMeasure});
const {connectionStatus} = reconnector; const {connectionStatus} = reconnector;
const statuses = []; const statuses: ConnectionStatus[] = [];
const subscription = reconnector.connectionStatus.subscribe(s => { const subscription = reconnector.connectionStatus.subscribe(s => {
statuses.push(s); statuses.push(s);
}); });
@ -170,7 +181,7 @@ export function tests() {
const clock = new MockClock(); const clock = new MockClock();
const {createMeasure} = clock; const {createMeasure} = clock;
const onlineStatus = new ObservableValue(false); 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 reconnector = new Reconnector({retryDelay, onlineStatus, createMeasure});
const {connectionStatus} = reconnector; const {connectionStatus} = reconnector;
reconnector.onRequestFailed(createHsApiMock(1)); reconnector.onRequestFailed(createHsApiMock(1));

View file

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

View file

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