2018-12-11 14:43:41 +00:00
|
|
|
/*
|
2021-04-06 11:26:50 +00:00
|
|
|
Copyright 2018, 2021 The Matrix.org Foundation C.I.C.
|
2018-12-11 14:43:41 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
A countdown timer, exposing a promise api.
|
|
|
|
A timer starts in a non-started state,
|
|
|
|
and needs to be started by calling `start()`` on it first.
|
|
|
|
|
|
|
|
Timers can be `abort()`-ed which makes the promise reject prematurely.
|
|
|
|
|
|
|
|
Once a timer is finished or aborted, it can't be started again
|
|
|
|
(because the promise should not be replaced). Instead, create
|
|
|
|
a new one through `clone()` or `cloneIfRun()`.
|
|
|
|
*/
|
|
|
|
export default class Timer {
|
2021-07-12 08:02:46 +00:00
|
|
|
private timerHandle: number;
|
2021-04-26 13:02:53 +00:00
|
|
|
private startTs: number;
|
|
|
|
private promise: Promise<void>;
|
|
|
|
private resolve: () => void;
|
|
|
|
private reject: (Error) => void;
|
2021-04-06 11:26:50 +00:00
|
|
|
|
2022-12-16 12:29:59 +00:00
|
|
|
public constructor(private timeout: number) {
|
2021-04-26 13:02:53 +00:00
|
|
|
this.setNotStarted();
|
2018-12-11 14:43:41 +00:00
|
|
|
}
|
|
|
|
|
2021-04-26 13:02:53 +00:00
|
|
|
private setNotStarted() {
|
|
|
|
this.timerHandle = null;
|
|
|
|
this.startTs = null;
|
|
|
|
this.promise = new Promise<void>((resolve, reject) => {
|
|
|
|
this.resolve = resolve;
|
|
|
|
this.reject = reject;
|
2018-12-11 14:43:41 +00:00
|
|
|
}).finally(() => {
|
2021-04-26 13:02:53 +00:00
|
|
|
this.timerHandle = null;
|
2018-12-11 14:43:41 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-26 14:01:05 +00:00
|
|
|
private onTimeout = () => {
|
2018-12-11 14:43:41 +00:00
|
|
|
const now = Date.now();
|
2021-04-26 13:02:53 +00:00
|
|
|
const elapsed = now - this.startTs;
|
|
|
|
if (elapsed >= this.timeout) {
|
|
|
|
this.resolve();
|
|
|
|
this.setNotStarted();
|
2018-12-11 14:43:41 +00:00
|
|
|
} else {
|
2021-04-26 13:02:53 +00:00
|
|
|
const delta = this.timeout - elapsed;
|
2022-11-30 11:32:56 +00:00
|
|
|
this.timerHandle = window.setTimeout(this.onTimeout, delta);
|
2018-12-11 14:43:41 +00:00
|
|
|
}
|
2021-06-29 12:11:58 +00:00
|
|
|
};
|
2018-12-11 14:43:41 +00:00
|
|
|
|
2022-12-16 12:29:59 +00:00
|
|
|
public changeTimeout(timeout: number) {
|
2021-04-26 13:02:53 +00:00
|
|
|
if (timeout === this.timeout) {
|
2018-12-11 14:43:41 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-04-26 13:02:53 +00:00
|
|
|
const isSmallerTimeout = timeout < this.timeout;
|
|
|
|
this.timeout = timeout;
|
2018-12-11 14:43:41 +00:00
|
|
|
if (this.isRunning() && isSmallerTimeout) {
|
2021-04-26 13:02:53 +00:00
|
|
|
clearTimeout(this.timerHandle);
|
|
|
|
this.onTimeout();
|
2018-12-11 14:43:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* if not started before, starts the timer.
|
2019-01-17 09:43:01 +00:00
|
|
|
* @returns {Timer} the same timer
|
2018-12-11 14:43:41 +00:00
|
|
|
*/
|
2022-12-16 12:29:59 +00:00
|
|
|
public start() {
|
2018-12-11 14:43:41 +00:00
|
|
|
if (!this.isRunning()) {
|
2021-04-26 13:02:53 +00:00
|
|
|
this.startTs = Date.now();
|
2022-11-30 11:32:56 +00:00
|
|
|
this.timerHandle = window.setTimeout(this.onTimeout, this.timeout);
|
2018-12-11 14:43:41 +00:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* (re)start the timer. If it's running, reset the timeout. If not, start it.
|
2019-01-17 09:43:01 +00:00
|
|
|
* @returns {Timer} the same timer
|
2018-12-11 14:43:41 +00:00
|
|
|
*/
|
2022-12-16 12:29:59 +00:00
|
|
|
public restart() {
|
2018-12-11 14:43:41 +00:00
|
|
|
if (this.isRunning()) {
|
|
|
|
// don't clearTimeout here as this method
|
|
|
|
// can be called in fast succession,
|
|
|
|
// instead just take note and compare
|
|
|
|
// when the already running timeout expires
|
2021-04-26 13:02:53 +00:00
|
|
|
this.startTs = Date.now();
|
2018-12-11 14:43:41 +00:00
|
|
|
return this;
|
|
|
|
} else {
|
|
|
|
return this.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* if the timer is running, abort it,
|
|
|
|
* and reject the promise for this timer.
|
2019-01-17 09:43:01 +00:00
|
|
|
* @returns {Timer} the same timer
|
2018-12-11 14:43:41 +00:00
|
|
|
*/
|
2022-12-16 12:29:59 +00:00
|
|
|
public abort() {
|
2018-12-11 14:43:41 +00:00
|
|
|
if (this.isRunning()) {
|
2021-04-26 13:02:53 +00:00
|
|
|
clearTimeout(this.timerHandle);
|
|
|
|
this.reject(new Error("Timer was aborted."));
|
|
|
|
this.setNotStarted();
|
2018-12-11 14:43:41 +00:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*promise that will resolve when the timer elapses,
|
|
|
|
*or is rejected when abort is called
|
|
|
|
*@return {Promise}
|
|
|
|
*/
|
2022-12-16 12:29:59 +00:00
|
|
|
public finished() {
|
2021-04-26 13:02:53 +00:00
|
|
|
return this.promise;
|
2018-12-11 14:43:41 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 12:29:59 +00:00
|
|
|
public isRunning() {
|
2021-04-26 13:02:53 +00:00
|
|
|
return this.timerHandle !== null;
|
2018-12-11 14:43:41 +00:00
|
|
|
}
|
|
|
|
}
|