This repository has been archived on 2022-08-19. You can view files and clone it, but cannot push or open issues or pull requests.
hydrogen-web/src/ui/web/dom/Clock.js

53 lines
1,019 B
JavaScript
Raw Normal View History

2020-04-10 02:49:49 +05:30
import {AbortError} from "../utils/error.js";
2020-04-10 02:49:49 +05:30
class Timeout {
constructor(ms) {
this._reject = null;
2020-04-05 18:41:15 +05:30
this._handle = null;
this._promise = new Promise((resolve, reject) => {
this._reject = reject;
2020-04-05 18:41:15 +05:30
this._handle = setTimeout(() => {
this._reject = null;
resolve();
}, ms);
});
}
2020-04-05 18:41:15 +05:30
elapsed() {
return this._promise;
}
abort() {
if (this._reject) {
this._reject(new AbortError());
2020-04-05 18:41:15 +05:30
clearTimeout(this._handle);
this._handle = null;
this._reject = null;
}
}
}
2020-04-10 02:49:49 +05:30
class TimeMeasure {
constructor() {
this._start = window.performance.now();
}
measure() {
return window.performance.now() - this._start;
}
}
2020-04-10 02:49:49 +05:30
export class Clock {
createMeasure() {
2020-04-10 02:49:49 +05:30
return new TimeMeasure();
}
createTimeout(ms) {
2020-04-10 02:49:49 +05:30
return new Timeout(ms);
}
now() {
return Date.now();
}
}