Convert ExponentialRetryDelay.js to ts

This commit is contained in:
RMidhunSuresh 2021-11-21 21:12:28 +05:30
parent 3a24019d96
commit e1a823400a
7 changed files with 24 additions and 12 deletions

View file

@ -21,7 +21,7 @@ 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.js";
import {ExponentialRetryDelay} from "./net/ExponentialRetryDelay.js"; import {ExponentialRetryDelay} from "./net/ExponentialRetryDelay";
import {MediaRepository} from "./net/MediaRepository.js"; import {MediaRepository} from "./net/MediaRepository.js";
import {RequestScheduler} from "./net/RequestScheduler.js"; import {RequestScheduler} from "./net/RequestScheduler.js";
import {Sync, SyncStatus} from "./Sync.js"; import {Sync, SyncStatus} from "./Sync.js";

View file

@ -15,18 +15,28 @@ limitations under the License.
*/ */
import {AbortError} from "../../utils/error"; import {AbortError} from "../../utils/error";
import type {Timeout} from "../../platform/web/dom/Clock.js";
type TimeoutCreator = (ms: number) => Timeout;
const enum Default { start = 2000 }
export class ExponentialRetryDelay { export class ExponentialRetryDelay {
constructor(createTimeout) { private readonly _start: number = Default.start;
private _current: number = Default.start;
private readonly _createTimeout: TimeoutCreator;
private readonly _max: number;
private _timeout?: Timeout;
constructor(createTimeout: TimeoutCreator) {
const start = 2000; const start = 2000;
this._start = start; this._start = start;
this._current = start; this._current = start;
this._createTimeout = createTimeout; this._createTimeout = createTimeout;
this._max = 60 * 5 * 1000; //5 min this._max = 60 * 5 * 1000; //5 min
this._timeout = null;
} }
async waitForRetry() { async waitForRetry(): Promise<void> {
this._timeout = this._createTimeout(this._current); this._timeout = this._createTimeout(this._current);
try { try {
await this._timeout.elapsed(); await this._timeout.elapsed();
@ -39,22 +49,22 @@ export class ExponentialRetryDelay {
throw err; throw err;
} }
} finally { } finally {
this._timeout = null; this._timeout = undefined;
} }
} }
abort() { abort(): void {
if (this._timeout) { if (this._timeout) {
this._timeout.abort(); this._timeout.abort();
} }
} }
reset() { reset(): void {
this._current = this._start; this._current = this._start;
this.abort(); this.abort();
} }
get nextValue() { get nextValue(): number {
return this._current; return this._current;
} }
} }

View file

@ -15,7 +15,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import {encodeQueryParams, encodeBody} from "./common.js"; import {encodeQueryParams, encodeBody} from "./common";
import {HomeServerRequest} from "./HomeServerRequest.js"; import {HomeServerRequest} from "./HomeServerRequest.js";
const CS_R0_PREFIX = "/_matrix/client/r0"; const CS_R0_PREFIX = "/_matrix/client/r0";

View file

@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import {encodeQueryParams} from "./common.js"; import {encodeQueryParams} from "./common";
import {decryptAttachment} from "../e2ee/attachment.js"; import {decryptAttachment} from "../e2ee/attachment.js";
export class MediaRepository { export class MediaRepository {

View file

@ -120,7 +120,7 @@ export class Reconnector {
import {Clock as MockClock} from "../../mocks/Clock.js"; import {Clock as MockClock} from "../../mocks/Clock.js";
import {ExponentialRetryDelay} from "./ExponentialRetryDelay.js"; import {ExponentialRetryDelay} from "./ExponentialRetryDelay";
import {ConnectionError} from "../error.js" import {ConnectionError} from "../error.js"
export function tests() { export function tests() {

View file

@ -18,7 +18,7 @@ limitations under the License.
import {AbortError} from "../../utils/error"; import {AbortError} from "../../utils/error";
import {HomeServerError} from "../error.js"; import {HomeServerError} from "../error.js";
import {HomeServerApi} from "./HomeServerApi.js"; import {HomeServerApi} from "./HomeServerApi.js";
import {ExponentialRetryDelay} from "./ExponentialRetryDelay.js"; import {ExponentialRetryDelay} from "./ExponentialRetryDelay";
class Request { class Request {
constructor(methodName, args) { constructor(methodName, args) {

View file

@ -14,6 +14,8 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
// todo: this file should be called something else?
import type {RequestResult} from "../web/dom/request/fetch.js"; import type {RequestResult} from "../web/dom/request/fetch.js";
import type {IEncodedBody} from "../../matrix/net/common"; import type {IEncodedBody} from "../../matrix/net/common";