2020-04-19 19:02:10 +02:00
|
|
|
import {AbortError} from "../../../utils/error.js";
|
2020-04-04 17:34:46 +02:00
|
|
|
|
2020-04-09 23:19:49 +02:00
|
|
|
class Timeout {
|
2020-04-04 17:34:46 +02:00
|
|
|
constructor(ms) {
|
|
|
|
this._reject = null;
|
2020-04-05 15:11:15 +02:00
|
|
|
this._handle = null;
|
2020-04-04 17:34:46 +02:00
|
|
|
this._promise = new Promise((resolve, reject) => {
|
|
|
|
this._reject = reject;
|
2020-04-05 15:11:15 +02:00
|
|
|
this._handle = setTimeout(() => {
|
2020-04-04 17:34:46 +02:00
|
|
|
this._reject = null;
|
|
|
|
resolve();
|
|
|
|
}, ms);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-05 15:11:15 +02:00
|
|
|
elapsed() {
|
2020-04-04 17:34:46 +02:00
|
|
|
return this._promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
abort() {
|
|
|
|
if (this._reject) {
|
|
|
|
this._reject(new AbortError());
|
2020-04-05 15:11:15 +02:00
|
|
|
clearTimeout(this._handle);
|
|
|
|
this._handle = null;
|
2020-04-04 17:34:46 +02:00
|
|
|
this._reject = null;
|
|
|
|
}
|
|
|
|
}
|
2020-05-05 23:12:14 +02:00
|
|
|
|
|
|
|
dispose() {
|
|
|
|
this.abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Interval {
|
|
|
|
constructor(ms, callback) {
|
|
|
|
this._handle = setInterval(callback, ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
dispose() {
|
|
|
|
if (this._handle) {
|
|
|
|
clearInterval(this._handle);
|
|
|
|
this._handle = null;
|
|
|
|
}
|
|
|
|
}
|
2020-04-04 17:34:46 +02:00
|
|
|
}
|
|
|
|
|
2020-05-05 23:12:14 +02:00
|
|
|
|
2020-04-09 23:19:49 +02:00
|
|
|
class TimeMeasure {
|
2020-04-04 17:34:46 +02:00
|
|
|
constructor() {
|
|
|
|
this._start = window.performance.now();
|
|
|
|
}
|
|
|
|
|
|
|
|
measure() {
|
|
|
|
return window.performance.now() - this._start;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-20 21:26:39 +02:00
|
|
|
export class Clock {
|
2020-04-04 17:34:46 +02:00
|
|
|
createMeasure() {
|
2020-04-09 23:19:49 +02:00
|
|
|
return new TimeMeasure();
|
2020-04-04 17:34:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
createTimeout(ms) {
|
2020-04-09 23:19:49 +02:00
|
|
|
return new Timeout(ms);
|
2020-04-04 17:34:46 +02:00
|
|
|
}
|
|
|
|
|
2020-05-05 23:12:14 +02:00
|
|
|
createInterval(callback, ms) {
|
|
|
|
return new Interval(ms, callback);
|
|
|
|
}
|
|
|
|
|
2020-04-04 17:34:46 +02:00
|
|
|
now() {
|
|
|
|
return Date.now();
|
|
|
|
}
|
|
|
|
}
|