Make more of the codebase conform to strict types (#10857)

This commit is contained in:
Michael Telatynski 2023-05-16 14:25:43 +01:00 committed by GitHub
parent 7f017a84c2
commit 6a3f59cc76
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
45 changed files with 127 additions and 121 deletions

View file

@ -14,6 +14,8 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import { IDeferred, defer } from "matrix-js-sdk/src/utils";
/**
A countdown timer, exposing a promise api.
A timer starts in a non-started state,
@ -28,9 +30,7 @@ a new one through `clone()` or `cloneIfRun()`.
export default class Timer {
private timerHandle?: number;
private startTs?: number;
private promise: Promise<void>;
private resolve: () => void;
private reject: (err: Error) => void;
private deferred!: IDeferred<void>;
public constructor(private timeout: number) {
this.setNotStarted();
@ -39,10 +39,8 @@ export default class Timer {
private setNotStarted(): void {
this.timerHandle = undefined;
this.startTs = undefined;
this.promise = new Promise<void>((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
}).finally(() => {
this.deferred = defer();
this.deferred.promise = this.deferred.promise.finally(() => {
this.timerHandle = undefined;
});
}
@ -51,7 +49,7 @@ export default class Timer {
const now = Date.now();
const elapsed = now - this.startTs!;
if (elapsed >= this.timeout) {
this.resolve();
this.deferred.resolve();
this.setNotStarted();
} else {
const delta = this.timeout - elapsed;
@ -108,7 +106,7 @@ export default class Timer {
public abort(): Timer {
if (this.isRunning()) {
clearTimeout(this.timerHandle);
this.reject(new Error("Timer was aborted."));
this.deferred.reject(new Error("Timer was aborted."));
this.setNotStarted();
}
return this;
@ -120,7 +118,7 @@ export default class Timer {
*@return {Promise}
*/
public finished(): Promise<void> {
return this.promise;
return this.deferred.promise;
}
public isRunning(): boolean {